| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\Queue\Console;
|
|
|
4 |
|
|
|
5 |
use DateTimeInterface;
|
|
|
6 |
use Illuminate\Console\Command;
|
|
|
7 |
use Illuminate\Contracts\Encryption\Encrypter;
|
|
|
8 |
use Illuminate\Queue\Events\JobRetryRequested;
|
|
|
9 |
use Illuminate\Support\Arr;
|
|
|
10 |
use RuntimeException;
|
|
|
11 |
use Symfony\Component\Console\Attribute\AsCommand;
|
|
|
12 |
|
|
|
13 |
#[AsCommand(name: 'queue:retry')]
|
|
|
14 |
class RetryCommand extends Command
|
|
|
15 |
{
|
|
|
16 |
/**
|
|
|
17 |
* The console command signature.
|
|
|
18 |
*
|
|
|
19 |
* @var string
|
|
|
20 |
*/
|
|
|
21 |
protected $signature = 'queue:retry
|
|
|
22 |
{id?* : The ID of the failed job or "all" to retry all jobs}
|
|
|
23 |
{--queue= : Retry all of the failed jobs for the specified queue}
|
|
|
24 |
{--range=* : Range of job IDs (numeric) to be retried}';
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* The name of the console command.
|
|
|
28 |
*
|
|
|
29 |
* This name is used to identify the command during lazy loading.
|
|
|
30 |
*
|
|
|
31 |
* @var string|null
|
|
|
32 |
*
|
|
|
33 |
* @deprecated
|
|
|
34 |
*/
|
|
|
35 |
protected static $defaultName = 'queue:retry';
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* The console command description.
|
|
|
39 |
*
|
|
|
40 |
* @var string
|
|
|
41 |
*/
|
|
|
42 |
protected $description = 'Retry a failed queue job';
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Execute the console command.
|
|
|
46 |
*
|
|
|
47 |
* @return void
|
|
|
48 |
*/
|
|
|
49 |
public function handle()
|
|
|
50 |
{
|
|
|
51 |
$jobsFound = count($ids = $this->getJobIds()) > 0;
|
|
|
52 |
|
|
|
53 |
if ($jobsFound) {
|
|
|
54 |
$this->components->info('Pushing failed queue jobs back onto the queue.');
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
foreach ($ids as $id) {
|
|
|
58 |
$job = $this->laravel['queue.failer']->find($id);
|
|
|
59 |
|
|
|
60 |
if (is_null($job)) {
|
|
|
61 |
$this->components->error("Unable to find failed job with ID [{$id}].");
|
|
|
62 |
} else {
|
|
|
63 |
$this->laravel['events']->dispatch(new JobRetryRequested($job));
|
|
|
64 |
|
|
|
65 |
$this->components->task($id, fn () => $this->retryJob($job));
|
|
|
66 |
|
|
|
67 |
$this->laravel['queue.failer']->forget($id);
|
|
|
68 |
}
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
$jobsFound ? $this->newLine() : $this->components->info('No retryable jobs found.');
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
/**
|
|
|
75 |
* Get the job IDs to be retried.
|
|
|
76 |
*
|
|
|
77 |
* @return array
|
|
|
78 |
*/
|
|
|
79 |
protected function getJobIds()
|
|
|
80 |
{
|
|
|
81 |
$ids = (array) $this->argument('id');
|
|
|
82 |
|
|
|
83 |
if (count($ids) === 1 && $ids[0] === 'all') {
|
|
|
84 |
return Arr::pluck($this->laravel['queue.failer']->all(), 'id');
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
if ($queue = $this->option('queue')) {
|
|
|
88 |
return $this->getJobIdsByQueue($queue);
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
if ($ranges = (array) $this->option('range')) {
|
|
|
92 |
$ids = array_merge($ids, $this->getJobIdsByRanges($ranges));
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
return array_values(array_filter(array_unique($ids)));
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
/**
|
|
|
99 |
* Get the job IDs by queue, if applicable.
|
|
|
100 |
*
|
|
|
101 |
* @param string $queue
|
|
|
102 |
* @return array
|
|
|
103 |
*/
|
|
|
104 |
protected function getJobIdsByQueue($queue)
|
|
|
105 |
{
|
|
|
106 |
$ids = collect($this->laravel['queue.failer']->all())
|
|
|
107 |
->where('queue', $queue)
|
|
|
108 |
->pluck('id')
|
|
|
109 |
->toArray();
|
|
|
110 |
|
|
|
111 |
if (count($ids) === 0) {
|
|
|
112 |
$this->components->error("Unable to find failed jobs for queue [{$queue}].");
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
return $ids;
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
/**
|
|
|
119 |
* Get the job IDs ranges, if applicable.
|
|
|
120 |
*
|
|
|
121 |
* @param array $ranges
|
|
|
122 |
* @return array
|
|
|
123 |
*/
|
|
|
124 |
protected function getJobIdsByRanges(array $ranges)
|
|
|
125 |
{
|
|
|
126 |
$ids = [];
|
|
|
127 |
|
|
|
128 |
foreach ($ranges as $range) {
|
|
|
129 |
if (preg_match('/^[0-9]+\-[0-9]+$/', $range)) {
|
|
|
130 |
$ids = array_merge($ids, range(...explode('-', $range)));
|
|
|
131 |
}
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
return $ids;
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
/**
|
|
|
138 |
* Retry the queue job.
|
|
|
139 |
*
|
|
|
140 |
* @param \stdClass $job
|
|
|
141 |
* @return void
|
|
|
142 |
*/
|
|
|
143 |
protected function retryJob($job)
|
|
|
144 |
{
|
|
|
145 |
$this->laravel['queue']->connection($job->connection)->pushRaw(
|
|
|
146 |
$this->refreshRetryUntil($this->resetAttempts($job->payload)), $job->queue
|
|
|
147 |
);
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
/**
|
|
|
151 |
* Reset the payload attempts.
|
|
|
152 |
*
|
|
|
153 |
* Applicable to Redis and other jobs which store attempts in their payload.
|
|
|
154 |
*
|
|
|
155 |
* @param string $payload
|
|
|
156 |
* @return string
|
|
|
157 |
*/
|
|
|
158 |
protected function resetAttempts($payload)
|
|
|
159 |
{
|
|
|
160 |
$payload = json_decode($payload, true);
|
|
|
161 |
|
|
|
162 |
if (isset($payload['attempts'])) {
|
|
|
163 |
$payload['attempts'] = 0;
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
return json_encode($payload);
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
/**
|
|
|
170 |
* Refresh the "retry until" timestamp for the job.
|
|
|
171 |
*
|
|
|
172 |
* @param string $payload
|
|
|
173 |
* @return string
|
|
|
174 |
*
|
|
|
175 |
* @throws \RuntimeException
|
|
|
176 |
*/
|
|
|
177 |
protected function refreshRetryUntil($payload)
|
|
|
178 |
{
|
|
|
179 |
$payload = json_decode($payload, true);
|
|
|
180 |
|
|
|
181 |
if (! isset($payload['data']['command'])) {
|
|
|
182 |
return json_encode($payload);
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
if (str_starts_with($payload['data']['command'], 'O:')) {
|
|
|
186 |
$instance = unserialize($payload['data']['command']);
|
|
|
187 |
} elseif ($this->laravel->bound(Encrypter::class)) {
|
|
|
188 |
$instance = unserialize($this->laravel->make(Encrypter::class)->decrypt($payload['data']['command']));
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
if (! isset($instance)) {
|
|
|
192 |
throw new RuntimeException('Unable to extract job payload.');
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
if (is_object($instance) && ! $instance instanceof \__PHP_Incomplete_Class && method_exists($instance, 'retryUntil')) {
|
|
|
196 |
$retryUntil = $instance->retryUntil();
|
|
|
197 |
|
|
|
198 |
$payload['retryUntil'] = $retryUntil instanceof DateTimeInterface
|
|
|
199 |
? $retryUntil->getTimestamp()
|
|
|
200 |
: $retryUntil;
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
return json_encode($payload);
|
|
|
204 |
}
|
|
|
205 |
}
|