php
48 lines · 7 steps
A retrying queue job in Laravel
A queued job that transcodes a podcast, retries on failure, and alerts Slack when it finally gives up.
Explained by
highlit
1<?php
2
3namespace App\Jobs;
4
5use App\Notifications\SlackJobFailed;
6use Illuminate\Bus\Queueable;
7use Illuminate\Contracts\Queue\ShouldQueue;
8use Illuminate\Foundation\Bus\Dispatchable;
9use Illuminate\Queue\InteractsWithQueue;
10use Illuminate\Queue\SerializesModels;
11use Illuminate\Support\Facades\Notification;
12use Illuminate\Support\Facades\Log;
13use Throwable;
14
15class ProcessPodcastUpload implements ShouldQueue
16{
17 use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
18
19 public int $tries = 3;
20 public int $timeout = 120;
21
22 public function __construct(public int $podcastId)
23 {
24 }
25
26 public function handle(): void
27 {
28 $podcast = \App\Models\Podcast::findOrFail($this->podcastId);
29
30 $podcast->transcode();
31 $podcast->markProcessed();
32 }
33
34 public function failed(Throwable $exception): void
35 {
36 Log::error('Podcast processing failed', [
37 'podcast_id' => $this->podcastId,
38 'exception' => $exception->getMessage(),
39 ]);
40
41 Notification::route('slack', config('services.slack.alerts_webhook'))
42 ->notify(new SlackJobFailed(
43 job: class_basename($this),
44 context: ['podcast_id' => $this->podcastId, 'attempts' => $this->attempts()],
45 exception: $exception,
46 ));
47 }
48}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Storing only an ID and re-fetching in handle keeps queued payloads small and always current.
- 2The tries and timeout properties bound retries and runtime without any extra wiring.
- 3A failed hook turns the last failure into observable signal — logs plus a human alert.
Related explainers
php
<?php namespace App\Console\Commands;
Releasing stale document locks in Laravel
artisan-command
transactions
row-locking
Intermediate
6 steps
ruby
class TemplateInterpolator PLACEHOLDER = /\{\{\s*([\w.]+)\s*\}\}/ def initialize(strict: false)
Interpolating templates with dotted keys in Ruby
regex
string-interpolation
hash-traversal
Intermediate
6 steps
php
<?php namespace App\Providers;
Subdomain multi-tenancy routing in Laravel
multi-tenancy
service-container
route-binding
Advanced
7 steps
php
<?php namespace App\Broadcasting;
Authorizing presence channels in Laravel
broadcasting
authorization
presence-channels
Intermediate
3 steps
go
package config import ( "fmt"
A thread-safe config singleton in Go
singleton
concurrency
environment-variables
Intermediate
7 steps
rust
use std::net::Ipv4Addr; use std::str::FromStr; #[derive(Debug, Clone, Copy)]
Parsing and matching IPv4 CIDR ranges in Rust
bitwise-operations
parsing
error-handling
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/a-retrying-queue-job-in-laravel-explained-php-e473/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.