From 2bc8b52e59639e8ba995c606e2eb34561fbe74f5 Mon Sep 17 00:00:00 2001 From: Jan-Oliver Pantel Date: Tue, 26 Apr 2022 13:56:22 +0200 Subject: [PATCH 1/2] Add methods to append and prepend jobs to existing chain --- src/Illuminate/Bus/Queueable.php | 26 +++++++ tests/Integration/Queue/JobChainingTest.php | 79 +++++++++++++++++++++ 2 files changed, 105 insertions(+) diff --git a/src/Illuminate/Bus/Queueable.php b/src/Illuminate/Bus/Queueable.php index bd67513acb95..ac4c597dd312 100644 --- a/src/Illuminate/Bus/Queueable.php +++ b/src/Illuminate/Bus/Queueable.php @@ -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. * diff --git a/tests/Integration/Queue/JobChainingTest.php b/tests/Integration/Queue/JobChainingTest.php index b0ad447768f3..8ba0b7501390 100644 --- a/tests/Integration/Queue/JobChainingTest.php +++ b/tests/Integration/Queue/JobChainingTest.php @@ -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; @@ -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 @@ -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(); + } +} From 38f1ea71d8e8bd4d2ad28921805975e4480ff540 Mon Sep 17 00:00:00 2001 From: Jan-Oliver Pantel Date: Tue, 26 Apr 2022 14:07:49 +0200 Subject: [PATCH 2/2] Style fixes --- src/Illuminate/Bus/Queueable.php | 8 ++++---- tests/Integration/Queue/JobChainingTest.php | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Illuminate/Bus/Queueable.php b/src/Illuminate/Bus/Queueable.php index ac4c597dd312..7bdc01e53f13 100644 --- a/src/Illuminate/Bus/Queueable.php +++ b/src/Illuminate/Bus/Queueable.php @@ -194,8 +194,8 @@ public function chain($chain) /** * Prepend a job to the current chain so that it is run after the currently running job. * - * @param mixed $job - * @return $this + * @param mixed $job + * @return $this */ public function prependToChain($job) { @@ -207,8 +207,8 @@ public function prependToChain($job) /** * Append a job to the end of the current chain. * - * @param mixed $job - * @return $this + * @param mixed $job + * @return $this */ public function appendToChain($job) { diff --git a/tests/Integration/Queue/JobChainingTest.php b/tests/Integration/Queue/JobChainingTest.php index 8ba0b7501390..5b5fb8a1da55 100644 --- a/tests/Integration/Queue/JobChainingTest.php +++ b/tests/Integration/Queue/JobChainingTest.php @@ -351,7 +351,7 @@ class JobChainAddingExistingJob implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable; - /** @var Carbon|null */ + /** @var Carbon|null */ public static $ranAt = null; public function handle() @@ -364,7 +364,7 @@ class JobChainAddingAddedJob implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable; - /** @var Carbon|null */ + /** @var Carbon|null */ public static $ranAt = null; public function handle()