Laravel 5.2队列作业继续重试

时间:2018-03-26 06:24:31

标签: php laravel redis laravel-5.2

class ProcessComment extends Job implements ShouldQueue
{
    use InteractsWithQueue;

    /**
     * @var int
     */
    public $tries = 1;


    public function handle(Somedepency $someDependency) {
         // method body....
         // tries to connect to a database
         // deliberately provide the wrong database url so that the job .  
         // will throw exception and hence faild
    }

问题是当我运行php artisan queue:work --daemonphp artisan queue:work --daemon --tries=1

tries选项似乎不起作用。在我的redis队列中,我不断看到它试图制作的尝试。它应该只尝试一次,如果工作失败,那就忽略那份工作并继续前进。

"EXEC"
1522044746.165780 [0 172.20.0.5:48992] "WATCH" "queues:comments:reserved"
1522044746.166110 [0 172.20.0.5:48992] "ZRANGEBYSCORE" "queues:comments:reserved" "-inf" "1522044746"
1522044746.166718 [0 172.20.0.5:48992] "UNWATCH"
1522044746.167436 [0 172.20.0.5:48992] "LPOP" "queues:comments"
1522044746.168051 [0 172.20.0.5:48992] "ZADD" "queues:comments:reserved" "1522044806" {"some serialized data here ... "attempts: 4"}

等等

这是我的configs / queue.php

'default' => env('QUEUE_DRIVER', 'redis'),

    'connections' => [

        'redis' => [
            'driver' => 'redis',
            'connection' => 'default',
            'queue' => 'comments'
        ],
    ],

尝试谷歌很多,但找不到满意的答案。

由于

1 个答案:

答案 0 :(得分:0)

您需要做的就是添加一个公共$ tries属性,在该数字之后,作业将失败。

/**
 * The number of times the job may be attempted.
 *
 * @var int
 */
public $tries = 5;