From 572f3752964dece793eedcc9797e90a2b2144603 Mon Sep 17 00:00:00 2001 From: Neil Carlo Sucuangco Date: Thu, 9 Oct 2025 16:50:38 +0800 Subject: [PATCH 1/3] avoid duplicate chain catch when nested batch fails on sync queue --- src/Illuminate/Bus/Batch.php | 34 ++++++++++++++++----- tests/Integration/Queue/JobChainingTest.php | 18 +++++++++++ 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/Illuminate/Bus/Batch.php b/src/Illuminate/Bus/Batch.php index bcfb898cb940..20dd0cbcd900 100644 --- a/src/Illuminate/Bus/Batch.php +++ b/src/Illuminate/Bus/Batch.php @@ -7,6 +7,7 @@ use Illuminate\Contracts\Queue\Factory as QueueFactory; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Queue\CallQueuedClosure; +use Illuminate\Queue\SyncQueue; use Illuminate\Support\Arr; use Illuminate\Support\Collection; use JsonSerializable; @@ -184,15 +185,32 @@ public function add($jobs) return $job; }); - $this->repository->transaction(function () use ($jobs, $count) { - $this->repository->incrementTotalJobs($this->id, $count); + $queueConnection = $this->queue->connection($this->options['connection'] ?? null); - $this->queue->connection($this->options['connection'] ?? null)->bulk( - $jobs->all(), - $data = '', - $this->options['queue'] ?? null - ); - }); + if ($queueConnection instanceof SyncQueue) { + $this->repository->transaction(function () use ($count) { + $this->repository->incrementTotalJobs($this->id, $count); + }); + + try { + $queueConnection->bulk( + $jobs->all(), + $data = '', + $this->options['queue'] ?? null + ); + } catch (Throwable $e) { + } + } else { + $this->repository->transaction(function () use ($jobs, $count) { + $this->repository->incrementTotalJobs($this->id, $count); + + $this->queue->connection($this->options['connection'] ?? null)->bulk( + $jobs->all(), + $data = '', + $this->options['queue'] ?? null + ); + }); + } return $this->fresh(); } diff --git a/tests/Integration/Queue/JobChainingTest.php b/tests/Integration/Queue/JobChainingTest.php index 0e48619f9d57..9f7265e40463 100644 --- a/tests/Integration/Queue/JobChainingTest.php +++ b/tests/Integration/Queue/JobChainingTest.php @@ -484,6 +484,24 @@ public function testChainBatchFailureNotAllowed() $this->assertEquals(['batch failed', 'chain failed'], JobRunRecorder::$failures); } + public function testChainBatchFailureNotAllowedOnSyncDoesNotDuplicateChainCatch() + { + config(['queue.default' => 'sync']); + + JobRunRecorder::reset(); + + Bus::chain([ + new JobChainingNamedTestJob('c1'), + Bus::batch([ + new JobChainingTestFailingBatchedJob('fb-sync'), + ])->allowFailures(false)->catch(fn () => JobRunRecorder::recordFailure('batch failed')), + new JobChainingNamedTestJob('c2'), + ])->catch(fn () => JobRunRecorder::recordFailure('chain failed'))->dispatch(); + + $this->assertEquals(['batch failed', 'chain failed'], JobRunRecorder::$failures); + $this->assertSame(1, collect(JobRunRecorder::$failures)->filter(fn ($m) => $m === 'chain failed')->count()); + } + public function testChainConditionable() { $chain = Bus::chain([]) From cc28dbedc5fc651ed737e25ef0e57e862777a5de Mon Sep 17 00:00:00 2001 From: Neil Carlo Sucuangco Date: Fri, 10 Oct 2025 00:30:41 +0800 Subject: [PATCH 2/3] Update Batch.php --- src/Illuminate/Bus/Batch.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Bus/Batch.php b/src/Illuminate/Bus/Batch.php index 20dd0cbcd900..282f9d308458 100644 --- a/src/Illuminate/Bus/Batch.php +++ b/src/Illuminate/Bus/Batch.php @@ -201,10 +201,10 @@ public function add($jobs) } catch (Throwable $e) { } } else { - $this->repository->transaction(function () use ($jobs, $count) { + $this->repository->transaction(function () use ($jobs, $count, $queueConnection) { $this->repository->incrementTotalJobs($this->id, $count); - $this->queue->connection($this->options['connection'] ?? null)->bulk( + $queueConnection->bulk( $jobs->all(), $data = '', $this->options['queue'] ?? null From c814c6bec5dd1b0be80f748cb594067425069c01 Mon Sep 17 00:00:00 2001 From: Neil Carlo Sucuangco Date: Sat, 11 Oct 2025 07:24:23 +0800 Subject: [PATCH 3/3] Update Batch.php --- src/Illuminate/Bus/Batch.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Illuminate/Bus/Batch.php b/src/Illuminate/Bus/Batch.php index 282f9d308458..c9b8d376d71e 100644 --- a/src/Illuminate/Bus/Batch.php +++ b/src/Illuminate/Bus/Batch.php @@ -188,9 +188,7 @@ public function add($jobs) $queueConnection = $this->queue->connection($this->options['connection'] ?? null); if ($queueConnection instanceof SyncQueue) { - $this->repository->transaction(function () use ($count) { - $this->repository->incrementTotalJobs($this->id, $count); - }); + $this->repository->incrementTotalJobs($this->id, $count); try { $queueConnection->bulk(