From 99ee8f757acac5b27290cc2f4b2f5ba873cef75e Mon Sep 17 00:00:00 2001 From: Adam Rusinowski Date: Mon, 18 Nov 2024 18:10:43 +0100 Subject: [PATCH] added purging all queues --- src/Command/PurgeQueueCommand.php | 101 +++++++++++++++++++++--------- 1 file changed, 70 insertions(+), 31 deletions(-) diff --git a/src/Command/PurgeQueueCommand.php b/src/Command/PurgeQueueCommand.php index 907dc68..d258ef3 100644 --- a/src/Command/PurgeQueueCommand.php +++ b/src/Command/PurgeQueueCommand.php @@ -22,6 +22,8 @@ use CakeDC\QueueMonitor\Exception\QueueMonitorException; use CakeDC\QueueMonitor\Service\EnqueueClientService; use Psr\Log\LogLevel; +use function Cake\I18n\__; +use function Cake\Collection\collection; /** * Purge command. @@ -66,6 +68,12 @@ public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionPar ->addArgument('queue-config', [ 'help' => __('Queue configuration key'), ]) + ->addOption('all', [ + 'help' => __('All messages will be purged'), + 'short' => 'a', + 'boolean' => true, + 'default' => false, + ]) ->addOption('yes', [ 'short' => 'y', 'boolean' => true, @@ -87,44 +95,54 @@ public function execute(Arguments $args, ConsoleIo $io) return self::CODE_SUCCESS; } - $queueConfig = $args->getArgument('queue-config'); - - if (!$this->validateQueueConfig($queueConfig)) { - $io->error(__('Queue configuration key is invalid')); - $configuredQueues = $this->getConfiguredQueues(); - if ($configuredQueues) { - $io->error(__('Valid configuration keys are: {0}', implode(', ', $configuredQueues))); - } else { - $io->error(__('There are no queue configurations')); - } - $this->displayHelp($this->getOptionParser(), $args, $io); - return self::CODE_ERROR; - } + if ($args->getOption('all')) { + $this->checkConfirmation( + __('Are you sure you want to purge messages from all queues?'), + $args, + $io + ); - if (!$args->getOption('yes')) { - $confirmation = $io->askChoice( + collection($this->getConfiguredQueues())->each(function (string $queueConfig) use ($io): void { + try { + $this->enqueueClientService->purgeQueue($queueConfig); + $io->success(__('Queue `{0}` purged successfully', $queueConfig)); + } catch (QueueMonitorException $e) { + $io->error(__('Unable to purge queue `{0}`, reason: {1}', $queueConfig, $e->getMessage())); + } + }); + } else { + $queueConfig = $args->getArgument('queue-config'); + + if (!$this->validateQueueConfig($queueConfig)) { + $io->error(__('Queue configuration key is invalid')); + $configuredQueues = $this->getConfiguredQueues(); + if ($configuredQueues) { + $io->error(__('Valid configuration keys are: {0}', implode(', ', $configuredQueues))); + } else { + $io->error(__('There are no queue configurations')); + } + $this->displayHelp($this->getOptionParser(), $args, $io); + + return self::CODE_ERROR; + } + + $this->checkConfirmation( __('Are you sure you want to purge messages from specified queue?'), - [ - __('yes'), - __('no') - ], - __('no') + $args, + $io ); - if ($confirmation === __('no')) { - $io->abort(__('Aborting')); - } - } - try { - $this->enqueueClientService->purgeQueue($queueConfig); - $io->success(__('Queue purged successfully')); + try { + $this->enqueueClientService->purgeQueue($queueConfig); + $io->success(__('Queue `{0}` purged successfully', $queueConfig)); - return self::CODE_SUCCESS; - } catch (QueueMonitorException $e) { - $io->error(__('Unable to purge queue, reason: {0}', $e->getMessage())); + return self::CODE_SUCCESS; + } catch (QueueMonitorException $e) { + $io->error(__('Unable to purge queue `{0}`, reason: {1}', $queueConfig, $e->getMessage())); - return self::CODE_ERROR; + return self::CODE_ERROR; + } } } @@ -153,4 +171,25 @@ private function getConfiguredQueues(): array { return array_keys(Configure::read('Queue', [])); } + + /** + * Check confirmation + */ + private function checkConfirmation(string $prompt, Arguments $args, ConsoleIo $io): void + { + if (!$args->getOption('yes')) { + $confirmation = $io->askChoice( + $prompt, + [ + __('yes'), + __('no') + ], + __('no') + ); + + if ($confirmation === __('no')) { + $io->abort(__('Aborting')); + } + } + } }