Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Add methods to append and prepend jobs to existing chain #42138

Merged
merged 2 commits into from
Apr 26, 2022
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
26 changes: 26 additions & 0 deletions src/Illuminate/Bus/Queueable.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,32 @@ public function chain($chain)
return $this;
}

/**
* Prepend a job to the current chain so that it is run after the currently running job.
*
* @param mixed $job
* @return $this
*/
public function prependToChain($job)
{
$this->chained = Arr::prepend($this->chained, $this->serializeJob($job));

return $this;
}

/**
* Append a job to the end of the current chain.
*
* @param mixed $job
* @return $this
*/
public function appendToChain($job)
{
$this->chained = array_merge($this->chained, [$this->serializeJob($job)]);

return $this;
}

/**
* Serialize a job for queuing.
*
Expand Down
79 changes: 79 additions & 0 deletions tests/Integration/Queue/JobChainingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Queue;
use Orchestra\Testbench\TestCase;
Expand Down Expand Up @@ -211,6 +212,38 @@ public function testChainJobsUseDefaultConfig()
$this->assertNull(JobChainingTestThirdJob::$usedQueue);
$this->assertNull(JobChainingTestThirdJob::$usedConnection);
}

public function testChainJobsCanBePrepended()
{
JobChainAddingPrependingJob::withChain([new JobChainAddingExistingJob])->dispatch();

$this->assertNotNull(JobChainAddingAddedJob::$ranAt);
$this->assertNotNull(JobChainAddingExistingJob::$ranAt);
$this->assertTrue(JobChainAddingAddedJob::$ranAt->isBefore(JobChainAddingExistingJob::$ranAt));
}

public function testChainJobsCanBePrependedWithoutExistingChain()
{
JobChainAddingPrependingJob::dispatch();

$this->assertNotNull(JobChainAddingAddedJob::$ranAt);
}

public function testChainJobsCanBeAppended()
{
JobChainAddingAppendingJob::withChain([new JobChainAddingExistingJob])->dispatch();

$this->assertNotNull(JobChainAddingAddedJob::$ranAt);
$this->assertNotNull(JobChainAddingExistingJob::$ranAt);
$this->assertTrue(JobChainAddingAddedJob::$ranAt->isAfter(JobChainAddingExistingJob::$ranAt));
}

public function testChainJobsCanBeAppendedWithoutExistingChain()
{
JobChainAddingAppendingJob::dispatch();

$this->assertNotNull(JobChainAddingAddedJob::$ranAt);
}
}

class JobChainingTestFirstJob implements ShouldQueue
Expand Down Expand Up @@ -293,3 +326,49 @@ public function handle()
$this->fail();
}
}

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

public function handle()
{
$this->prependToChain(new JobChainAddingAddedJob);
}
}

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

public function handle()
{
$this->appendToChain(new JobChainAddingAddedJob);
}
}

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

/** @var Carbon|null */
public static $ranAt = null;

public function handle()
{
static::$ranAt = now();
}
}

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

/** @var Carbon|null */
public static $ranAt = null;

public function handle()
{
static::$ranAt = now();
}
}