Skip to content

Commit

Permalink
[9.x] Add methods to append and prepend jobs to existing chain (#42138)
Browse files Browse the repository at this point in the history
* Add methods to append and prepend jobs to existing chain

* Style fixes
  • Loading branch information
janpantel authored Apr 26, 2022
1 parent fc740b8 commit 7ac98bb
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
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();
}
}

0 comments on commit 7ac98bb

Please sign in to comment.