laravel 作业尝试次数过多或运行时间过长
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53075318/
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
Job has been attempted too many times or run too long
提问by Webnet
I have a job that works flawless locally, but in production I run into issues where it doesn't work. I've encompassed the entire handle()
with a try/catch
and am not seeing anything logged to Bugsnag, despite many other exceptions elsewhere from being deployed.
我有一份在本地工作完美无缺的工作,但在生产中我遇到了它不起作用的问题。尽管在其他地方部署了许多其他例外,但我已经handle()
用 a包含了整个内容try/catch
并且没有看到任何记录到 Bugsnag 的内容。
public function handle() {
try {
// do stuff
} catch (\Exception $e) {
Bugsnag::notifyException($e);
throw $e;
}
}
According to Laravel Horizonthis queue job runs for 0.0026001930236816406
seconds and I never see it work and never see any other errors in the failed_jobs
table as it relates to this job.
根据Laravel Horizon,这个队列作业运行了0.0026001930236816406
几秒钟,我从来没有看到它工作,也从来没有看到failed_jobs
表中与这个作业相关的任何其他错误。
config/queue.php
配置/队列.php
'redis' => [
'driver' => 'redis',
'connection' => 'default',
'queue' => 'default',
'retry_after' => (60 * 10), // 10 minutes
'block_for' => null,
],
config/horizon.php
配置/地平线.php
'environments' => [
'production' => [
'supervisor' => [
'connection' => 'redis',
'queue' => [
'default',
],
'balance' => 'auto',
'processes' => 10,
'tries' => 3,
// 10 seconds under the queue's retry_after to avoid overlap
'timeout' => (60 * 10) - 10, // Just under 10 mins
],
If something is causing this job to retry over and over, how can I find out how? I'm at a loss.
如果某些原因导致此作业一遍又一遍地重试,我该如何找出方法?我不知所措。
Investigation thus far
调查至今
- My expectation is I should be able to run the query:
- 我的期望是我应该能够运行查询:
SELECT DISTINCT exception, COUNT(id) as errors
FROM failed_jobs
WHERE payload LIKE '%[TAG-JOB-HAS]%'
GROUP BY exception;
To see more than this error message:
要查看更多此错误消息:
Job has been attempted too many times or run too long
作业尝试次数过多或运行时间过长
but that's all I see.
但这就是我所看到的。
- Laravel Horizon's dashboard shows the job in question is running for < 1 second, so I know it's not actually timing out.
- Laravel Horizon的仪表板显示有问题的作业运行了 < 1 秒,所以我知道它实际上并没有超时。
回答by Anil Kumar
Try to catch the exception in the failed method given by laravel
尝试捕获laravel给出的失败方法中的异常
/**
* The job failed to process.
*
* @param Exception $exception
* @return void
*/
public function failed(Exception $exception)
{
// Send user notification of failure, etc...
}
and check whether your default queue driver in local is sync then its expected behavior.
并检查您在本地的默认队列驱动程序是否同步,然后是其预期行为。
回答by a_sarana
According to documentation, you can handle job failing in two common ways:
根据文档,您可以通过两种常见方式处理作业失败:
- using failed job events
- using
failed()
method.
- 使用失败的作业事件
- 使用
failed()
方法。
In the first case, you can handle all jobs using Queue::failing()
method. You'll receive Illuminate\Queue\Events\JobFailed
event as a parameter, and it contains exception.
在第一种情况下,您可以使用Queue::failing()
方法处理所有作业。您将接收Illuminate\Queue\Events\JobFailed
事件作为参数,它包含异常。
In another case, you can use failed()
method, it should be placed near your handle()
method. You can receive Exception $exception
as a parameter too.
在另一种情况下,您可以使用failed()
方法,它应该放在您的handle()
方法附近。您也可以Exception $exception
作为参数接收。
Example:
例子:
public function failed(\Throwable $exception)
{
// Log failure
}
Hope this helps.
希望这可以帮助。
回答by Yasser
I had the same problem
我有同样的问题
I fixed it by increasing the 'retry_after' parameter
我通过增加“retry_after”参数来修复它
make sure the retry_after value is greater than the time it takes a job to run
确保 retry_after 值大于运行作业所需的时间
in config/queue.phpfile
在config/queue.php文件中
'connections' => [
'sync' => [
'driver' => 'sync',
],
'database' => [
'driver' => 'database',
'table' => 'jobs',
'queue' => 'default',
'retry_after' => 9000,
],