我正在写一个简单的队列。
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事件。