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

Adding a batch to a chain through prependToChain/appendToChain results in serialization errors #52468

Closed
SabatinoMasala opened this issue Aug 13, 2024 · 2 comments

Comments

@SabatinoMasala
Copy link
Contributor

Laravel Version

11.20.0

PHP Version

8.2

Database Driver & Version

No response

Description

When creating and dispatching a chain with a nested batch, the type of the batch will be a ChainedBatch

\Illuminate\Support\Facades\Bus::chain([
    Bus::batch([
        new \App\Jobs\HelloWorld(),
    ])
])->dispatch();

However, when appending or prepending a batch to a chain, the type will be a PendingBatch, leading to serialization errors.

\Illuminate\Support\Facades\Bus::chain([
    new \App\Jobs\Batcher(),
])->dispatch();

// App\Jobs\Batcher()

<?php

namespace App\Jobs;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Bus;

class Batcher implements ShouldQueue
{
    use Queueable;

    /**
     * Create a new job instance.
     */
    public function __construct()
    {
        //
    }

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        // The type of this batch will be PendingBatch
        $this->prependToChain(
            Bus::batch([
                new \App\Jobs\HelloWorld(),
            ])
        );
    }
}

To mitigate this, you can manually create a ChainedBatch from the PendingBatch as follows:

$chainedBatch = new ChainedBatch(Bus::batch([
    new HelloWorld(),
]));
$this->prependToChain($chainedBatch);

And this will make sure the batch gets queued onto the chain correctly.

Steps To Reproduce

Dispatching a batch onto a chain from within a job using prependToChain or appendToChain will result into a closure serialization error.

Copy link

Thank you for reporting this issue!

As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.

If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.

Thank you!

@driesvints
Copy link
Member

Seems the PR for this one was merged. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants