Using after commit for jobs in bus batch laravel

66 views Asked by At

I'm trying to use job batch with after commit jobs but I can't figure out what is wrong here and can't find such example in the docs or I'm missing something this exception is thrown serialization of 'Closure' is not allowed in vendor/laravel/framework/src/Illuminate/Queue/Queue.php line 159

Bus::batch([
            app(StorePostMainImage::class, [
                'post' => $post,
                'tempImageId' => $dto->mainImageId,
            ])->afterCommit(),
            app(StorePostImages::class, [
                'post' => $post,
                'tempImagesIds' => $dto->imagesIds,
            ])->afterCommit(),
        ])->then(function (Batch $batch) use ($post) {
            event(new PostImageBusProcessed(
                postId: $post->id,
                batchId: $batch->id
            ));
        })->dispatch();

this is the StorePostMainImage class

public function __construct(
        protected StorePostMainImageAction $storePostMainImageAction,
        protected int $tempImageId,
        protected Post $post,
    ) {
    }

        public function handle(): void
    {
        $this->storePostMainImageAction->execute(
            post: $this->post,
            tempImageId: $this->tempImageId
        );
    }

this is the StorePostImages class

public function __construct(
        protected StorePostImagesAction $storePostImagesAction,
        protected Post $post,
        protected array $tempImageIds
    ) {
    }

    public function handle(): void
    {
        $this->storePostImagesAction->execute(
            post: $this->post,
            tempImageIds: $this->tempImageIds
        );
    }

i have tried to use jobs without using app() helper and remove the action classes from the jobs constructor and use them in handle like

app(StorePostImagesAction::class)->execute(
            post: $this->post,
            tempImageIds: $this->tempImageIds
        );

with these changes no exceptions thrown but the batch dispatched before the transaction committed or even rolled back and the jobs processed idk if the after committed jobs with bus batches is applicable by the framework or not

0

There are 0 answers