Skip to content

Commit c24122b

Browse files
[12.x] Add ability to prepend() and append() to PendingChain (#56536)
* Allow creating chain without initial value * Add ability to prepend and append to PendingChain * Add tests * Update PendingChain.php --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent e28bab4 commit c24122b

File tree

4 files changed

+104
-3
lines changed

4 files changed

+104
-3
lines changed

src/Illuminate/Bus/Dispatcher.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,10 @@ public function batch($jobs)
161161
/**
162162
* Create a new chain of queueable jobs.
163163
*
164-
* @param \Illuminate\Support\Collection|array $jobs
164+
* @param \Illuminate\Support\Collection|array|null $jobs
165165
* @return \Illuminate\Foundation\Bus\PendingChain
166166
*/
167-
public function chain($jobs)
167+
public function chain($jobs = null)
168168
{
169169
$jobs = Collection::wrap($jobs);
170170
$jobs = ChainedBatch::prepareNestedBatches($jobs);

src/Illuminate/Foundation/Bus/PendingChain.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
namespace Illuminate\Foundation\Bus;
44

55
use Closure;
6+
use Illuminate\Bus\ChainedBatch;
67
use Illuminate\Contracts\Bus\Dispatcher;
78
use Illuminate\Queue\CallQueuedClosure;
9+
use Illuminate\Support\Collection;
810
use Illuminate\Support\Traits\Conditionable;
911
use Laravel\SerializableClosure\SerializableClosure;
1012

@@ -94,6 +96,50 @@ public function onQueue($queue)
9496
return $this;
9597
}
9698

99+
/**
100+
* Prepend a job to the chain.
101+
*
102+
* @param mixed $job
103+
* @return $this
104+
*/
105+
public function prepend($job)
106+
{
107+
$jobs = ChainedBatch::prepareNestedBatches(
108+
Collection::wrap($job)
109+
);
110+
111+
if ($this->job) {
112+
array_unshift($this->chain, $this->job);
113+
}
114+
115+
$this->job = $jobs->shift();
116+
117+
array_unshift($this->chain, ...$jobs->toArray());
118+
119+
return $this;
120+
}
121+
122+
/**
123+
* Append a job to the chain.
124+
*
125+
* @param mixed $job
126+
* @return $this
127+
*/
128+
public function append($job)
129+
{
130+
$jobs = ChainedBatch::prepareNestedBatches(
131+
Collection::wrap($job)
132+
);
133+
134+
if (! $this->job) {
135+
$this->job = $jobs->shift();
136+
}
137+
138+
array_push($this->chain, ...$jobs->toArray());
139+
140+
return $this;
141+
}
142+
97143
/**
98144
* Set the desired delay in seconds for the chain.
99145
*

src/Illuminate/Support/Facades/Bus.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* @method static mixed dispatchNow(mixed $command, mixed $handler = null)
1414
* @method static \Illuminate\Bus\Batch|null findBatch(string $batchId)
1515
* @method static \Illuminate\Bus\PendingBatch batch(\Illuminate\Support\Collection|mixed $jobs)
16-
* @method static \Illuminate\Foundation\Bus\PendingChain chain(\Illuminate\Support\Collection|array $jobs)
16+
* @method static \Illuminate\Foundation\Bus\PendingChain chain(\Illuminate\Support\Collection|array $jobs = null)
1717
* @method static bool hasCommandHandler(mixed $command)
1818
* @method static mixed getCommandHandler(mixed $command)
1919
* @method static mixed dispatchToQueue(mixed $command)

tests/Integration/Queue/JobChainingTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,61 @@ public function testChainJobsCanBeAppendedWithoutExistingChain()
322322
$this->assertNotNull(JobChainAddingAddedJob::$ranAt);
323323
}
324324

325+
public function testChainCanBeAppended()
326+
{
327+
$chain = Bus::chain();
328+
329+
$chain->append($firstJob = new JobChainingNamedTestJob('j1'));
330+
$chain->append($secondJob = new JobChainingNamedTestJob('j2'));
331+
$chain->append($thirdJob = new JobChainingNamedTestJob('j3'));
332+
333+
$this->assertEquals($firstJob, $chain->job);
334+
$this->assertEquals([$secondJob, $thirdJob], $chain->chain);
335+
}
336+
337+
public function testChainCanBeAppendedWithInitialJob()
338+
{
339+
$chain = Bus::chain([
340+
$firstJob = new JobChainingNamedTestJob('j1'),
341+
]);
342+
343+
$chain->append([
344+
$secondJob = new JobChainingNamedTestJob('j2'),
345+
$thirdJob = new JobChainingNamedTestJob('j3'),
346+
]);
347+
348+
$this->assertEquals($firstJob, $chain->job);
349+
$this->assertEquals([$secondJob, $thirdJob], $chain->chain);
350+
}
351+
352+
public function testChainCanBePrepended()
353+
{
354+
$chain = Bus::chain();
355+
356+
$chain->prepend($firstJob = new JobChainingNamedTestJob('j1'));
357+
$chain->prepend($secondJob = new JobChainingNamedTestJob('j2'));
358+
$chain->prepend($thirdJob = new JobChainingNamedTestJob('j3'));
359+
360+
$this->assertEquals($thirdJob, $chain->job);
361+
$this->assertEquals([$secondJob, $firstJob], $chain->chain);
362+
}
363+
364+
public function testChainCanBePrependedWithInitialJob()
365+
{
366+
$chain = Bus::chain([
367+
$firstJob = new JobChainingNamedTestJob('j4'),
368+
]);
369+
370+
$chain->prepend([
371+
$secondJob = new JobChainingNamedTestJob('j1'),
372+
$thirdJob = new JobChainingNamedTestJob('j2'),
373+
$fourthJob = new JobChainingNamedTestJob('j3'),
374+
]);
375+
376+
$this->assertEquals($secondJob, $chain->job);
377+
$this->assertEquals([$thirdJob, $fourthJob, $firstJob], $chain->chain);
378+
}
379+
325380
public function testBatchCanBeAddedToChain()
326381
{
327382
Bus::chain([

0 commit comments

Comments
 (0)