Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 4, 2025

Conversation

stevebauman
Copy link
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)
Copy link
Contributor Author

@stevebauman stevebauman Aug 4, 2025

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!

Copy link
Contributor

@shaedrich shaedrich Aug 4, 2025

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