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] Fixes retrying failed jobs causes PHP memory exhaustion errors when dealing with thousands of failed jobs #49186

Merged
merged 3 commits into from
Nov 30, 2023
Merged
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
18 changes: 13 additions & 5 deletions src/Illuminate/Queue/Console/RetryCommand.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's necessary this queue column is indexed @crynobone @taylorotwell

->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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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