有时调用cron两次或三次 [英] Calling cron twice or triple sometimes

查看:172
本文介绍了有时调用cron两次或三次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不得不做cron Job,因为我没有cPanel的访问



我的cron Job类的逻辑,是当有人点击从网站,验证时间和调用类。



... so ....



这里是我的类cronJob,和我的问题是,有时它调用类两次或三次 ..(也许当前时间从两个或更多的用户是相同的...

 <?php 
class cronJob extends Core {
public function execute(){
include(interfaces / cronJob.php);
$ items = $ this-> query(SELECT`id`,`class`,`interval`,`time`from`cron_job`);
while($ item = $ this-> fetch($ items))
{
if($ this-> getTime()> $ item ['time']){
$ this-> query(UPDATE`cron_job` SET` time` ='。($ this-> getTime()+ $ item ['interval'])。''WHERE`id` = '。$ item ['id']。');
$ this-> getLog(cronJob:。$ this-> getTime()。Class:。$ item ['class']);

include_once(cron /\".$ item ['class']。。php);
$ job = new $ item ['class'];
$ job-> run();
}
}
}
}
?>

...和...这里是时间函数(我认为不必要:P )

  public function getTime(){
return time()
}

日志:

  2013-04-14 12:59:02  -  cronJob:1365890342类:updateFiles 
2013-04-14 12:59:37 - cronJob:1365890377类:updateTrivia
2013-04-14 01:00:13 - cronJob:1365890413类:updateTrivia
2013-04-14 01:00:49 - cronJob:1365890449类:updateTrivia
2013-04-14 01:01:25 - cronJob:1365890485类:updateTrivia
2013-04-14 01:02:01 - cronJob:1365890521类:updateTrivia
2013-04-14 01:02:37 - cronJob: 1365890557类:updateTrivia
2013-04-14 01:03:13 - cronJob:1365890593类:updateTrivia
2013-04-14 01:03:49 - cronJob:1365890629类:updateTrivia
2013-04-14 01:04:25 - cronJob:1365890665类:updateTrivia
2013-04-14 ** 01:05:01 ** - cronJob:1365890701类:updateTrivia
2013-04- 14 ** 01:05:01 ** - cronJob:1365890701类:updateTrivia

p>

>



所以...有任何人有想法修复它吗?

解决方案

通常称为 互斥锁 的功能。



另一个问题SO 可能给你一些灵感,如何在你的情况下实现锁。

请注意,提到的 APC 功能是一个可选的PHP模块,不是大多数标准发行版(不在许多网站主机上)。


I had to done something like "cron Job" because I don't have access to cPanel

My logic of this cron Job class, was when someone hits something from the website, verify the time and call the class.

...so....

Here is my class of cronJob, and my problem is that sometimes it calls the class twice or triple.. (maybe the current time from two or more users is the same...)

<?php
class cronJob extends Core {
    public function execute() {
        include("interfaces/cronJob.php");
        $items = $this->query("SELECT `id`, `class`, `interval`, `time` FROM `cron_job`");
        while($item = $this->fetch($items))
        {
            if($this->getTime() > $item['time']) {
                $this->query("UPDATE `cron_job` SET `time` = '".($this->getTime() + $item['interval'])."' WHERE `id` = '".$item['id']."'");
                $this->getLog("cronJob: ".$this->getTime()." Class: ".$item['class']);

                include_once("cron/".$item['class'].".php");
                $job = new $item['class'];
                $job->run();
            } 
        }
    }
}
?>

...and... here is the time function (I do not think it's necessary :P)

public function getTime() {
    return time() + 305;
}

The logs:

2013-04-14 12:59:02 - cronJob: 1365890342 Class: updateFiles
2013-04-14 12:59:37 - cronJob: 1365890377 Class: updateTrivia
2013-04-14 01:00:13 - cronJob: 1365890413 Class: updateTrivia
2013-04-14 01:00:49 - cronJob: 1365890449 Class: updateTrivia
2013-04-14 01:01:25 - cronJob: 1365890485 Class: updateTrivia
2013-04-14 01:02:01 - cronJob: 1365890521 Class: updateTrivia
2013-04-14 01:02:37 - cronJob: 1365890557 Class: updateTrivia
2013-04-14 01:03:13 - cronJob: 1365890593 Class: updateTrivia
2013-04-14 01:03:49 - cronJob: 1365890629 Class: updateTrivia
2013-04-14 01:04:25 - cronJob: 1365890665 Class: updateTrivia
2013-04-14 **01:05:01** - cronJob: 1365890701 Class: updateTrivia
2013-04-14 **01:05:01** - cronJob: 1365890701 Class: updateTrivia

Table:

so... does anyone have any idea to fix it ?

解决方案

What you need is a feature commonly referred to as an exclusive lock.

Another question on SO might give you some inspiration on how to implement a lock in your case.
Note, that the mentioned APC feature is an optional PHP module not shipped with most standard distributions (and not present on many web hosts)

这篇关于有时调用cron两次或三次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆