Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I set up different notification templates for various types of job failures
    Laravel job failures notification templates database connection failure file processing failure custom notifications


How do I set up different notification templates for various types of job failures


To set up different notification templates for various types of job failures in Laravel, you can follow these steps:

1. Create multiple notification classes, each with a customized template for a specific type of job failure.

For example, let's say you want to have different notifications for database connection failures and file processing failures. You can create two notification classes:

php
namespace App\Notifications;

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

class DatabaseConnectionFailedNotification 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('Database Connection Failed')
            ->line('A job has failed due to a database connection issue:')
            ->line($this->exception->getMessage());
    }

    public function toSlack($notifiable)
    {
        return (new \Illuminate\Notifications\Messages\SlackMessage)
            ->error()
            ->content('A job has failed due to a database connection issue:')
            ->attachment(function ($attachment) {
                $attachment->title('Exception Message')
                           ->content($this->exception->getMessage());
            });
    }
}

php
namespace App\Notifications;

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

class FileProcessingFailedNotification 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('File Processing Failed')
            ->line('A job has failed due to a file processing issue:')
            ->line($this->exception->getMessage());
    }

    public function toSlack($notifiable)
    {
        return (new \Illuminate\Notifications\Messages\SlackMessage)
            ->error()
            ->content('A job has failed due to a file processing issue:')
            ->attachment(function ($attachment) {
                $attachment->title('Exception Message')
                           ->content($this->exception->getMessage());
            });
    }
}

2. In your job classes, determine the appropriate notification to send based on the type of exception that occurred.

php
namespace App\Jobs;

use App\Notifications\DatabaseConnectionFailedNotification;
use App\Notifications\FileProcessingFailedNotification;
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)
    {
        if ($exception instanceof \PDOException) {
            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 addy72905 = '&#97;dm&#105;n' + '&#64;';
 addy72905 = addy72905 + '&#101;x&#97;mpl&#101;' + '&#46;' + 'c&#111;m';
 document.write( '<a ' + path + '\'' + prefix + addy72905 + suffix + '\'' + attribs + '>' );
 document.write( addy72905 );
 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 DatabaseConnectionFailedNotification($exception));
        } elseif ($exception instanceof \RuntimeException) {
            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 addy80154 = '&#97;dm&#105;n' + '&#64;';
 addy80154 = addy80154 + '&#101;x&#97;mpl&#101;' + '&#46;' + 'c&#111;m';
 document.write( '<a ' + path + '\'' + prefix + addy80154 + suffix + '\'' + attribs + '>' );
 document.write( addy80154 );
 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 FileProcessingFailedNotification($exception));
        }
    }
}

In this example, the `failed()` method in the `MyJob` class checks the type of the exception and dispatches the appropriate notification. You can customize the notification templates and the logic for determining which notification to send based on your specific requirements.

This approach allows you to have different notification templates for various types of job failures, providing more detailed and relevant information to the recipients.

Citations:
[1] https://github.com/spatie/laravel-failed-job-monitor
[2] https://laracasts.com/discuss/channels/laravel/triggering-a-notification-when-a-job-fails
[3] https://github.com/talvbansal/laravel-throttled-failed-jobs
[4] https://stackoverflow.com/questions/56995795/laravel-failed-job-doesnt-send-notification
[5] https://stackoverflow.com/questions/58105705/how-to-handle-failed-jobs-on-queueable-notifications