0

When using queueable notifications:

class MyNotification extends Notification implements ShouldQueue
{

    use Queueable;
}

How do I handle failed jobs? If I'd have dispatched the email/notification via a job class I could use the failed method:

public function failed(Exception $exception) {

  Log::debug('MyNotification failed');

}

However the failed method in a notification doesn't work

Alex
  • 6,497
  • 11
  • 47
  • 58
  • The failed method works in notifications... – Salim Djerbouh Sep 25 '19 at 20:17
  • So the failed() method above placed in the MyNotification class should work? Its not logging the error for me. Is there some other class I need to import to fire the failed() method. – Alex Sep 25 '19 at 22:31

2 Answers2

1

You should check Laravel documentation here.

For example, in your AppServiceProvider you can add:

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

Handling of the failed job is not responsibility of the notifications but of the queues.

  • Simply dropping a link is not an answer, propose a solution to the OP on SO, and even when linking to docs, copy-paste the part where OP would find a solution in case the website goes down – Salim Djerbouh Sep 25 '19 at 20:30
  • @CaddyDZ thanks for the comment, I will edit my answer to explain more – Aleksandar Milivojsa Sep 25 '19 at 20:33
  • Isn't that going to handle all failed jobs though? I know I could filter but I'm wondering if there is a way to handle failed jobs on the notification class. – Alex Sep 25 '19 at 21:58
1

Caddy DZ is correct there is a handle() method for notifications: https://github.com/illuminate/notifications/blob/master/SendQueuedNotifications.php#L92

My issue was not importing the Exception class, code should be:

public function failed(\Exception $exception) {

  Log::debug('MyNotification failed');

}
Alex
  • 6,497
  • 11
  • 47
  • 58