如何使作业失败并使其跳过Laravel队列中的下一次尝试? [英] How to fail a job and make it skip next attempts in the queue in Laravel?

查看:35
本文介绍了如何使作业失败并使其跳过Laravel队列中的下一次尝试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的队列.

I'm writing a simple queue.

namespace App\Jobs;

use App\SomeMessageSender;

class MessageJob extends Job
{
    protected $to;
    protected $text;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($to, $text)
    {
        $this->to = $to;
        $this->text = $text;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle(SomeMessageSender $sender)
    {
    if ($sender->paramsAreValid($this->to, $this->text) {
            $sender->sendMessage($this->to, $this->text);
        }
    else {
        // Fail without being attempted any further
            throw new Exception ('The message params are not valid');
        }
    }
}

如果参数无效,则上面的代码将引发异常,从而导致作业失败,但是如果仍然有尝试,将再次尝试.相反,我想强制此操作立即失败,再也不会尝试.

If the params are not valid the above code will throw an exception which causes the job to fail but if it still has attempts left, it will be tried again. Instead I want to force this to fail instantly and never attempt again.

我该怎么做?

推荐答案

使用InteractsWithQueue 特征,如果要删除作业,则调用 delete(),如果要失败,则调用 fail($ exception = null).作业失败意味着它将被删除,登录到fail_jobs表中,并触发JobFailed事件.

Use the InteractsWithQueue trait and call either delete() if you want to delete the job, or fail($exception = null) if you want to fail it. Failing the job means it will be deleted, logged into the failed_jobs table and the JobFailed event is triggered.

这篇关于如何使作业失败并使其跳过Laravel队列中的下一次尝试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆