【发布时间】:2020-03-11 10:04:49
【问题描述】:
我正在尝试找出正确的方法。
public $tries = 10;
/**
* Execute the job.
*
* @return void
*/
public function handle(){
$result_of_some_logic = false;
if($result_of_some_logic){
// for the purpose of this example, we're all done here.
} else{
// we need to retry 10 minutes from now. how to trigger this attempt, and this attempt only!, to fail?
}
}
我阅读了 laravel 文档,但我不清楚正确的方法是什么。我注意到,如果我创建了一个 php 错误(例如 throw new whatisnotdeclaredinnamespace()),则作业尝试将失败,工作人员将重试直到超过 $tries 的数量。这几乎是我想要的行为,但我显然想要一个干净的代码解决方案。
总结一下:在 Laravel 5.8 中,在 handle() 函数中“标记”尝试失败的正确方法是什么?
【问题讨论】:
-
你能解释一下为什么你不想使用异常吗?
-
因为它会导致 laravel 日志中出现大量堆栈跟踪日志,而大多数时候“异常”只是第三方要求我等待我可以向他们的 API 提交另一个请求。我想优雅地看到尝试次数上升,直到最大尝试次数过去,然后升级为异常并让 failed() 方法检索异常消息并发送通知电子邮件。
标签: laravel queue jobs laravel-artisan