Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How to send custom notifications for Laravel failed Jobs
    Laravel custom notifications failed jobs ShouldQueue Queueable email notifications Slack notifications Laravel jobs


How to send custom notifications for Laravel failed Jobs


Here is how you can send custom notifications for failed Laravel jobs:

1. Create a custom notification class that implements the `ShouldQueue` interface and the `Queueable` trait. This will allow the notification to be queued and delivered asynchronously.

php
namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class JobFailedNotification extends Notification implements ShouldQueue
{
    use Queueable;

    protected $exception;

    public function __construct($exception)
    {
        $this->exception = $exception;
    }

    public function via($notifiable)
    {
        return ['mail', 'slack'];
    }

    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->subject('Job Failed')
            ->line('A job has failed with the following exception:')
            ->line($this->exception->getMessage());
    }

    public function toSlack($notifiable)
    {
        return (new \Illuminate\Notifications\Messages\SlackMessage)
            ->error()
            ->content('A job has failed with the following exception:')
            ->attachment(function ($attachment) {
                $attachment->title('Exception Message')
                           ->content($this->exception->getMessage());
            });
    }
}

2. In your job class, override the `failed()` method and dispatch the custom notification:

php
namespace App\Jobs;

use App\Notifications\JobFailedNotification;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Notification;

class MyJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public function handle()
    {
        // Your job logic here
    }

    public function failed(Exception $exception)
    {
        Notification::route('mail', '
 <script language='JavaScript' type='text/javascript'>
 <!--
 var prefix = 'm&#97;&#105;lt&#111;:';
 var suffix = '';
 var attribs = '';
 var path = 'hr' + 'ef' + '=';
 var addy48319 = '&#97;dm&#105;n' + '&#64;';
 addy48319 = addy48319 + '&#101;x&#97;mpl&#101;' + '&#46;' + 'c&#111;m';
 document.write( '<a ' + path + '\'' + prefix + addy48319 + suffix + '\'' + attribs + '>' );
 document.write( addy48319 );
 document.write( '<\/a>' );
 //-->
 </script><script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '<span style=\'display: none;\'>' );
 //-->
 </script>This e-mail address is being protected from spambots. You need JavaScript enabled to view it
 <script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '</' );
 document.write( 'span>' );
 //-->
 </script>')
            ->route('slack', 'https://hooks.slack.com/services/...')
            ->notify(new JobFailedNotification($exception));
    }
}

In this example, the `JobFailedNotification` class is responsible for sending the notification via both email and Slack. The `failed()` method in the job class dispatches the notification to the configured notifiable routes (email and Slack webhook URL).

The key points are:

1. Create a custom notification class that implements `ShouldQueue` and `Queueable`.
2. Override the `failed()` method in your job class and dispatch the custom notification.
3. Configure the notification to be sent via the desired channels (email, Slack, etc.).

This approach allows you to customize the notification content and delivery channels for failed jobs, providing more detailed and actionable information to the relevant parties.

Citations:
[1] https://github.com/talvbansal/laravel-throttled-failed-jobs
[2] https://stackoverflow.com/questions/56995795/laravel-failed-job-doesnt-send-notification
[3] https://stackoverflow.com/questions/49509750/send-error-report-to-sentry-within-laravel-queue/49511013
[4] https://laravel.com/docs/11.x/notifications
[5] https://laravel.com/docs/11.x/queues