-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathRequeueCommand.php
More file actions
163 lines (134 loc) · 4.79 KB
/
RequeueCommand.php
File metadata and controls
163 lines (134 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org/)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org/)
* @link https://cakephp.org CakePHP(tm) Project
* @since 0.1.0
* @license https://opensource.org/licenses/MIT MIT License
*/
namespace Cake\Queue\Command;
use Cake\Command\Command;
use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
use Cake\Console\ConsoleOptionParser;
use Cake\ORM\Locator\LocatorAwareTrait;
use Cake\Queue\QueueManager;
use Exception;
class RequeueCommand extends Command
{
use LocatorAwareTrait;
/**
* Get the command name.
*
* @return string
*/
public static function defaultName(): string
{
return 'queue requeue';
}
/**
* Gets the option parser instance and configures it.
*
* @return \Cake\Console\ConsoleOptionParser
*/
public function getOptionParser(): ConsoleOptionParser
{
$parser = parent::getOptionParser();
$parser->setDescription('Requeue failed jobs.');
$parser->addArgument('ids', [
'required' => false,
'help' => 'Requeue jobs by the FailedJob ID (comma-separated).',
]);
$parser->addOption('class', [
'help' => 'Requeue jobs by the job class.',
]);
$parser->addOption('queue', [
'help' => 'Requeue jobs by the queue the job was received on.',
]);
$parser->addOption('config', [
'help' => 'Requeue jobs by the config used to queue the job.',
]);
$parser->addOption('force', [
'help' => 'Automatically assume yes in response to confirmation prompt.',
'short' => 'f',
'boolean' => true,
]);
return $parser;
}
/**
* @param \Cake\Console\Arguments $args Arguments
* @param \Cake\Console\ConsoleIo $io ConsoleIo
* @return void
*/
public function execute(Arguments $args, ConsoleIo $io): void
{
/** @var \Cake\Queue\Model\Table\FailedJobsTable $failedJobsTable */
$failedJobsTable = $this->getTableLocator()->get('Cake/Queue.FailedJobs');
$jobsToRequeueQuery = $failedJobsTable->find();
if ($args->hasArgument('ids')) {
$idsArg = $args->getArgument('ids');
if ($idsArg !== null) {
$ids = explode(',', $idsArg);
$jobsToRequeueQuery->whereInList('id', $ids);
}
}
if ($args->hasOption('class')) {
$jobsToRequeueQuery->where(['class' => $args->getOption('class')]);
}
if ($args->hasOption('queue')) {
$jobsToRequeueQuery->where(['queue' => $args->getOption('queue')]);
}
if ($args->hasOption('config')) {
$jobsToRequeueQuery->where(['config' => $args->getOption('config')]);
}
$requeueingCount = $jobsToRequeueQuery->count();
if (!$requeueingCount) {
$io->out('0 jobs found.');
return;
}
if (!$args->getOption('force')) {
$confirmed = $io->askChoice("Requeue {$requeueingCount} jobs?", ['y', 'n'], 'n');
if ($confirmed !== 'y') {
return;
}
}
$io->out("Requeueing {$requeueingCount} jobs.");
$succeededCount = 0;
$failedCount = 0;
/** @var array<\Cake\Queue\Model\Entity\FailedJob> $jobsToRequeue */
$jobsToRequeue = $jobsToRequeueQuery->all();
foreach ($jobsToRequeue as $failedJob) {
$io->verbose("Requeueing FailedJob with ID {$failedJob->id}.");
try {
QueueManager::push(
[$failedJob->class, $failedJob->method],
$failedJob->decoded_data,
[
'config' => $failedJob->config,
'priority' => $failedJob->priority,
'queue' => $failedJob->queue,
],
);
$failedJobsTable->deleteOrFail($failedJob);
$succeededCount++;
} catch (Exception $e) {
$io->err("Exception occurred while requeueing FailedJob with ID {$failedJob->id}");
$io->err((string)$e);
$failedCount++;
}
}
if ($failedCount) {
$io->err("Failed to requeue {$failedCount} jobs.");
}
if ($succeededCount) {
$io->success("{$succeededCount} jobs requeued.");
}
}
}