@frankclark were you able to overcome this? For me, it seems as the finally block sometimes gets called with failures, sometimes not, still trying to figure it out
Jul 30, 2021
8
Level 2
Laravel Batch Jobs - Call Finally even if error
Hello there,
I am using batches to run a series of jobs. I thought logically, if you use allowFailures() that the finally() callback would still get called
$myThingsBatches = $myThings->map(static function ($item) {
return [
new createPartOne($item->id),
new createPartTwo($item->id),
];
});
Bus::batch($myThingsBatches)->then(function (Batch $batch) use ($parentThing) {
Log::info("Inside 'then'");
})->catch(function (Batch $batch, Throwable $exception) {
Log::info("Inside catch : " . $exception->getMessage());
})->finally(function (Batch $batch) use ($parentThing) {
Log::info("Inside finally");
CreateMainThing::dispatch($parentThing->id);
})->allowFailures()->dispatch();
So, if one of the batches in $myThingsBatches times out, the finally callback never happens. My desired outcome is that the finally callback gets run no matter what.
As documented the catch-> only gets called for the first failure in the batch, so that isn't suitable either as i want to wait for all of the jobs to have either finished or failed before what happens in finally is executed.
Any ideas ?
Please or to participate in this conversation.