Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading