Laravel 事件监听队列失败并删除
前几天写了队列,一直有问题很头疼不知道错哪了最后加了一个监听事件哎烦不说了上代码吧
app/Provides/EventServiceProvider 中加入
//队列异常报错都会走这里
use Illuminate\Queue\Events\JobExceptionOccurred;
/**
* Register any events for your application.
*
* @return void
*/
public function boot()
{
parent::boot();
Event::listen(JobExceptionOccurred::class, JobLogListener::class);
//
}
app/Listeners文件下
创建JobLogListener
/**
* Handle the event.
*
* @param JobExceptionOccurred $event
*
* @return void
*/
public function handle(JobExceptionOccurred $event)
{
//获取当前执行的队列实例
$job = $event->job;
//收集错误的实例
$exception = $event->exception;
//获取重试的次数 如果失败三次 就干掉
$attempts = $job->attempts();
$payLoad = $event->connectionName;
if ($attempts >= 3) {
$contentStr = "===============开始分割线===================\r\n";
$contentStr .= "队列时间:" . date('Y-m-d H:i:s') . "\r\n";
$contentStr .= "队列名称 :" . $payLoad. . "\r\n";
$contentStr .= "错误信息 :" . $exception->getMessage() . "\r\n";
$contentStr .= "报错行数 :" . $exception->getLine() . "\r\n";
$contentStr .= "报错文件 :" . $exception->getFile() . "\r\n";
$contentStr .= "===============结束分割线===================\r\n";
$contentStr .= "\r\n";
//这里会输出你的错误位置
file_put_contents(storage_path('/logs/Queue_field.txt'), $contentStr, FILE_APPEND);
//这块是推送钉钉你们也是不用
Ding::send($contentStr);
//删除队列
$job->delete();
}
}