Simple helper to prevent parallel cronjob execution
Require "steinm6/cron-helper": "dev-master"
Just include the file src/CronHelper.php
Initialize the CronHelper with a filename. The CronHelper will use this filename for locking up.
$cron = new CronHelper('myfilename');
To lock the execution call the lock()-function. To unlock the cronjob use the unlock()-function. You may determine how long the cronjob was locked by calling the getLockDuration()-function, which returns the time passed since the lock() in seconds.
Here is a basic example on how to use the CronHelper:
use steinm6\CronHelper\CronHelper;
// Initialize CronHelper
$cron = new CronHelper('lockfile-name');
// lock this job
if ($cron->lock()) {
foreach($x as $y){
// You might want to reset the timer, to prevent running into the unlock() below...
$cron->resetTimer();
// Do something
sleep(10);
}
$cron->unlock();
} else {
// If the lock persists for 3600 seconds, something went wrong. Remove the lock so that the next cronjob is executed.
if ($cron->getLockDuration() > 3600)
$cron->unlock();
}