Skip to content

Commit 7ccb4dc

Browse files
[10.x] Fix append and prepend batch to chain (#53455)
* Fix append and prepend batch to chain * formatting, add comma * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 646520a commit 7ccb4dc

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

src/Illuminate/Bus/Queueable.php

+10
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,11 @@ public function chain($chain)
199199
*/
200200
public function prependToChain($job)
201201
{
202+
$job = match (true) {
203+
$job instanceof PendingBatch => new ChainedBatch($job),
204+
default => $job,
205+
};
206+
202207
$this->chained = Arr::prepend($this->chained, $this->serializeJob($job));
203208

204209
return $this;
@@ -212,6 +217,11 @@ public function prependToChain($job)
212217
*/
213218
public function appendToChain($job)
214219
{
220+
$job = match (true) {
221+
$job instanceof PendingBatch => new ChainedBatch($job),
222+
default => $job,
223+
};
224+
215225
$this->chained = array_merge($this->chained, [$this->serializeJob($job)]);
216226

217227
return $this;

tests/Integration/Queue/JobChainingTest.php

+68
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,30 @@ public function testChainJobsCanBeAppended()
288288
$this->assertTrue(JobChainAddingAddedJob::$ranAt->isAfter(JobChainAddingExistingJob::$ranAt));
289289
}
290290

291+
public function testChainJobsCanBePrependedBatch()
292+
{
293+
Bus::chain([
294+
new JobChainAddingPrependedBatch('j1'),
295+
new JobChainingNamedTestJob('j2'),
296+
])->dispatch();
297+
298+
$this->runQueueWorkerCommand(['--stop-when-empty' => true]);
299+
300+
$this->assertEquals(['j1', 'b1', 'b2', 'j2'], JobRunRecorder::$results);
301+
}
302+
303+
public function testChainJobsCanBeAppendedBatch()
304+
{
305+
Bus::chain([
306+
new JobChainAddingAppendingBatch('j1'),
307+
new JobChainingNamedTestJob('j2'),
308+
])->dispatch();
309+
310+
$this->runQueueWorkerCommand(['--stop-when-empty' => true]);
311+
312+
$this->assertEquals(['j1', 'j2', 'b1', 'b2'], JobRunRecorder::$results);
313+
}
314+
291315
public function testChainJobsCanBeAppendedWithoutExistingChain()
292316
{
293317
JobChainAddingAppendingJob::dispatch();
@@ -652,6 +676,50 @@ public function handle()
652676
}
653677
}
654678

679+
class JobChainAddingAppendingBatch implements ShouldQueue
680+
{
681+
use Dispatchable, InteractsWithQueue, Queueable;
682+
683+
public string $id;
684+
685+
public function __construct(string $id)
686+
{
687+
$this->id = $id;
688+
}
689+
690+
public function handle()
691+
{
692+
$this->appendToChain(Bus::batch([
693+
new JobChainingNamedTestJob('b1'),
694+
new JobChainingNamedTestJob('b2'),
695+
]));
696+
697+
JobRunRecorder::record($this->id);
698+
}
699+
}
700+
701+
class JobChainAddingPrependedBatch implements ShouldQueue
702+
{
703+
use Dispatchable, InteractsWithQueue, Queueable;
704+
705+
public string $id;
706+
707+
public function __construct(string $id)
708+
{
709+
$this->id = $id;
710+
}
711+
712+
public function handle()
713+
{
714+
$this->prependToChain(Bus::batch([
715+
new JobChainingNamedTestJob('b1'),
716+
new JobChainingNamedTestJob('b2'),
717+
]));
718+
719+
JobRunRecorder::record($this->id);
720+
}
721+
}
722+
655723
class JobChainAddingExistingJob implements ShouldQueue
656724
{
657725
use Dispatchable, InteractsWithQueue, Queueable;

0 commit comments

Comments
 (0)