在cron上运行PHP脚本。日志输出不正确

时间:2012-06-01 21:12:04

标签: php cron

我在cron上运行了一个PHP脚本,它具有以下功能:

  1. 处理队列中的项目
  2. 如果项目处理成功,则processQueueItem()方法返回true。如果不成功,则返回false。
  3. 在一个循环中重复50分钟
  4. 该代码还包括回声,用于将信息输出到我设置的日志中。

    以下是代码:

    <?php
    require_once('/var/www/vhosts/com-ext.com/httpdocs/745_TheYGSGroup/LWC_API/com/MindFireInc/LookWhosClicking/class.LWC.php');    
    require_once('/var/www/vhosts/com-ext.com/httpdocs/745_TheYGSGroup/LWC_API/com/MindFireInc/Database/class.DB.php');
    require_once('/var/www/vhosts/com-ext.com/httpdocs/745_TheYGSGroup/includes/classes.php');
    
    echo date("M d Y H:i:s"), " cron.processQueue.php has started.", "\n";
    
    $consumer = new Consumer;
    
    set_time_limit(3300);
    $RUNS_MIN = 10;
    $itemCount = 0;
    
    $startedOn = time();
    $expireOn = $startedOn + (60 * $RUNS_MIN); // In Sec
    while( time() < $expireOn ) {
    
        if( $consumer->processQueueItem() )
            $itemCount++;
    
        if( $itemCount % 60 == 0 )
            echo date("M d Y H:i:s"), " Script still running... ", $itemCount, " processed.\n";
    
    }
    
    echo date("M d Y H:i:s"), " cron.processQueue.php has ended. Processed ", $itemCount, " items. \n"; 
    

    &GT;

    processQueueItem()工作正常(这就是为什么我没有在这篇文章中包含脚本)并且输出工作正常,但我对处理过的项目的计数表现得很奇怪。

    如果脚本运行且队列为空,它将休眠(60)并为processQueueItem()返回false。它输出“cron.processQueue.php已启动”以及50“脚本仍在运行... 0已处理”行。最后它输出“已结束”脚本。这是一个例子:

    Jun 01 2012 11:00:02 cron.processQueue.php has started.
    Jun 01 2012 11:01:02 Script still running... 0 processed.
    Jun 01 2012 11:02:02 Script still running... 0 processed.
    Jun 01 2012 11:03:02 Script still running... 0 processed.
    Jun 01 2012 11:04:02 Script still running... 0 processed.
    Jun 01 2012 11:05:02 Script still running... 0 processed.
    Jun 01 2012 11:06:02 Script still running... 0 processed.
    Jun 01 2012 11:07:02 Script still running... 0 processed.
    Jun 01 2012 11:08:02 Script still running... 0 processed.
    Jun 01 2012 11:09:02 Script still running... 0 processed.
    Jun 01 2012 11:10:02 Script still running... 0 processed.
    Jun 01 2012 11:11:02 Script still running... 0 processed.
    Jun 01 2012 11:12:02 Script still running... 0 processed.
    Jun 01 2012 11:13:02 Script still running... 0 processed.
    Jun 01 2012 11:14:02 Script still running... 0 processed.
    Jun 01 2012 11:15:02 Script still running... 0 processed.
    Jun 01 2012 11:16:02 Script still running... 0 processed.
    Jun 01 2012 11:17:02 Script still running... 0 processed.
    Jun 01 2012 11:18:02 Script still running... 0 processed.
    Jun 01 2012 11:19:02 Script still running... 0 processed.
    Jun 01 2012 11:20:02 Script still running... 0 processed.
    Jun 01 2012 11:21:02 Script still running... 0 processed.
    Jun 01 2012 11:22:02 Script still running... 0 processed.
    Jun 01 2012 11:23:02 Script still running... 0 processed.
    Jun 01 2012 11:24:02 Script still running... 0 processed.
    Jun 01 2012 11:25:02 Script still running... 0 processed.
    Jun 01 2012 11:26:02 Script still running... 0 processed.
    Jun 01 2012 11:27:02 Script still running... 0 processed.
    Jun 01 2012 11:28:02 Script still running... 0 processed.
    Jun 01 2012 11:29:02 Script still running... 0 processed.
    Jun 01 2012 11:30:02 Script still running... 0 processed.
    Jun 01 2012 11:31:02 Script still running... 0 processed.
    Jun 01 2012 11:32:02 Script still running... 0 processed.
    Jun 01 2012 11:33:02 Script still running... 0 processed.
    Jun 01 2012 11:34:02 Script still running... 0 processed.
    Jun 01 2012 11:35:02 Script still running... 0 processed.
    Jun 01 2012 11:36:02 Script still running... 0 processed.
    Jun 01 2012 11:37:02 Script still running... 0 processed.
    Jun 01 2012 11:38:02 Script still running... 0 processed.
    Jun 01 2012 11:39:02 Script still running... 0 processed.
    Jun 01 2012 11:40:02 Script still running... 0 processed.
    Jun 01 2012 11:41:02 Script still running... 0 processed.
    Jun 01 2012 11:42:02 Script still running... 0 processed.
    Jun 01 2012 11:43:02 Script still running... 0 processed.
    Jun 01 2012 11:44:02 Script still running... 0 processed.
    Jun 01 2012 11:45:02 Script still running... 0 processed.
    Jun 01 2012 11:46:02 Script still running... 0 processed.
    Jun 01 2012 11:47:02 Script still running... 0 processed.
    Jun 01 2012 11:48:02 Script still running... 0 processed.
    Jun 01 2012 11:49:02 Script still running... 0 processed.
    Jun 01 2012 11:50:02 Script still running... 0 processed.
    Jun 01 2012 11:50:02 cron.processQueue.php has ended. Processed 0 items. 
    

    但是,如果队列中有100个项目(队列每秒处理大约1个项目),则脚本完成所有项目,给出其最后一个“脚本仍在运行...”的输出,然后停止。由于此脚本在cron上运行,因此日志中列出的下一个项目是“cron.processQueue.php已启动”。这是一个例子:

    May 31 2012 16:30:01 pullData.php has started.
    May 31 2012 16:31:18 Script still running... 60 processed.
    May 31 2012 16:32:33 Script still running... 120 processed.
    May 31 2012 16:33:48 Script still running... 180 processed.
    May 31 2012 16:35:03 Script still running... 240 processed.
    May 31 2012 16:36:19 Script still running... 300 processed.
    May 31 2012 16:37:34 Script still running... 360 processed.
    May 31 2012 16:38:50 Script still running... 420 processed.
    May 31 2012 16:40:05 Script still running... 480 processed.
    May 31 2012 16:41:21 Script still running... 540 processed.
    May 31 2012 16:42:36 Script still running... 600 processed.
    May 31 2012 16:43:51 Script still running... 660 processed.
    May 31 2012 16:45:05 Script still running... 720 processed.
    May 31 2012 16:46:22 Script still running... 780 processed.
    May 31 2012 16:47:37 Script still running... 840 processed.
    May 31 2012 16:48:52 Script still running... 900 processed.
    May 31 2012 16:50:09 Script still running... 960 processed.
    May 31 2012 16:51:31 Script still running... 1020 processed.
    May 31 2012 16:52:46 Script still running... 1080 processed.
    May 31 2012 16:54:01 Script still running... 1140 processed.
    May 31 2012 16:55:16 Script still running... 1200 processed.
    May 31 2012 16:56:32 Script still running... 1260 processed.
    May 31 2012 16:57:47 Script still running... 1320 processed.
    May 31 2012 16:59:02 Script still running... 1380 processed.
    May 31 2012 17:00:17 Script still running... 1440 processed.
    May 31 2012 17:01:33 Script still running... 1500 processed.
    May 31 2012 17:02:48 Script still running... 1560 processed.
    May 31 2012 17:30:01 cron.processQueue.php has started.
    

    我不太清楚为什么我的进程在它有一些项目之后没有结束,然后在脚本的一次运行中没有项目。我真的很抱歉,如果这没有任何意义,但任何见解将不胜感激!

2 个答案:

答案 0 :(得分:1)

问题肯定是while循环:

while( time() < $expireOn ) {

此循环将一直运行,直到时间到期,并且会考虑队列的状态。您需要执行以下操作:

$continue = true;
while( time() < $expireOn && !($continue === false)) {

    if( $continue = $consumer->processQueueItem() )

一旦while返回$consumer->processQueueItem(),这将导致false循环停止。

答案 1 :(得分:0)

我敢打赌问题在于$ consumer-&gt; processQueueItem()。它可能正在工作,但你可能没有返回TRUE(或者根本没有任何值[void]),所以你的项目计数器永远不会增加