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 final class RememberMeCookie {
Signed remember-me cookies in PHP
authentication
hmac
cookies
Intermediate
8 steps
php
<?php namespace App\Http\Middleware;
Caching HTTP responses with Laravel middleware
middleware
caching
http
Intermediate
8 steps
php
<?php namespace App\Validation;
Building a reusable address form validator in PHP
validation
error-accumulation
regex
Intermediate
9 steps
php
<?php namespace App\Http\Controllers;
A cached autocomplete endpoint in Laravel
caching
validation
query-ranking
Intermediate
8 steps
php
<?php declare(strict_types=1);
Normalizing human names in PHP
unicode
text-normalization
transliteration
Intermediate
8 steps
php
<?php namespace App\Http\Controllers;
Handling Stripe webhooks in Laravel
webhooks
signature-verification
dependency-injection
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.