steinm6/cron-helper

防止并行执行cronjob的简单辅助工具

1.1.0 2014-10-20 11:17 UTC

This package is not auto-updated.

Last update: 2024-09-24 02:00:43 UTC


README

防止并行执行cronjob的简单辅助工具

安装

Composer

需要 "steinm6/cron-helper": "dev-master"

手动

只需包含文件 src/CronHelper.php

用法

使用文件名初始化CronHelper。CronHelper将使用此文件名进行锁定。

$cron = new CronHelper('myfilename');

要锁定执行,调用lock()函数。要解锁cronjob,使用unlock()函数。您可以通过调用getLockDuration()函数来确定cronjob锁定的时间长度,该函数返回自lock()以来经过的秒数。

以下是如何使用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();
}