Laravel 9 PHP 8 – Issue with batch cancelling

I’m using Laravel 9 with PHP 8.0.

I would like to launch several hundred jobs one after the other.

I initialized my jobs by storing them in “recognitionsBus”. Then ran the batch.

I tried to reduce the job to the minimum to just log informations, but each time a job is launched, it passes in the condition which says that the batch is cancelled

Also, not all jobs are executed. After a certain number, the batch stops. It may be an issue on my side but the batch doesn’t pass through the then/catch or finally. Do you know why ?

Thank you in advance.

Controller

    foreach ($recognitions as $recognition) {
    $test = Test::create([...]);

    $recognitionsBus->push(new TestJob($test);
    }

    Bus::batch($recognitionsBus->toArray())
    ->then(function (Batch $batch) use ($test) {
        Log::info("success");
    })->catch(function (Batch $batch, Throwable $e) use($test) {
        Log::error("error");

        throw $e;
    })->finally(function (Batch $batch) use ($test) {
        Log::info("finish");
    })
    ->dispatch();

Job

    <?php

    namespace AppJobs;

    use IlluminateBusBatchable;
    use IlluminateBusQueueable;
    use IlluminateContractsQueueShouldQueue;
    use IlluminateFoundationBusDispatchable;
    use IlluminateQueueInteractsWithQueue;
    use IlluminateQueueSerializesModels;
    use IlluminateSupportFacadesLog;
    use Throwable;

    class TestJob implements ShouldQueue
    {
    use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $test;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($test)
    {
        $this->test = $test;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        if ($this->batch()->cancelled()) {
            Log::info('canceled');

            return;
        }

        Log::info($this->test->id);
    }

    /**
     * Handle a job failure.
     */
    public function failed(Throwable $exception)
    {
        Log::info($exception->getMessage());
    }
    }

Command

php artisan queue:work