|
9 | 9 | - [Skip Jobs](#laravel-tip--skip-jobs-️) |
10 | 10 | - [Keeping Jobs Unique Until Processing](#laravel-tip--keeping-jobs-unique-until-processing-️) |
11 | 11 | - [Rate Limit Jobs](#laravel-tip--rate-limit-jobs-️) |
| 12 | +- [Monitor Failed Jobs](#laravel-tip--monitor-failed-jobs-️) |
12 | 13 |
|
13 | 14 | ## Laravel Tip 💡: Dispatch After Response ([⬆️](#queues--jobs-tips-cd-)) |
14 | 15 |
|
@@ -184,3 +185,37 @@ public function middleware(): array |
184 | 185 | return [new RateLimited('reports')]; |
185 | 186 | } |
186 | 187 | ``` |
| 188 | + |
| 189 | +## Laravel Tip 💡: Monitor Failed Jobs ([⬆️](#queues--jobs-tips-cd-)) |
| 190 | + |
| 191 | +Have you ever needed to keep an eye on failed jobs and get notified when that happens? The failing method allows you to do exactly that 🚀 |
| 192 | + |
| 193 | +```php |
| 194 | +<?php |
| 195 | + |
| 196 | +namespace App\Providers; |
| 197 | + |
| 198 | +use Illuminate\Support\Facades\Queue; |
| 199 | +use Illuminate\Support\ServiceProvider; |
| 200 | +use Illuminate\Queue\Events\JobFailed; |
| 201 | +use App\Jobs\Contracts\ShouldNotifyOnFailures; |
| 202 | + |
| 203 | +class AppServiceProvider extends ServiceProvider |
| 204 | +{ |
| 205 | + public function boot(): void |
| 206 | + { |
| 207 | + Queue::failing(function (JobFailed $event) { |
| 208 | + $event->exception->getMessage(); // Exception message |
| 209 | + $event->job->getName(); // Job class |
| 210 | + $event->job->getRawBody(); // Job body |
| 211 | + $event->exception->getTraceAsString(); // Exception trace |
| 212 | + |
| 213 | + // You can even mark the jobs with a contract to decide |
| 214 | + // whether to send notifications or not 🔥 |
| 215 | + if ($event->job instanceof ShouldNotifyOnFailures) { |
| 216 | + $notifiable->notify(new JobFailedNotification($event)); |
| 217 | + } |
| 218 | + }); |
| 219 | + } |
| 220 | +} |
| 221 | +``` |
0 commit comments