Skip to content

Commit 818d064

Browse files
committed
added tip #328
1 parent 50253c4 commit 818d064

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Currently, there are over 300 tips categorized as follows:
1919
- 🌐 [API & HTTP Client Tips](./tips/api-and-http-client.md) (12 tips)
2020
- 💡 [Random Cool Tips](./tips/others.md) (15 tips)
2121
- 🖼️ [View Tips](./tips/views.md) (10 tips)
22-
- 📬 [Queues & Job Tips](./tips/queues-and-jobs.md) (9 tips)
22+
- 📬 [Queues & Job Tips](./tips/queues-and-jobs.md) (10 tips)
2323
- 🔒 [Authentication & Authorization Tips](./tips/auth.md) (6 tips)
2424
- 📦 [Laravel Container Tips](./tips/container.md) (6 tips)
2525
- ⚠️ [Error Handling Tips](./tips/error-handling.md) (4 tips)

tips/queues-and-jobs.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- [Skip Jobs](#laravel-tip--skip-jobs-️)
1010
- [Keeping Jobs Unique Until Processing](#laravel-tip--keeping-jobs-unique-until-processing-️)
1111
- [Rate Limit Jobs](#laravel-tip--rate-limit-jobs-️)
12+
- [Monitor Failed Jobs](#laravel-tip--monitor-failed-jobs-️)
1213

1314
## Laravel Tip 💡: Dispatch After Response ([⬆️](#queues--jobs-tips-cd-))
1415

@@ -184,3 +185,37 @@ public function middleware(): array
184185
return [new RateLimited('reports')];
185186
}
186187
```
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

Comments
 (0)