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 class NameParser {
Parsing a full name into components in PHP
string-parsing
arrays
normalization
Intermediate
8 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
php
<?php final class RotatingFileLogger {
How a rotating file logger works in PHP
logging
file-rotation
io
Intermediate
9 steps
php
class TwoFactorController extends Controller { public function show(Request $request): View|RedirectResponse {
Two-factor auth challenge flow in Laravel
authentication
two-factor
middleware
Intermediate
10 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.