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

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事件。

10-04 23:17
查看更多