我有一个在 cron 上运行的 PHP 脚本,它具有以下功能:
- 处理队列中的项目
- 如果项目处理成功,则 processQueueItem() 方法返回 true。如果不成功,则返回 false。
- 在 while 循环中重复 50 分钟
该代码还包括将信息输出到我设置的日志的回声。
这是代码:
<?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";
?>
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.
我不太确定为什么我的进程在它有一些项目之后没有结束,然后在脚本的一次运行中没有项目。如果这没有任何意义,我真的很抱歉,但任何见解将不胜感激!