Skip to content

Commit

Permalink
[10.x] Fixes retrying failed jobs causes PHP memory exhaustion errors…
Browse files Browse the repository at this point in the history
… when dealing with thousands of failed jobs (#49186)

* [10.x] Fixes retrying failed jobs causes PHP memory exhaustion errors when dealing with thousands of failed jobs

fixes #49185

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* Apply fixes from StyleCI

* formatting

---------

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
Co-authored-by: StyleCI Bot <bot@styleci.io>
Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
3 people committed Nov 30, 2023
1 parent e4ffa95 commit 2b9d596
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/Illuminate/Queue/Console/RetryCommand.php
Expand Up @@ -70,7 +70,11 @@ protected function getJobIds()
$ids = (array) $this->argument('id');

if (count($ids) === 1 && $ids[0] === 'all') {
return Arr::pluck($this->laravel['queue.failer']->all(), 'id');
$failer = $this->laravel['queue.failer'];

return method_exists($failer, 'ids')
? $failer->ids()
: Arr::pluck($failer->all(), 'id');
}

if ($queue = $this->option('queue')) {
Expand All @@ -92,10 +96,14 @@ protected function getJobIds()
*/
protected function getJobIdsByQueue($queue)
{
$ids = collect($this->laravel['queue.failer']->all())
->where('queue', $queue)
->pluck('id')
->toArray();
$failer = $this->laravel['queue.failer'];

$ids = method_exists($failer, 'ids')
? $failer->ids($queue)
: collect($failer->all())
->where('queue', $queue)
->pluck('id')
->toArray();

if (count($ids) === 0) {
$this->components->error("Unable to find failed jobs for queue [{$queue}].");
Expand Down
15 changes: 15 additions & 0 deletions src/Illuminate/Queue/Failed/DatabaseFailedJobProvider.php
Expand Up @@ -64,6 +64,21 @@ public function log($connection, $queue, $payload, $exception)
));
}

/**
* Get the IDs of all of the failed jobs.
*
* @param string|null $queue
* @return array
*/
public function ids($queue = null)
{
return $this->getTable()
->when(! is_null($queue), fn ($query) => $query->where('queue', $queue))
->orderBy('id', 'desc')
->pluck('id')
->all();
}

/**
* Get a list of all of the failed jobs.
*
Expand Down
15 changes: 15 additions & 0 deletions src/Illuminate/Queue/Failed/DatabaseUuidFailedJobProvider.php
Expand Up @@ -67,6 +67,21 @@ public function log($connection, $queue, $payload, $exception)
return $uuid;
}

/**
* Get the IDs of all of the failed jobs.
*
* @param string|null $queue
* @return array
*/
public function ids($queue = null)
{
return $this->getTable()
->when(! is_null($queue), fn ($query) => $query->where('queue', $queue))
->orderBy('id', 'desc')
->pluck('uuid')
->all();
}

/**
* Get a list of all of the failed jobs.
*
Expand Down
14 changes: 14 additions & 0 deletions src/Illuminate/Queue/Failed/DynamoDbFailedJobProvider.php
Expand Up @@ -78,6 +78,20 @@ public function log($connection, $queue, $payload, $exception)
return $id;
}

/**
* Get the IDs of all of the failed jobs.
*
* @param string|null $queue
* @return array
*/
public function ids($queue = null)
{
return collect($this->all())
->when(! is_null($queue), fn ($collect) => $collect->where('queue', $queue))
->pluck('id')
->all();
}

/**
* Get a list of all of the failed jobs.
*
Expand Down
3 changes: 3 additions & 0 deletions src/Illuminate/Queue/Failed/FailedJobProviderInterface.php
Expand Up @@ -2,6 +2,9 @@

namespace Illuminate\Queue\Failed;

/**
* @method array ids(string $queue = null)
*/
interface FailedJobProviderInterface
{
/**
Expand Down
14 changes: 14 additions & 0 deletions src/Illuminate/Queue/Failed/FileFailedJobProvider.php
Expand Up @@ -78,6 +78,20 @@ public function log($connection, $queue, $payload, $exception)
});
}

/**
* Get the IDs of all of the failed jobs.
*
* @param string|null $queue
* @return array
*/
public function ids($queue = null)
{
return collect($this->all())
->when(! is_null($queue), fn ($collect) => $collect->where('queue', $queue))
->pluck('id')
->all();
}

/**
* Get a list of all of the failed jobs.
*
Expand Down
11 changes: 11 additions & 0 deletions src/Illuminate/Queue/Failed/NullFailedJobProvider.php
Expand Up @@ -18,6 +18,17 @@ public function log($connection, $queue, $payload, $exception)
//
}

/**
* Get the IDs of all of the failed jobs.
*
* @param string|null $queue
* @return array
*/
public function ids($queue = null)
{
return [];
}

/**
* Get a list of all of the failed jobs.
*
Expand Down

0 comments on commit 2b9d596

Please sign in to comment.