Skip to content

[12.x] Add ability to prepend() and append() to PendingChain#56536

Merged
taylorotwell merged 4 commits into
laravel:12.xfrom
stevebauman:append-prepend-pending-chain
Aug 4, 2025
Merged

[12.x] Add ability to prepend() and append() to PendingChain#56536
taylorotwell merged 4 commits into
laravel:12.xfrom
stevebauman:append-prepend-pending-chain

Conversation

@stevebauman

Copy link
Copy Markdown
Contributor

Description:

This PR adds the ability to prepend and append jobs on a Bus PendingChain, giving developers the ability to conditionally transform the chain before its dispatched.

Example:

$recording = Media::first();

$chain = Bus::chain([
    new TranscribeRecording($recording)
]);

if ($this->hasAudioNormalizationEnabled()) {
    $chain->prepend(new NormalizeAudio($recording));
}

if ($this->hasTranscriptionRedactionEnabled()) {
    $chain->append(new RedactTranscription($recording));
}

$chain->dispatch();

In the example above:

  • If audio normalization is enabled, the NormalizeAudio job will be moved to the first position of the chain and the TranscribeRecording job will be moved to the second
  • If transcription redaction is enabled, it will be pushed onto the back of the chain ensuring it is always the last job to run regardless of whichever ones preceded it

Let me know your thoughts! Thanks for your time 🙏

* @return \Illuminate\Foundation\Bus\PendingChain
*/
public function chain($jobs)
public function chain($jobs = null)

@stevebauman stevebauman Aug 4, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was intentionally changed so developers can make chains without any initial argument:

$chain = Bus::chain();

$chain->append(...);

$chain->prepend(...);

$chain->dispatch();

If you'd rather avoid this, let me know!

@shaedrich shaedrich Aug 4, 2025

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Funny how one can be so trained to see something, that one instantly goes
image
as in this case: when() 😅

Bus::chain([
    new TranscribeRecording($recording)
])->when(
    $this->hasAudioNormalizationEnabled(),
    fn ($chain) => $chain->prepend(new NormalizeAudio($recording)),
)->when(
    $this->hasTranscriptionRedactionEnabled(),
    fn ($chain) => $chain->append(new RedactTranscription($recording)),
)->dispatch();

@taylorotwell taylorotwell merged commit c24122b into laravel:12.x Aug 4, 2025
58 of 60 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants