-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathPurgeFailedCommand.php
More file actions
130 lines (105 loc) · 3.73 KB
/
PurgeFailedCommand.php
File metadata and controls
130 lines (105 loc) · 3.73 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
<?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;
class PurgeFailedCommand extends Command
{
use LocatorAwareTrait;
/**
* Get the command name.
*
* @return string
*/
public static function defaultName(): string
{
return 'queue purge_failed';
}
/**
* Gets the option parser instance and configures it.
*
* @return \Cake\Console\ConsoleOptionParser
*/
public function getOptionParser(): ConsoleOptionParser
{
$parser = parent::getOptionParser();
$parser->setDescription('Delete failed jobs.');
$parser->addArgument('ids', [
'required' => false,
'help' => 'Delete jobs by the FailedJob ID (comma-separated).',
]);
$parser->addOption('class', [
'help' => 'Delete jobs by the job class.',
]);
$parser->addOption('queue', [
'help' => 'Delete jobs by the queue the job was received on.',
]);
$parser->addOption('config', [
'help' => 'Delete 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 int
*/
public function execute(Arguments $args, ConsoleIo $io): int
{
/** @var \Cake\Queue\Model\Table\FailedJobsTable $failedJobsTable */
$failedJobsTable = $this->getTableLocator()->get('Cake/Queue.FailedJobs');
$jobsToDelete = $failedJobsTable->find();
if ($args->hasArgument('ids')) {
$idsArg = $args->getArgument('ids');
if ($idsArg !== null) {
$ids = explode(',', $idsArg);
$jobsToDelete->whereInList($failedJobsTable->aliasField('id'), $ids);
}
}
if ($args->hasOption('class')) {
$jobsToDelete->where(['class' => $args->getOption('class')]);
}
if ($args->hasOption('queue')) {
$jobsToDelete->where(['queue' => $args->getOption('queue')]);
}
if ($args->hasOption('config')) {
$jobsToDelete->where(['config' => $args->getOption('config')]);
}
$deletingCount = $jobsToDelete->count();
if (!$deletingCount) {
$io->out('0 jobs found.');
return self::CODE_SUCCESS;
}
if (!$args->getOption('force')) {
$confirmed = $io->askChoice(sprintf('Delete %s jobs?', $deletingCount), ['y', 'n'], 'n');
if ($confirmed !== 'y') {
return self::CODE_SUCCESS;
}
}
$io->out(sprintf('Deleting %s jobs.', $deletingCount));
$failedJobsTable->deleteManyOrFail($jobsToDelete);
$io->success($deletingCount . ' jobs deleted.');
return self::CODE_SUCCESS;
}
}