gpt4 book ai didi

php 定时任务中断

转载 作者:IT王子 更新时间:2023-10-29 00:11:20 25 4
gpt4 key购买 nike

我在 php 中有一个 cronjob,它计算一些业务规则(例如:净转速、总转速、估计转速等......使用标准偏差和其他数学算法)

这个 cronjob 使用 exec 调用多个 cron 调用 3 个 php 脚本

对于每个调用,我都会在后台启动一个进程并告诉系统作业 x 已启动。例如:

这是我的逻辑

start 
main cron start - message
cron sub 1 start - message
run cron sub 1
cron sub 1 end - message

wait until cron sub 1 stop

process stuff

cron sub 2 start - message
run background cron sub 2 // this will automaticaly send a message when this jobs end

cron sub 3 start - message
run background cron sub 3 // this will automaticaly send a message when this jobs end

main cron end - message

end

我需要解决的是这个

如果有人手动运行作业并取消它 (ctrl+c) 或cron 中发生了一些不好的事情( fatal error 或无法连接到数据库)或其他任何东西(比如内存不足)

我想停止 cronjob 并告诉它发生了什么以及它停止的时间,这样我就可以从原来的地方开始。

这可能吗?谢谢

最佳答案

这是您必须尝试/调试的简单代码:

<?php
ignore_user_abort(false);

function shutdown() {
echo "shutdown function\n";
}
register_shutdown_function('shutdown');

class shutdown {
function __construct() {
echo "shutdown::construct\n";
}
function __destruct() {
echo "shutdown::destruct\n";
}
}

$s = new shutdown();

declare(ticks = 1);

function sig_handler($signo) {

switch ($signo) {
case SIGTERM:
echo "SIG: handle shutdown tasks\n";
break;
case SIGHUP:
echo "SIG: handle restart tasks\n";
break;
case SIGUSR1:
echo "SIG: Caught SIGUSR1\n";
break;
case SIGINT:
echo "SIG: Caught CTRL+C\n";
break;
default:
echo "SIG: handle all other signals\n";
break;
}
return;
}

echo "Installing signal handler...\n";

pcntl_signal(SIGTERM, "sig_handler");
pcntl_signal(SIGHUP, "sig_handler");
pcntl_signal(SIGUSR1, "sig_handler");
pcntl_signal(SIGINT, "sig_handler");

echo "Generating signal SIGTERM to self...\n";

# killing the script using bad memory allocation
/*
ini_set('memory_limit', '1K');
$data = '';
for($i = 0; $i < 1000; $i++) {
$data .= str_repeat($i . time(), time());
}
*/

# simulating CTRL+C
// for($i = 0; $i < 100000000; $i++) { } // don't forget to press CTRL+C

echo "Done\n";

如果 cronjob 正常运行你会得到:

shutdown::construct
Installing signal handler...
Generating signal SIGTERM to self...
Done
shutdown function
shutdown::destruct

如果遇到 fatal error :

shutdown::construct
Installing signal handler...
Generating signal SIGTERM to self...
PHP Fatal error: Possible integer overflow in memory allocation (11 * 1321404273 + 1) in /var/www/domain.com/php.php on line 51
shutdown function

如果你终止进程:

kill <processid>

shutdown::construct
Installing signal handler...
Generating signal SIGTERM to self...
SIG: handle shutdown tasks
Done
shutdown function
shutdown::destruct

您可以使用 posix_kill(posix_getpid(), <signal you like>);在每种情况下也是如此。

了解更多:

http://php.net/manual/en/function.pcntl-signal.php

http://en.wikipedia.org/wiki/Signal_%28computing%29

http://users.actcom.co.il/~choo/lupg/tutorials/signals/signals-programming.html

关于php 定时任务中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8136983/

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