Illuminate\Queue\MaxAttemptsExceededException:App\Jobs\ProcessOrder已尝试次数过多或运行时间过长。该任务可能之前已超时。
Illuminate\Queue\MaxAttemptsExceededException: App\Jobs\ProcessOrder has been attempted too many times or run too long. The job may have previously timed out.
ID: php/laravel-queue-worker-memory-leak
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Laravel 10 | active | — | — | — |
| Laravel 11 | active | — | — | — |
| PHP 8.1 | active | — | — | — |
| PHP 8.2 | active | — | — | — |
根因分析
队列工作进程超出了允许的内存限制(memory_limit)或任务的超时时间(retry_after或--timeout),导致任务被释放并重试,直到达到最大尝试次数阈值。
English
A queue worker process exceeded the allowed memory limit (memory_limit) or the job's timeout (retry_after or --timeout), causing the job to be released and retried until it reaches the maximum attempts threshold.
官方文档
https://laravel.com/docs/11.x/queues#max-attempts解决方案
-
Increase PHP's memory_limit in php.ini or via ini_set() for the queue worker: memory_limit = 512M. Then restart the worker: php artisan queue:restart.
-
Add explicit garbage collection in the job's handle() method: unset($largeVariable); gc_collect_cycles(); Also ensure no circular references exist.
-
Use the --max-jobs option to restart the worker after a set number of jobs: php artisan queue:work --max-jobs=100. This prevents memory accumulation.
无效尝试
常见但无效的做法:
-
60% 失败
This only delays the problem; the underlying memory leak or timeout issue still exists, and the job will eventually fail again or consume excessive resources.
-
70% 失败
Disabling timeouts can cause workers to hang indefinitely, leading to resource starvation and blocking other jobs.
-
80% 失败
Restarting resets the worker state but does not fix the memory leak; the error will reoccur after enough jobs are processed.