首页 > 解决方案 > 如何在侦听器的失败作业之间延迟

问题描述

我需要在 listeners 上的特定失败作业之间设置延迟。我知道是否指定 onption它的工作原理,但我需要监听--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');
    }
}

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

解决方案


您用于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);

推荐阅读