steinm6/cron-helper

Simple helper to prevent parallel cronjob execution

1.1.0 2014-10-20 11:17 UTC

This package is not auto-updated.

Last update: 2024-12-17 03:09:38 UTC


README

Simple helper to prevent parallel cronjob execution

Installation

Composer

Require "steinm6/cron-helper": "dev-master"

Manually

Just include the file src/CronHelper.php

Usage

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();
}