我可以在 Laravel 中处理作业超时吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48689146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I handle jobs timeout in Laravel?
提问by Nikola
I'm using Laravel 5.5 and i have some jobs that are checking emails with IMAP. Sometimes that can take too long, or lets say that user mistakes port or username, it would take too long for IMAP server to respond. So I came up with idea that i can restrict my jobs to some period, but when that period expires, not only that my job isn't moved to the failed jobs, but also my worker is killed. What am I missing? Is there any other way to do this? I would like to make some changes in the DB if the job expires. Any idea? Thank you in advance
我正在使用 Laravel 5.5,我有一些工作使用 IMAP 检查电子邮件。有时这可能需要很长时间,或者说用户错误地使用端口或用户名,IMAP 服务器需要很长时间才能响应。所以我想出了我可以将我的工作限制在某个时期的想法,但是当那个时期到期时,不仅我的工作不会转移到失败的工作中,而且我的工人也会被杀死。我错过了什么?有没有其他方法可以做到这一点?如果工作到期,我想对数据库进行一些更改。任何的想法?先感谢您
回答by Sangar82
First, you need to manage your workers with Supervisor. If they are killed, supervisor will restart them again.
https://laravel.com/docs/5.5/queues#supervisor-configuration
首先,您需要使用 Supervisor 管理您的员工。如果它们被杀死,supervisor 将重新启动它们。
https://laravel.com/docs/5.5/queues#supervisor-configuration
After, you need to read about job expirations and timeouts in Laravel docs.
https://laravel.com/docs/5.5/queues#job-expirations-and-timeouts
之后,您需要在 Laravel 文档中阅读有关作业到期和超时的信息。
https://laravel.com/docs/5.5/queues#job-expirations-and-timeouts
To solve your problem you need to increase the timeout of your workers. You can try with 3600 seconds. You need to increase the job expiration too (retry_after value in your queues on config/queues.pho). Try with 3550 seconds. If you see the problem occurs again, you can increase the timeouts values.
要解决您的问题,您需要增加工作人员的超时时间。您可以尝试 3600 秒。您还需要增加作业到期时间(config/queues.pho 上队列中的 retry_after 值)。尝试 3550 秒。如果您发现问题再次出现,您可以增加超时值。
Is very important that your code have try/catch to catch exceptions to exit if you catch an issue and don't wait 3550 seconds to release the job and the worker.
如果您发现问题并且不要等待 3550 秒来释放作业和工作人员,那么您的代码必须具有 try/catch 以捕获异常以退出,这一点非常重要。