【问题标题】:How to handle failed jobs on queueable notifications如何处理可排队通知上的失败作业
【发布时间】:2019-09-25 19:54:32
【问题描述】:

使用可排队通知时:

class MyNotification extends Notification implements ShouldQueue
{

    use Queueable;
}

如何处理失败的作业?如果我通过作业类发送电子邮件/通知,我可以使用 failed 方法:

public function failed(Exception $exception) {

  Log::debug('MyNotification failed');

}

但是通知中的失败方法不起作用

【问题讨论】:

  • 失败的方法适用于通知...
  • 那么上面放在 MyNotification 类中的 failed() 方法应该可以工作吗?它没有为我记录错误。是否需要导入其他类来触发 failed() 方法。

标签: php laravel laravel-queue laravel-notification


【解决方案1】:

你应该查看 Laravel 文档here

例如,在您的 AppServiceProvider 中您可以添加:

public function boot()
{
    Queue::failing(function (JobFailed $event) {
        // $event->connectionName
        // $event->job
        // $event->exception
    });
}

处理失败的作业不是通知的责任,而是队列的责任。

【讨论】:

  • 简单地删除链接不是答案,在 SO 上为 OP 提出解决方案,甚至在链接到文档时,复制粘贴 OP 可以找到解决方案的部分,以防网站出现故障
  • @CaddyDZ 感谢您的评论,我将编辑我的答案以解释更多
  • 那不是可以处理所有失败的工作吗?我知道我可以过滤,但我想知道是否有办法处理通知类上的失败作业。
【解决方案2】:

Caddy DZ 是正确的,有一个用于通知的 handle() 方法: https://github.com/illuminate/notifications/blob/master/SendQueuedNotifications.php#L92

我的问题是没有导入 Exception 类,代码应该是:

public function failed(\Exception $exception) {

  Log::debug('MyNotification failed');

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-27
    • 2015-07-06
    • 2021-07-03
    • 1970-01-01
    • 1970-01-01
    • 2020-01-31
    • 2012-07-18
    • 1970-01-01
    相关资源
    最近更新 更多