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条)

报告相同问题?

悬赏问题

  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?