|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +/** |
| 5 | + * CakePHP(tm) : Rapid Development Framework (https://cakephp.org) |
| 6 | + * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org/) |
| 7 | + * |
| 8 | + * Licensed under The MIT License |
| 9 | + * For full copyright and license information, please see the LICENSE.txt |
| 10 | + * Redistributions of files must retain the above copyright notice. |
| 11 | + * |
| 12 | + * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org/) |
| 13 | + * @link https://cakephp.org CakePHP(tm) Project |
| 14 | + * @since 0.1.0 |
| 15 | + * @license https://opensource.org/licenses/MIT MIT License |
| 16 | + */ |
| 17 | +namespace Cake\Queue\Command; |
| 18 | + |
| 19 | +use Cake\Command\Command; |
| 20 | +use Cake\Console\Arguments; |
| 21 | +use Cake\Console\ConsoleIo; |
| 22 | +use Cake\Console\ConsoleOptionParser; |
| 23 | +use Cake\ORM\Locator\LocatorAwareTrait; |
| 24 | + |
| 25 | +class PurgeFailedCommand extends Command |
| 26 | +{ |
| 27 | + use LocatorAwareTrait; |
| 28 | + |
| 29 | + /** |
| 30 | + * Get the command name. |
| 31 | + * |
| 32 | + * @return string |
| 33 | + */ |
| 34 | + public static function defaultName(): string |
| 35 | + { |
| 36 | + return 'queue purge_failed'; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Gets the option parser instance and configures it. |
| 41 | + * |
| 42 | + * @return \Cake\Console\ConsoleOptionParser |
| 43 | + */ |
| 44 | + public function getOptionParser(): ConsoleOptionParser |
| 45 | + { |
| 46 | + $parser = parent::getOptionParser(); |
| 47 | + |
| 48 | + $parser->setDescription('Delete failed jobs.'); |
| 49 | + |
| 50 | + $parser->addArgument('ids', [ |
| 51 | + 'required' => false, |
| 52 | + 'help' => 'Delete jobs by the FailedJob ID (comma-separated).', |
| 53 | + ]); |
| 54 | + $parser->addOption('class', [ |
| 55 | + 'help' => 'Delete jobs by the job class.', |
| 56 | + ]); |
| 57 | + $parser->addOption('queue', [ |
| 58 | + 'help' => 'Delete jobs by the queue the job was received on.', |
| 59 | + ]); |
| 60 | + $parser->addOption('config', [ |
| 61 | + 'help' => 'Delete jobs by the config used to queue the job.', |
| 62 | + ]); |
| 63 | + $parser->addOption('force', [ |
| 64 | + 'help' => 'Automatically assume yes in response to confirmation prompt.', |
| 65 | + 'short' => 'f', |
| 66 | + 'boolean' => true, |
| 67 | + ]); |
| 68 | + |
| 69 | + return $parser; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @param \Cake\Console\Arguments $args Arguments |
| 74 | + * @param \Cake\Console\ConsoleIo $io ConsoleIo |
| 75 | + * @return void |
| 76 | + */ |
| 77 | + public function execute(Arguments $args, ConsoleIo $io) |
| 78 | + { |
| 79 | + /** @var \Cake\Queue\Model\Table\FailedJobsTable $failedJobsTable */ |
| 80 | + $failedJobsTable = $this->getTableLocator()->get('Cake/Queue.FailedJobs'); |
| 81 | + |
| 82 | + $jobsToDelete = $failedJobsTable->find(); |
| 83 | + |
| 84 | + if ($args->hasArgument('ids')) { |
| 85 | + $idsArg = $args->getArgument('ids'); |
| 86 | + |
| 87 | + if ($idsArg !== null) { |
| 88 | + $ids = explode(',', $idsArg); |
| 89 | + |
| 90 | + $jobsToDelete->whereInList($failedJobsTable->aliasField('id'), $ids); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + if ($args->hasOption('class')) { |
| 95 | + $jobsToDelete->where(['class' => $args->getOption('class')]); |
| 96 | + } |
| 97 | + |
| 98 | + if ($args->hasOption('queue')) { |
| 99 | + $jobsToDelete->where(['queue' => $args->getOption('queue')]); |
| 100 | + } |
| 101 | + |
| 102 | + if ($args->hasOption('config')) { |
| 103 | + $jobsToDelete->where(['config' => $args->getOption('config')]); |
| 104 | + } |
| 105 | + |
| 106 | + $deletingCount = $jobsToDelete->count(); |
| 107 | + |
| 108 | + if (!$deletingCount) { |
| 109 | + $io->out('0 jobs found.'); |
| 110 | + |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + if (!$args->getOption('force')) { |
| 115 | + $confirmed = $io->askChoice("Delete {$deletingCount} jobs?", ['y', 'n'], 'n'); |
| 116 | + |
| 117 | + if ($confirmed !== 'y') { |
| 118 | + return; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + $io->out("Deleting {$deletingCount} jobs."); |
| 123 | + |
| 124 | + $failedJobsTable->deleteManyOrFail($jobsToDelete); |
| 125 | + |
| 126 | + $io->success("{$deletingCount} jobs deleted."); |
| 127 | + } |
| 128 | +} |
0 commit comments