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..0bd0b3456b05 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]."); + $batch->resume(); + } + $this->components->info("Pushing failed queue jobs of the batch [$id] back onto the queue."); foreach ($batch->failedJobIds as $failedJobId) { 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. * 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);