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

[10.x] Add resume method to batched jobs as an option #49198

Closed
wants to merge 4 commits into from
Closed
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
10 changes: 10 additions & 0 deletions src/Illuminate/Bus/Batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
8 changes: 8 additions & 0 deletions src/Illuminate/Bus/BatchRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
14 changes: 14 additions & 0 deletions src/Illuminate/Bus/DatabaseBatchRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
9 changes: 8 additions & 1 deletion src/Illuminate/Queue/Console/RetryBatchCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down
13 changes: 13 additions & 0 deletions src/Illuminate/Support/Testing/Fakes/BatchRepositoryFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
17 changes: 17 additions & 0 deletions tests/Bus/BusBatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down