Laravel 队列 horizon 超时问题

本地环境

一开始使用 phpstudy 环境,直接执行 php artisan queue:work 命令,单个队列没有超时问题,即使单个队列耗时 30 分钟。

后来为了安装 horizon 面板,所以将项目移植到了 laradock 环境下。然后使用 php artisan horizon 命令执行队列,但我这边经过测试,单个任务执行时间最多为 1 分钟,然后队列就会被终止,因为我这边的单个队列执行可能有些会超过 30 分钟,所以想了解一下如何更改 horizon 的单个队列超时时间。
我这边试着更改 horizon 下的配置文件,更改如下,但好像不生效

 /*
    |--------------------------------------------------------------------------
    | Queue Wait Time Thresholds
    |--------------------------------------------------------------------------
    |
    | This option allows you to configure when the LongWaitDetected event
    | will be fired. Every connection / queue combination may have its
    | own, unique threshold (in seconds) before this event is fired.
    |
    */

    'waits' => [
        'redis:default' => 100,
    ],

Laravel 队列 horizon 超时问题

麻烦知道的大佬们,告诉一下我这个小菜鸡!谢谢

《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 6

SynVideo一看就是费时的操作

3年前 评论
taohua (楼主) 3年前

retry_after 改了吗,要比 timeout 大

3年前 评论
taohua (楼主) 3年前

看官方文档https://learnku.com/docs/laravel/7.x/queues#job-expirations-and-timeouts这一段:

The --timeout value should always be at least several seconds shorter than your retry_after configuration value. This will ensure that a worker processing a given job is always killed before the job is retried. If your --timeout option is longer than your retry_after configuration value, your jobs may be processed twice.

你设置的超时时间应该比重试时间稍微少一些,否则任务可能一直在重试而一直达不到超时时间。

3年前 评论
zs4336

遇到了同样的问题,不过已经解决了。以下给出解决方案:

1、首先预估你的队列任务中,耗时最长的时间是多少(单位为秒),比如 3 分钟,也就是 180 秒。那么就需要在 config/horizon.php 配置文件中配置超时时间 timeout选项,horizon默认的是 60 秒,此处配置为前面说的 180 秒。

config/horizon.php

return [

    .
    .
    .

    'environments' => [

        .
        .
        .

        'local' => [
            'supervisor-1' => [
                'connection' => 'redis',
                'queue' => ['default'],
                'balance' => 'simple',
                'processes' => 3,
                'tries' => 1,
                'timeout' => 180, // 在这里 <===
            ],
        ],
    ],
];

2、由于队列任务设置的是 redis 驱动,那么还需要在 config/queue.php 文件中配置 connections 选项的 redis 子项。重点修改 retry_after,该选项的值需要稍微高于上面配置的 180 秒,那就设为 200 秒好了。

config/queue.php

return [

    .
    .
    .

    'connections' => [

        .
        .
        .

        'redis' => [
            'driver' => 'redis',
            'connection' => 'default',
            'queue' => env('REDIS_QUEUE', 'default'),
            'retry_after' => 200, // 在这里 <===
            'block_for' => 5,
        ],

    ]

];

3、最后就是你的 supervisor 配置文件, 要确保 stopwaitsecs 的值大于运行时间最长的任务所消耗的秒数。否则,Supervisor 可能会在工作完成前终止任务。

[program:horizon]
process_name=%(program_name)s
command=php /home/forge/app.com/artisan horizon
autostart=true
autorestart=true
user=forge
redirect_stderr=true
stdout_logfile=/home/forge/app.com/horizon.log
stopwaitsecs=3600

4、友情提示,如果有修改代码或者修改了 supervisor 的配置文件,记得重启队列任务。

5个月前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!