如何在侦听器的失败作业之间延迟

时间:2018-09-28 16:32:32

标签: laravel laravel-5 laravel-5.5 laravel-queue

我需要设置特定的失败作业之间的延迟,在侦听器上。 我知道是否应指定工作--delay=5,但是我需要在侦听器上进行特定的延迟(而不是在标准作业上)。我尝试将属性delay放在侦听器上,但不起作用。

<?php

namespace Froakie\Listeners;

use Carbon\Carbon;
use Froakie\Events\ExampleEvent;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

/**
 * Class ExampleListener
 *
 * @package Froakie\Listeners
 * @author Miguel Borges <miguel.borges@edirectinsure.com>
 */
class ExampleListener implements ShouldQueue
{
    use InteractsWithQueue;

    /**
     * The number of seconds the job can run before timing out.
     *
     * @var int
     */
    public $timeout = 5;

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

    public $delay = 5;

    public $seconds;

    /**
     * Handle the event.
     *
     * @param \Froakie\Events\ExampleEvent $event
     * @throws \Exception
     */
    public function handle(ExampleEvent $event)
    {
//        $this->delay(5);
            throw new \Exception('test');
    }
}

1 个答案:

答案 0 :(得分:1)

您使用release来延迟重试。示例:

public function handle(ExampleEvent $event)
{
    if ($this->attempts() <= $this->tries) {
        try {

            //Try something

        } catch (\Exception $e) {
            //Try again later
            $this->release($this->delay)
        }
    } else {
        //Force end the job
        $this->delete();
    }
}

但是应注意,输入的值是延迟时间(以秒为单位)。因此,如果您想将其延迟5分钟:

$this->release(300);
相关问题