Skip to content

Commit

Permalink
Support multiple batch IDs when retrying failed batch jobs (#52873)
Browse files Browse the repository at this point in the history
  • Loading branch information
skegel13 authored Sep 22, 2024
1 parent 66384be commit c1a5d9c
Showing 1 changed file with 34 additions and 14 deletions.
48 changes: 34 additions & 14 deletions src/Illuminate/Queue/Console/RetryBatchCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ 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}';

/**
* The console command description.
Expand All @@ -31,25 +32,32 @@ class RetryBatchCommand extends Command implements Isolatable
*/
public function handle()
{
$batch = $this->laravel[BatchRepository::class]->find($id = $this->argument('id'));
$batchesFound = count($ids = $this->getBatchJobIds()) > 0;

if (! $batch) {
$this->components->error("Unable to find a batch with ID [{$id}].");
if ($batchesFound) {
$this->components->info('Pushing failed batch jobs back onto the queue.');
}

return 1;
} elseif (empty($batch->failedJobIds)) {
$this->components->error('The given batch does not contain any failed jobs.');
foreach ($ids as $batchId) {
$batch = $this->laravel[BatchRepository::class]->find($batchId);

return 1;
}
if (! $batch) {
$this->components->error("Unable to find a batch with ID [{$batchId}].");
} elseif (empty($batch->failedJobIds)) {
$this->components->error('The given batch does not contain any failed jobs.');
}

$this->components->info("Pushing failed queue jobs of the batch [$id] back onto the queue.");
$this->components->info("Pushing failed queue jobs of the batch [$batchId] back onto the queue.");

foreach ($batch->failedJobIds as $failedJobId) {
$this->components->task($failedJobId, fn () => $this->callSilent('queue:retry', ['id' => $failedJobId]) == 0);
}
foreach ($batch->failedJobIds as $failedJobId) {
$this->components->task(
$failedJobId,
fn () => $this->callSilent('queue:retry', ['id' => $failedJobId]) == 0
);
}

$this->newLine();
$this->newLine();
}
}

/**
Expand All @@ -61,4 +69,16 @@ public function isolatableId()
{
return $this->argument('id');
}

/**
* Get the batch IDs to be retried.
*
* @return array
*/
protected function getBatchJobIds()
{
$ids = (array) $this->argument('id');

return array_values(array_filter(array_unique($ids)));
}
}

0 comments on commit c1a5d9c

Please sign in to comment.