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

Walkthrough

Space play step click any line
Three takeaways
  1. 1Bus::chain guarantees each job only starts after the previous one succeeds, giving you ordered async workflows.
  2. 2A single catch closure centralizes failure handling for the whole chain instead of scattering it across jobs.
  3. 3Recording an in-progress status before dispatch lets the UI reflect work that hasn't completed yet.

Related explainers

Share this explainer

Here's the card — post it anywhere.

Sequencing site setup with Laravel job chains — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code