douzhao5656 2012-06-01 21:12
浏览 102
已采纳

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

I've got a PHP script running on a cron that has the following functionality:

  1. Processes items in a queue
  2. If an item was processed successfully, processQueueItem() method returns true. If unsuccessful, returns false.
  3. Repeats in a while loop for 50 minutes

The code also includes echos to output information to a log I have set up.

Here's the code:

<?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.", "
";

$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.
";

}

echo date("M d Y H:i:s"), " cron.processQueue.php has ended. Processed ", $itemCount, " items. 
"; 

?>

The processQueueItem() is working fine (that's why I didn't include the script in this post) and the output is working fine, but my counting of processed items is acting weird.

If the script runs and the queue is empty, it will sleep(60) and return false for processQueueItem(). It outputs "cron.processQueue.php has started" along with 50 "Script still running... 0 Processed" lines. Lastly it outputs "has ended" script. Here's an example:

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. 

However, if the queue has 100 items in it (queue is being processed at roughly 1 item a second), the script completes all the items, gives an output of its last "Script still running..." and then stops. Since this script is run on a cron, the next item that is listed on the log is "cron.processQueue.php has started." Here's an example:

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.

I'm not quite sure why my process isn't ending after its had some items and then there are no items, within one run of the script. I'm really sorry if that makes no sense whatsoever, but any insight would be greatly appreciated!

  • 写回答

2条回答 默认 最新

  • duanbinian2243 2012-06-01 21:33
    关注

    The problem is definitely the while loop:

    while( time() < $expireOn ) {
    

    This loop will run until the time expires, and it doesn't take into account the status of the queue. You need to do something like:

    $continue = true;
    while( time() < $expireOn && !($continue === false)) {
    
        if( $continue = $consumer->processQueueItem() )
    

    This will cause the while loop to stop once $consumer->processQueueItem() returns false.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥85 maple软件,solve求反函数,出现rootof怎么办?
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题