diff --git a/src/Illuminate/Bus/Batch.php b/src/Illuminate/Bus/Batch.php index bcfb898cb940..c9b8d376d71e 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,30 @@ public function add($jobs) return $job; }); - $this->repository->transaction(function () use ($jobs, $count) { + $queueConnection = $this->queue->connection($this->options['connection'] ?? null); + + if ($queueConnection instanceof SyncQueue) { $this->repository->incrementTotalJobs($this->id, $count); - $this->queue->connection($this->options['connection'] ?? null)->bulk( - $jobs->all(), - $data = '', - $this->options['queue'] ?? null - ); - }); + try { + $queueConnection->bulk( + $jobs->all(), + $data = '', + $this->options['queue'] ?? null + ); + } catch (Throwable $e) { + } + } else { + $this->repository->transaction(function () use ($jobs, $count, $queueConnection) { + $this->repository->incrementTotalJobs($this->id, $count); + + $queueConnection->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([])