From a4065e1c0845491f14435a8f3b4f3a6b267177fe Mon Sep 17 00:00:00 2001 From: bmckay959 Date: Thu, 30 Nov 2023 09:55:49 -0600 Subject: [PATCH 1/4] Initial commit - Add resume method to batched jobs as an option --- src/Illuminate/Bus/Batch.php | 10 ++++++++++ src/Illuminate/Bus/BatchRepository.php | 8 ++++++++ src/Illuminate/Bus/DatabaseBatchRepository.php | 14 ++++++++++++++ src/Illuminate/Queue/Console/RetryBatchCommand.php | 9 ++++++++- 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Bus/Batch.php b/src/Illuminate/Bus/Batch.php index 644bfbce9dc6..1f423eaec998 100644 --- a/src/Illuminate/Bus/Batch.php +++ b/src/Illuminate/Bus/Batch.php @@ -386,6 +386,16 @@ public function cancel() $this->repository->cancel($this->id); } + /** + * Resume the batch. + * + * @return void + */ + public function resume() + { + $this->repository->resume($this->id); + } + /** * Determine if the batch has been cancelled. * diff --git a/src/Illuminate/Bus/BatchRepository.php b/src/Illuminate/Bus/BatchRepository.php index 0e580ca4dcd9..623bc7a74347 100644 --- a/src/Illuminate/Bus/BatchRepository.php +++ b/src/Illuminate/Bus/BatchRepository.php @@ -77,6 +77,14 @@ public function markAsFinished(string $batchId); */ public function cancel(string $batchId); + /** + * Resume the batch that has the given ID. + * + * @param string $batchId + * @return void + */ + public function resume(string $batchId); + /** * Delete the batch that has the given ID. * diff --git a/src/Illuminate/Bus/DatabaseBatchRepository.php b/src/Illuminate/Bus/DatabaseBatchRepository.php index 4333c515ac79..1f497e9d8e75 100644 --- a/src/Illuminate/Bus/DatabaseBatchRepository.php +++ b/src/Illuminate/Bus/DatabaseBatchRepository.php @@ -221,6 +221,20 @@ public function cancel(string $batchId) ]); } + /** + * Resume the batch that has the given ID. + * + * @param string $batchId + * @return void + */ + public function resume(string $batchId) + { + $this->connection->table($this->table)->where('id', $batchId)->update([ + 'cancelled_at' => null, + 'finished_at' => null, + ]); + } + /** * Delete the batch that has the given ID. * diff --git a/src/Illuminate/Queue/Console/RetryBatchCommand.php b/src/Illuminate/Queue/Console/RetryBatchCommand.php index f4af6c3a47bd..7a823fe0ca45 100644 --- a/src/Illuminate/Queue/Console/RetryBatchCommand.php +++ b/src/Illuminate/Queue/Console/RetryBatchCommand.php @@ -15,7 +15,9 @@ class RetryBatchCommand extends Command implements Isolatable * * @var string */ - protected $signature = 'queue:retry-batch {id : The ID of the batch whose failed jobs should be retried}'; + protected $signature = 'queue:retry-batch + {id : The ID of the batch whose failed jobs should be retried} + {--resume : If the batch was previously cancelled, this will un-cancel the batch}'; /** * The console command description. @@ -43,6 +45,11 @@ public function handle() return 1; } + if ($this->option('resume')) { + $this->components->info("Resuming job batch [$id]."); + + } + $this->components->info("Pushing failed queue jobs of the batch [$id] back onto the queue."); foreach ($batch->failedJobIds as $failedJobId) { From c6f973abfb9b7f99fc8a3123f72208417c773694 Mon Sep 17 00:00:00 2001 From: bmckay959 Date: Thu, 30 Nov 2023 09:59:42 -0600 Subject: [PATCH 2/4] add resume to command --- src/Illuminate/Queue/Console/RetryBatchCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Queue/Console/RetryBatchCommand.php b/src/Illuminate/Queue/Console/RetryBatchCommand.php index 7a823fe0ca45..0bd0b3456b05 100644 --- a/src/Illuminate/Queue/Console/RetryBatchCommand.php +++ b/src/Illuminate/Queue/Console/RetryBatchCommand.php @@ -47,7 +47,7 @@ public function handle() if ($this->option('resume')) { $this->components->info("Resuming job batch [$id]."); - + $batch->resume(); } $this->components->info("Pushing failed queue jobs of the batch [$id] back onto the queue."); From f785ef1e57c0721fef36e1970497a6b1590c0a58 Mon Sep 17 00:00:00 2001 From: bmckay959 Date: Thu, 30 Nov 2023 10:08:31 -0600 Subject: [PATCH 3/4] added test for resume --- tests/Bus/BusBatchTest.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/Bus/BusBatchTest.php b/tests/Bus/BusBatchTest.php index 49c394611185..6b601bb69ad3 100644 --- a/tests/Bus/BusBatchTest.php +++ b/tests/Bus/BusBatchTest.php @@ -304,6 +304,23 @@ public function test_batch_can_be_cancelled() $this->assertTrue($batch->cancelled()); } + public function test_batch_can_be_resumed() + { + $queue = m::mock(Factory::class); + + $batch = $this->createTestBatch($queue); + + $batch->cancel(); + + $batch = $batch->fresh(); + + $batch->resume(); + + $batch = $batch->fresh(); + + $this->assertFalse($batch->cancelled()); + } + public function test_batch_can_be_deleted() { $queue = m::mock(Factory::class); From 232c68fd12c1f10d36982b41b855fd73d69f95a3 Mon Sep 17 00:00:00 2001 From: bmckay959 Date: Thu, 30 Nov 2023 10:12:23 -0600 Subject: [PATCH 4/4] add resume to BatchRepositoryFake --- .../Support/Testing/Fakes/BatchRepositoryFake.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Illuminate/Support/Testing/Fakes/BatchRepositoryFake.php b/src/Illuminate/Support/Testing/Fakes/BatchRepositoryFake.php index 021c73f27163..5c5c68fe5427 100644 --- a/src/Illuminate/Support/Testing/Fakes/BatchRepositoryFake.php +++ b/src/Illuminate/Support/Testing/Fakes/BatchRepositoryFake.php @@ -129,6 +129,19 @@ public function cancel(string $batchId) } } + /** + * Resume the batch that has the given ID. + * + * @param string $batchId + * @return void + */ + public function resume(string $batchId) + { + if (isset($this->batches[$batchId])) { + $this->batches[$batchId]->resume(); + } + } + /** * Delete the batch that has the given ID. *