Skip to content
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

Run prepareNestedBatches on append/prependToChain & chain #52486

Merged
merged 4 commits into from
Aug 15, 2024

Conversation

SabatinoMasala
Copy link
Contributor

The issue with appending/prepending a batch from within a job

Currently, when appending or prepending a batch from within a job, you get the following error:

Serialization of 'Closure' is not allowed 

Diving into the Queuable class, this is because prepend/appendToChain fails to turn the PendingBatch into a ChainedBatch. The workaround for this is by creating a ChainedBatch manually as described here:
#52468

Proposal

Run ChainedBatch::prepareNestedBatches when appending or prepending a job from within another job, so you can add a batch onto a current chain as follows:

<?php

namespace App\Jobs;

use Illuminate\Bus\ChainedBatch;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Bus;

class Chainer implements ShouldQueue
{
    use Queueable;

    public function __construct() {}

    public function handle(): void {
        $batch = Bus::batch([
            new Logger('From batch'),
            new Logger('From batch'),
            new Logger('From batch'),
            Bus::batch([
                new Logger('From nested batch'),
            ]),
            new Logger('From batch'),
        ]);
        $this->appendToChain($batch);
    }
}

Discussion

Currently, my implementation of appendToChain/prependToChain wraps the single job in a collection to match the expected format of ChainedBatch::prepareNestedBatches() - this feels kinda unnecessary, so this could be further improved by either:

  • allowing an array of jobs to be appended/prepended
  • refactoring prepareNestedBatches and creating a method that accepts a single job (in addition to the currently available method that accepts a collection)

Happy to make the necessary changes if needed.

@SabatinoMasala SabatinoMasala changed the title Run prepareNestedBatches on append/prependToChain Run prepareNestedBatches on append/prependToChain & chain Aug 14, 2024
@SabatinoMasala
Copy link
Contributor Author

I've also just updated the chain method for consistency.

e6a3735

@taylorotwell taylorotwell merged commit 0b3d2aa into laravel:11.x Aug 15, 2024
29 checks passed
@taylorotwell
Copy link
Member

Thanks!

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.

2 participants