Skip to content

Commit 2b9d596

Browse files
crynoboneStyleCIBottaylorotwell
authored
[10.x] Fixes retrying failed jobs causes PHP memory exhaustion errors 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 <[email protected]> * Apply fixes from StyleCI * formatting --------- Signed-off-by: Mior Muhammad Zaki <[email protected]> Co-authored-by: StyleCI Bot <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
1 parent e4ffa95 commit 2b9d596

7 files changed

+85
-5
lines changed

src/Illuminate/Queue/Console/RetryCommand.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ protected function getJobIds()
7070
$ids = (array) $this->argument('id');
7171

7272
if (count($ids) === 1 && $ids[0] === 'all') {
73-
return Arr::pluck($this->laravel['queue.failer']->all(), 'id');
73+
$failer = $this->laravel['queue.failer'];
74+
75+
return method_exists($failer, 'ids')
76+
? $failer->ids()
77+
: Arr::pluck($failer->all(), 'id');
7478
}
7579

7680
if ($queue = $this->option('queue')) {
@@ -92,10 +96,14 @@ protected function getJobIds()
9296
*/
9397
protected function getJobIdsByQueue($queue)
9498
{
95-
$ids = collect($this->laravel['queue.failer']->all())
96-
->where('queue', $queue)
97-
->pluck('id')
98-
->toArray();
99+
$failer = $this->laravel['queue.failer'];
100+
101+
$ids = method_exists($failer, 'ids')
102+
? $failer->ids($queue)
103+
: collect($failer->all())
104+
->where('queue', $queue)
105+
->pluck('id')
106+
->toArray();
99107

100108
if (count($ids) === 0) {
101109
$this->components->error("Unable to find failed jobs for queue [{$queue}].");

src/Illuminate/Queue/Failed/DatabaseFailedJobProvider.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,21 @@ public function log($connection, $queue, $payload, $exception)
6464
));
6565
}
6666

67+
/**
68+
* Get the IDs of all of the failed jobs.
69+
*
70+
* @param string|null $queue
71+
* @return array
72+
*/
73+
public function ids($queue = null)
74+
{
75+
return $this->getTable()
76+
->when(! is_null($queue), fn ($query) => $query->where('queue', $queue))
77+
->orderBy('id', 'desc')
78+
->pluck('id')
79+
->all();
80+
}
81+
6782
/**
6883
* Get a list of all of the failed jobs.
6984
*

src/Illuminate/Queue/Failed/DatabaseUuidFailedJobProvider.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,21 @@ public function log($connection, $queue, $payload, $exception)
6767
return $uuid;
6868
}
6969

70+
/**
71+
* Get the IDs of all of the failed jobs.
72+
*
73+
* @param string|null $queue
74+
* @return array
75+
*/
76+
public function ids($queue = null)
77+
{
78+
return $this->getTable()
79+
->when(! is_null($queue), fn ($query) => $query->where('queue', $queue))
80+
->orderBy('id', 'desc')
81+
->pluck('uuid')
82+
->all();
83+
}
84+
7085
/**
7186
* Get a list of all of the failed jobs.
7287
*

src/Illuminate/Queue/Failed/DynamoDbFailedJobProvider.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,20 @@ public function log($connection, $queue, $payload, $exception)
7878
return $id;
7979
}
8080

81+
/**
82+
* Get the IDs of all of the failed jobs.
83+
*
84+
* @param string|null $queue
85+
* @return array
86+
*/
87+
public function ids($queue = null)
88+
{
89+
return collect($this->all())
90+
->when(! is_null($queue), fn ($collect) => $collect->where('queue', $queue))
91+
->pluck('id')
92+
->all();
93+
}
94+
8195
/**
8296
* Get a list of all of the failed jobs.
8397
*

src/Illuminate/Queue/Failed/FailedJobProviderInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Illuminate\Queue\Failed;
44

5+
/**
6+
* @method array ids(string $queue = null)
7+
*/
58
interface FailedJobProviderInterface
69
{
710
/**

src/Illuminate/Queue/Failed/FileFailedJobProvider.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,20 @@ public function log($connection, $queue, $payload, $exception)
7878
});
7979
}
8080

81+
/**
82+
* Get the IDs of all of the failed jobs.
83+
*
84+
* @param string|null $queue
85+
* @return array
86+
*/
87+
public function ids($queue = null)
88+
{
89+
return collect($this->all())
90+
->when(! is_null($queue), fn ($collect) => $collect->where('queue', $queue))
91+
->pluck('id')
92+
->all();
93+
}
94+
8195
/**
8296
* Get a list of all of the failed jobs.
8397
*

src/Illuminate/Queue/Failed/NullFailedJobProvider.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ public function log($connection, $queue, $payload, $exception)
1818
//
1919
}
2020

21+
/**
22+
* Get the IDs of all of the failed jobs.
23+
*
24+
* @param string|null $queue
25+
* @return array
26+
*/
27+
public function ids($queue = null)
28+
{
29+
return [];
30+
}
31+
2132
/**
2233
* Get a list of all of the failed jobs.
2334
*

0 commit comments

Comments
 (0)