gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 21:41:13 24 4
gpt4 key购买 nike

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

  1. 处理队列中的项目
  2. 如果项目处理成功,processQueueItem() 方法返回 true。如果不成功,则返回 false。
  3. 在 while 循环中重复 50 分钟

该代码还包括将信息输出到我设置的日志的 echo 。

代码如下:

<?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.

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

最佳答案

问题肯定是 while 循环:

while( time() < $expireOn ) {

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

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

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

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

关于php - 在 cron 上运行 PHP 脚本。日志输出不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10857408/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com