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 class NameParser {
Parsing a full name into components in PHP
string-parsing
arrays
normalization
Intermediate
8 steps
rust
use serde::Deserialize; #[derive(Debug, Deserialize)] #[serde(untagged)]
Parsing flexible JSON shapes with serde
deserialization
enums
json
Intermediate
6 steps
ruby
require "shellwords" require "open3" module Backup
Building safe shell commands in Ruby
shell-out
subprocess
command-injection
Intermediate
7 steps
php
<?php namespace App\Services\Checkout;
Validating coupons with Laravel's Pipeline
pipeline
chain of responsibility
transactions
Intermediate
7 steps
php
<?php namespace App\Services;
How a password strength validator works in PHP
validation
regular-expressions
data-driven
Intermediate
8 steps
php
<?php namespace App\Services;
Building a cached daily leaderboard in Laravel
caching
aggregation
eager-loading
Intermediate
9 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.