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 information, but each time a job is launched, it passes in the condition which says that the batch is canceled.
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?
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
namespace App\Jobs;
use Illuminate\Bus\Batchable;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
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