dev-master
9999999-devHandles critical sections while executing multiple php scripts in parallel. Useful for cron and any other recurrently run scripts.
MIT
The Requires
- php >= 5.4.0
locker parallel critical mutex semaphore section
Wallogit.com
2017 © Pedro Peláez
Handles critical sections while executing multiple php scripts in parallel. Useful for cron and any other recurrently run scripts.
This library handles problem of code critical section which should be executed only once at a time., (*2)
You have a cron task that is executing every minute, but it takes some time to execute. When server load's go critical it can take more than minute, which leads to bugs as a same cron is executed in multiple process at the same time., (*3)
You can encapsulate this code into critical section with use of this library., (*4)
``` php $criticalSection = new FileCriticalSection();, (*5)
$criticalSection->enter('cron_long_task'); doSomething(); $criticalSection->leave('cron_long_task');, (*6)
This code example does take care of critical sections. You say which critical section should be entered. In this case it is `cron_long_task`. No other script can enter this critical section until the original one leaves the critical section. Keep in mind that critical section is left even if the script ends, or `CriticalSection` object is destructed. If you want to enter critical section you can't script will wait until it can be entered. If you need to skip code which is already in critical section use timeout or `canEnter` method. Timeout critical section ------------------------ You can also do timeout enter. This way you can say explicitly say how long should the script wait to enter critical section. This is useful if you want to do something, but you know it only make sense in some time interval. ``` php $criticalSection = new FileCriticalSection(); $code = __FILE__; if ($criticalSection->enter($code, 30)) { doSomething(); $criticalSection->leave($code); }
Keep in mind that you have to check if enter returned true if you use timeout., (*7)
If you are using everyminute cron (or even faster), we can assume that you can skip one or two calls here and there. To do so you can check if critical section can be entered before entering it., (*8)
``` php $criticalSection = new FileCriticalSection();, (*9)
$code = FILE; if ($criticalSection->canEnter($code)) { $criticalSection->enter($code); doSomething(); $criticalSection->leave($code); }, (*10)
Another way to do so is to enter critical section with zero timeout. ``` php $criticalSection = new FileCriticalSection(); $code = __FILE__; if ($criticalSection->enter($code, 0)) { doSomething(); $criticalSection->leave($code); }
This bundle is under the MIT license. See the complete license in the bundle:, (*11)
LICENSE
Issues and feature requests are tracked in the Github issue tracker., (*12)
Handles critical sections while executing multiple php scripts in parallel. Useful for cron and any other recurrently run scripts.
MIT
locker parallel critical mutex semaphore section