diff --git a/src/Illuminate/Bus/ChainedBatch.php b/src/Illuminate/Bus/ChainedBatch.php index 7dc1a39ecaab..19e77017a19e 100644 --- a/src/Illuminate/Bus/ChainedBatch.php +++ b/src/Illuminate/Bus/ChainedBatch.php @@ -130,9 +130,11 @@ protected function attachRemainderOfChainToEndOfBatch(PendingBatch $batch) $next->chainQueue = $this->chainQueue; $next->chainCatchCallbacks = $this->chainCatchCallbacks; + $next = serialize($next); + $batch->finally(function (Batch $batch) use ($next) { if (! $batch->cancelled()) { - Container::getInstance()->make(Dispatcher::class)->dispatch($next); + Container::getInstance()->make(Dispatcher::class)->dispatch(unserialize($next)); } }); diff --git a/tests/Integration/Queue/JobChainingTest.php b/tests/Integration/Queue/JobChainingTest.php index 8cf10be0e966..6d142d7fc99a 100644 --- a/tests/Integration/Queue/JobChainingTest.php +++ b/tests/Integration/Queue/JobChainingTest.php @@ -470,6 +470,24 @@ public function testBatchCanBeAddedToChain() $this->assertEquals(['c1', 'c2', 'b1', 'b2', 'b3', 'b4', 'c3'], JobRunRecorder::$results); } + public function testClosureAfterBatchInChainIsDispatched() + { + Bus::chain([ + new JobChainingNamedTestJob('c1'), + Bus::batch([ + new JobChainingTestBatchedJob('b1'), + new JobChainingTestBatchedJob('b2'), + ]), + function () { + JobRunRecorder::record('closure-after-batch'); + }, + ])->dispatch(); + + $this->runQueueWorkerCommand(['--stop-when-empty' => true]); + + $this->assertContains('closure-after-batch', JobRunRecorder::$results); + } + public function testBatchInChainUsesCorrectQueue() { $otherQueue = $this->getQueueDriver() === 'redis' ? '{other}' : 'other';