Skip to content

Commit

Permalink
[10.x] Fix append and prepend batch to chain (#53455)
Browse files Browse the repository at this point in the history
* Fix append and prepend batch to chain

* formatting, add comma

* formatting

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
Bencute and taylorotwell authored Nov 11, 2024
1 parent 646520a commit 7ccb4dc
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Illuminate/Bus/Queueable.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ public function chain($chain)
*/
public function prependToChain($job)
{
$job = match (true) {
$job instanceof PendingBatch => new ChainedBatch($job),
default => $job,
};

$this->chained = Arr::prepend($this->chained, $this->serializeJob($job));

return $this;
Expand All @@ -212,6 +217,11 @@ public function prependToChain($job)
*/
public function appendToChain($job)
{
$job = match (true) {
$job instanceof PendingBatch => new ChainedBatch($job),
default => $job,
};

$this->chained = array_merge($this->chained, [$this->serializeJob($job)]);

return $this;
Expand Down
68 changes: 68 additions & 0 deletions tests/Integration/Queue/JobChainingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,30 @@ public function testChainJobsCanBeAppended()
$this->assertTrue(JobChainAddingAddedJob::$ranAt->isAfter(JobChainAddingExistingJob::$ranAt));
}

public function testChainJobsCanBePrependedBatch()
{
Bus::chain([
new JobChainAddingPrependedBatch('j1'),
new JobChainingNamedTestJob('j2'),
])->dispatch();

$this->runQueueWorkerCommand(['--stop-when-empty' => true]);

$this->assertEquals(['j1', 'b1', 'b2', 'j2'], JobRunRecorder::$results);
}

public function testChainJobsCanBeAppendedBatch()
{
Bus::chain([
new JobChainAddingAppendingBatch('j1'),
new JobChainingNamedTestJob('j2'),
])->dispatch();

$this->runQueueWorkerCommand(['--stop-when-empty' => true]);

$this->assertEquals(['j1', 'j2', 'b1', 'b2'], JobRunRecorder::$results);
}

public function testChainJobsCanBeAppendedWithoutExistingChain()
{
JobChainAddingAppendingJob::dispatch();
Expand Down Expand Up @@ -652,6 +676,50 @@ public function handle()
}
}

class JobChainAddingAppendingBatch implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable;

public string $id;

public function __construct(string $id)
{
$this->id = $id;
}

public function handle()
{
$this->appendToChain(Bus::batch([
new JobChainingNamedTestJob('b1'),
new JobChainingNamedTestJob('b2'),
]));

JobRunRecorder::record($this->id);
}
}

class JobChainAddingPrependedBatch implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable;

public string $id;

public function __construct(string $id)
{
$this->id = $id;
}

public function handle()
{
$this->prependToChain(Bus::batch([
new JobChainingNamedTestJob('b1'),
new JobChainingNamedTestJob('b2'),
]));

JobRunRecorder::record($this->id);
}
}

class JobChainAddingExistingJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable;
Expand Down

0 comments on commit 7ccb4dc

Please sign in to comment.