gpt4 book ai didi

laravel - 如何使作业失败并使其跳过 Laravel 队列中的下一次尝试?

转载 作者:行者123 更新时间:2023-12-02 23:25:27 24 4
gpt4 key购买 nike

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

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');
}
}
}

如果参数无效,上面的代码将抛出异常,导致作业失败,但如果仍有尝试,则会再次尝试。相反,我想强制它立即失败并且不再尝试

我该怎么做?

最佳答案

使用InteractsWithQueue如果您想删除作业,则调用 delete();如果您想让作业失败,则调用 fail($exception = null)。作业失败意味着它将被删除,记录到 failed_jobs 表中,并触发 JobFailed 事件。

关于laravel - 如何使作业失败并使其跳过 Laravel 队列中的下一次尝试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41693359/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com