0

我不得不做类似“cron Job”之类的事情,因为我无权访问 cPanel

我对这个 cron Job 类的逻辑是,当有人从网站上点击某些东西时,验证时间并调用该类。

...所以....

这是我的 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() + 305;
}

日志:

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

桌子:

在此处输入图像描述

所以......有没有人有任何想法来解决它?

4

2 回答 2

0

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)

于 2013-04-13T22:38:22.630 回答
0

您可以尝试:

  • 使用数据库阻止它:例如,锁定表/启动事务(小心,选项在 MyISAM 引擎上不可用)。
  • 写入和读取文件。如果文件的内容 != "x",则跳过更新。
于 2013-04-13T22:29:02.363 回答