Skip to content

Commit

Permalink
added purging all queues
Browse files Browse the repository at this point in the history
  • Loading branch information
arusinowski committed Nov 18, 2024
1 parent 1f3f543 commit 99ee8f7
Showing 1 changed file with 70 additions and 31 deletions.
101 changes: 70 additions & 31 deletions src/Command/PurgeQueueCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand All @@ -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;
}
}
}

Expand Down Expand Up @@ -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'));
}
}
}
}

0 comments on commit 99ee8f7

Please sign in to comment.