Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Can I use environment variables to customize job failure notifications


Can I use environment variables to customize job failure notifications


Yes, you can use environment variables to customize job failure notifications in Laravel. Here are some steps to achieve this:

1. Create Environment Variables:
Create environment variables in your `.env` file to store the notification details. For example, you can create variables for the notification channels, such as `FAILED_JOB_MAIL_TO` and `FAILED_JOB_SLACK_WEBHOOK_URL`.

php
   FAILED_JOB_MAIL_TO=
 <script language='JavaScript' type='text/javascript'>
 <!--
 var prefix = 'm&#97;&#105;lt&#111;:';
 var suffix = '';
 var attribs = '';
 var path = 'hr' + 'ef' + '=';
 var addy92166 = '&#101;m&#97;&#105;l' + '&#64;';
 addy92166 = addy92166 + '&#101;x&#97;mpl&#101;' + '&#46;' + 'c&#111;m';
 document.write( '<a ' + path + '\'' + prefix + addy92166 + suffix + '\'' + attribs + '>' );
 document.write( addy92166 );
 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>
   FAILED_JOB_SLACK_WEBHOOK_URL=https://hooks.slack.com/services/your-slack-webhook-url
   

2. Configure the Notification:
In your notification class, you can use these environment variables to customize the notification. For example, you can use them to set the recipient for the mail notification or the Slack webhook URL.

php
   namespace App\Notifications;

   use Illuminate\Bus\Queueable;
   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())
               ->to(env('FAILED_JOB_MAIL_TO'));
       }

       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());
               })
               ->webhookUrl(env('FAILED_JOB_SLACK_WEBHOOK_URL'));
       }
   }
   

3. Use Environment Variables in the Job Class:
In your job class, you can use these environment variables to customize the notification. For example, you can use them to set the recipient for the mail notification or the Slack webhook URL.

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;

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

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

       public function failed(Exception $exception)
       {
           Notification::route('mail', env('FAILED_JOB_MAIL_TO'))
               ->route('slack', env('FAILED_JOB_SLACK_WEBHOOK_URL'))
               ->notify(new JobFailedNotification($exception));
       }
   }
   

By using environment variables, you can easily customize the notification details without having to modify the code. This approach allows you to manage the notification settings in a centralized location and ensures that the notifications are sent to the correct recipients and channels[1][3].

Citations:
[1] https://github.com/talvbansal/laravel-throttled-failed-jobs
[2] https://laracasts.com/discuss/channels/laravel/retrying-a-failed-job-with-different-environment-variables
[3] https://github.com/spatie/laravel-failed-job-monitor
[4] https://stackoverflow.com/questions/56995795/laravel-failed-job-doesnt-send-notification
[5] https://stackoverflow.com/questions/52266315/laravel-job-notification-failing