简体   繁体   English

如何为 Laravel 工匠设置超时

[英]How to set timeout for Laravel artisan

How to set timeout for an artisan job?如何为工匠工作设置超时? And when time out the job will be killed.当超时时,工作将被杀死。

I'm using Laravel 5.0 on my application.I scheduled some artisan jobs every minute in app/Console/Kernel.php.我在我的应用程序上使用 Laravel 5.0。我每分钟在 app/Console/Kernel.php 中安排一些工匠工作。 When I grep those jobs当我grep那些工作

ps -ef | grep artisan

It seems that every job is running in separate process.似乎每个作业都在单独的进程中运行。

root 23322 1 0 13:44?根 23322 1 0 13:44? 00:00:00 xxx/php-5.5.7/bin/php artisan fetchTopic 0 10 1 00:00:00 xxx/php-5.5.7/bin/php 工匠 fetchTopic 0 10 1

root 23324 1 0 13:44?根 23324 1 0 13:44? 00:00:00 xxx/php-5.5.7/bin/php artisan fetchTopic 0 10 2 00:00:00 xxx/php-5.5.7/bin/php 工匠 fetchTopic 0 10 2

root 23326 1 0 13:44?根 23326 1 0 13:44? 00:00:00 xxx/php-5.5.7/bin/php artisan fetchComment 00:00:00 xxx/php-5.5.7/bin/php artisan fetchComment

And when one job finish its task, the process will be killed automatically.当一个作业完成任务时,该进程将自动终止。 But it is strange that some of the processes are not killed normally.但是奇怪的是有些进程没有被正常杀死。 And over time, more and more strange processes are permanent which lead to CPU 100%.随着时间的推移,越来越多的奇怪进程是永久性的,导致 CPU 100%。 So I want to set an timeout for the artisan job, when time out the job will be killed automatically.所以我想为工匠工作设置一个超时时间,超时时工作将自动终止。

I checked the Laravel code and there is no graceful way of killing these long artisan processes.我检查了 Laravel 代码,没有优雅的方法可以杀死这些漫长的工匠进程。

However you can limit overall php execution time on your own risk.但是,您可以自行承担风险来限制 php 的整体执行时间。 You can use function set_time_limit() for this or add max_execution_time in your php.ini您可以为此使用函数set_time_limit()或在 php.ini 中添加 max_execution_time

I had the same issue and changing the value of max_input_time in the cli php.ini file lead to the desired effect.我遇到了同样的问题,在cli php.ini 文件中更改max_input_time的值会导致预期的效果。

Example: ( /etc/php/7.3/cli/php.ini )示例:( /etc/php/7.3/cli/php.ini

;;;;;;;;;;;;;;;;;;;
; Resource Limits ;
;;;;;;;;;;;;;;;;;;;

; Maximum execution time of each script, in seconds
; http://php.net/max-execution-time
; Note: This directive is hardcoded to 0 for the CLI SAPI
max_execution_time = 99

You can specify max_execution_time as an option.您可以将max_execution_time指定为一个选项。

Example, instead of例如,而不是

php artisan your-slow-command

You can use:您可以使用:

php -d max_execution_time=900 artisan your-slow-command

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM