php
39 lines · 6 steps
Sequencing site setup with Laravel job chains
A queued job chain runs provisioning steps in strict order and marks the site failed if any link throws.
Explained by
highlit
1<?php
2
3namespace App\Services\Provisioning;
4
5use App\Jobs\ConfigureDnsRecords;
6use App\Jobs\InstallSslCertificate;
7use App\Jobs\NotifyCustomerProvisioned;
8use App\Jobs\ProvisionServerInstance;
9use App\Jobs\WarmApplicationCache;
10use App\Models\Site;
11use Illuminate\Support\Facades\Bus;
12use Throwable;
13
14class SiteProvisioner
15{
16 public function launch(Site $site): void
17 {
18 Bus::chain([
19 new ProvisionServerInstance($site),
20 new ConfigureDnsRecords($site),
21 new InstallSslCertificate($site),
22 new WarmApplicationCache($site),
23 new NotifyCustomerProvisioned($site),
24 ])
25 ->onConnection('redis')
26 ->onQueue('provisioning')
27 ->catch(function (Throwable $e) use ($site) {
28 $site->forceFill([
29 'status' => 'failed',
30 'failure_reason' => $e->getMessage(),
31 ])->save();
32
33 report($e);
34 })
35 ->dispatch();
36
37 $site->forceFill(['status' => 'provisioning'])->save();
38 }
39}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Bus::chain guarantees each job only starts after the previous one succeeds, giving you ordered async workflows.
- 2A single catch closure centralizes failure handling for the whole chain instead of scattering it across jobs.
- 3Recording an in-progress status before dispatch lets the UI reflect work that hasn't completed yet.
Related explainers
php
<?php namespace App\Services;
Building signed, expiring download URLs in PHP in Laravel
hmac
url-signing
authentication
Intermediate
7 steps
php
<?php declare(strict_types=1);
A single-action JSON user controller in PHP
invokable-class
json-api
validation
Intermediate
7 steps
php
final class UserActivityStream { public function __construct( private readonly \PDO $db,
Streaming user activity with PHP generators
generators
pagination
lazy-evaluation
Intermediate
8 steps
php
<?php namespace App\Casts;
How a custom Eloquent cast wraps an Address in Laravel
value-objects
serialization
data-mapping
Intermediate
7 steps
php
use Illuminate\Support\Facades\Route; use App\Http\Controllers\Admin\DashboardController; use App\Http\Controllers\Admin\UserController; use App\Http\Controllers\Admin\ReportController;
How nested route groups compose in Laravel
routing
middleware
authorization
Intermediate
8 steps
php
final class TokenBucketLimiter { private float $tokens; private float $lastRefill;
How a token bucket rate limiter works
rate-limiting
token-bucket
throttling
Intermediate
7 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/sequencing-site-setup-with-laravel-job-chains-explained-php-0a6a/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.