Skip to content

Commit e3f5d97

Browse files
committed
added PurgeQueueCommand.php
1 parent b40f50a commit e3f5d97

File tree

6 files changed

+219
-28
lines changed

6 files changed

+219
-28
lines changed

src/Command/PurgeCommand.php renamed to src/Command/PurgeLogsCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
/**
2727
* Purge command.
2828
*/
29-
final class PurgeCommand extends Command
29+
final class PurgeLogsCommand extends Command
3030
{
3131
use DisableTrait;
3232
use LogTrait;
@@ -47,7 +47,7 @@ public function __construct(
4747
*/
4848
public static function defaultName(): string
4949
{
50-
return 'queue-monitor purge';
50+
return 'queue-monitor purge-logs';
5151
}
5252

5353
/**

src/Command/PurgeQueueCommand.php

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* Copyright 2010 - 2024, Cake Development Corporation (https://www.cakedc.com)
6+
*
7+
* Licensed under The MIT License
8+
* Redistributions of files must retain the above copyright notice.
9+
*
10+
* @copyright Copyright 2010 - 2024, Cake Development Corporation (https://www.cakedc.com)
11+
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
12+
*/
13+
namespace CakeDC\QueueMonitor\Command;
14+
15+
use Cake\Command\Command;
16+
use Cake\Console\Arguments;
17+
use Cake\Console\ConsoleIo;
18+
use Cake\Console\ConsoleOptionParser;
19+
use Cake\Core\Configure;
20+
use Cake\Log\LogTrait;
21+
use CakeDC\QueueMonitor\Core\DisableTrait;
22+
use CakeDC\QueueMonitor\Exception\QueueMonitorException;
23+
use CakeDC\QueueMonitor\Service\EnqueueClientService;
24+
use Psr\Log\LogLevel;
25+
26+
/**
27+
* Purge command.
28+
*/
29+
final class PurgeQueueCommand extends Command
30+
{
31+
use DisableTrait;
32+
use LogTrait;
33+
34+
/**
35+
* Constructor
36+
*/
37+
public function __construct(
38+
private readonly EnqueueClientService $enqueueClientService,
39+
) {
40+
parent::__construct();
41+
}
42+
43+
/**
44+
* @inheritDoc
45+
*/
46+
public static function defaultName(): string
47+
{
48+
return 'queue-monitor purge-queue';
49+
}
50+
51+
/**
52+
* @inheritDoc
53+
*/
54+
public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
55+
{
56+
return parent::buildOptionParser($parser)
57+
->setDescription(__('Queue purger'))
58+
->addArgument('queue-config', [
59+
'help' => __('Queue configuration key'),
60+
]);
61+
}
62+
63+
/**
64+
* @inheritDoc
65+
*/
66+
public function execute(Arguments $args, ConsoleIo $io)
67+
{
68+
if ($this->isDisabled()) {
69+
$this->log(
70+
'Logs were not purged because Queue Monitor is disabled.',
71+
LogLevel::WARNING
72+
);
73+
74+
return self::CODE_SUCCESS;
75+
}
76+
$queueConfig = $args->getArgument('queue-config');
77+
78+
if (!$this->validateQueueConfig($queueConfig)) {
79+
$io->error(__('Queue configuration key is invalid'));
80+
$configuredQueues = $this->getConfiguredQueues();
81+
if ($configuredQueues) {
82+
$io->error(__('Valid configuration keys are: {0}', implode(', ', $configuredQueues)));
83+
} else {
84+
$io->error(__('There are no queue configurations'));
85+
}
86+
87+
return self::CODE_ERROR;
88+
}
89+
90+
try {
91+
$this->enqueueClientService->purgeQueue($queueConfig);
92+
$io->success(__('Queue purged successfully'));
93+
94+
return self::CODE_SUCCESS;
95+
} catch (QueueMonitorException $e) {
96+
$io->error(__('Unable to purge queue, reason: {0}', $e->getMessage()));
97+
98+
return self::CODE_ERROR;
99+
}
100+
}
101+
102+
/**
103+
* Validate queue config
104+
*/
105+
private function validateQueueConfig(?string $queueConfig): bool
106+
{
107+
if (empty($queueConfig)) {
108+
return false;
109+
}
110+
111+
$validQueueConfigs = $this->getConfiguredQueues();
112+
113+
if (!in_array($queueConfig, $validQueueConfigs, true)) {
114+
return false;
115+
}
116+
117+
return true;
118+
}
119+
120+
/**
121+
* Get configured queues
122+
*/
123+
private function getConfiguredQueues(): array
124+
{
125+
return array_keys(Configure::read('Queue', []));
126+
}
127+
}

src/Command/TestEnqueueCommand.php renamed to src/Command/TestQueueCommand.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@
1919
use Cake\Core\Configure;
2020
use Cake\Mailer\MailerAwareTrait;
2121
use Cake\Validation\Validation;
22+
use CakeDC\QueueMonitor\Core\DisableTrait;
23+
use Psr\Log\LogLevel;
2224

2325
/**
24-
* Test Enqueue Command
26+
* Test Queue Command
2527
*/
26-
final class TestEnqueueCommand extends Command
28+
final class TestQueueCommand extends Command
2729
{
30+
use DisableTrait;
2831
use MailerAwareTrait;
2932

3033
private const ARGUMENT_EMAIL = 'email';
@@ -34,7 +37,7 @@ final class TestEnqueueCommand extends Command
3437
*/
3538
public static function defaultName(): string
3639
{
37-
return 'queue-monitor test-enqueue';
40+
return 'queue-monitor test-queue';
3841
}
3942

4043
/**
@@ -63,6 +66,15 @@ public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionPar
6366
*/
6467
public function execute(Arguments $args, ConsoleIo $io)
6568
{
69+
if ($this->isDisabled()) {
70+
$this->log(
71+
'Test Enqueue was not performed because Queue Monitor is disabled.',
72+
LogLevel::WARNING
73+
);
74+
75+
return self::CODE_SUCCESS;
76+
}
77+
6678
$email = $args->getArgument(self::ARGUMENT_EMAIL);
6779
if (!Validation::email($email)) {
6880
$io->error(__('Invalid email'));
@@ -78,11 +90,11 @@ public function execute(Arguments $args, ConsoleIo $io)
7890
$email,
7991
$io
8092
): void {
81-
/** @var \CakeDC\QueueMonitor\Mailer\TestEnqueueMailer $mailer */
82-
$mailer = $this->getMailer('QueueMonitor.TestEnqueue');
83-
/** @uses \CakeDC\QueueMonitor\Mailer\TestEnqueueMailer::testEnqueue() */
93+
/** @var \CakeDC\QueueMonitor\Mailer\TestQueueMailer $mailer */
94+
$mailer = $this->getMailer('CakeDC/QueueMonitor.TestQueue');
95+
/** @uses \CakeDC\QueueMonitor\Mailer\TestQueueMailer::testQueue() */
8496
$mailer->push(
85-
action: $mailer::SEND_TEST_ENQUEUE,
97+
action: $mailer::SEND_TEST_QUEUE,
8698
args: [
8799
$email,
88100
$queueConfigKey,
@@ -92,7 +104,7 @@ public function execute(Arguments $args, ConsoleIo $io)
92104
]
93105
);
94106
$io->info(__(
95-
'Enqueued test email `{0}` in queue `{1}`',
107+
'Queued test email `{0}` in queue `{1}`',
96108
$email,
97109
$queueConfigKey
98110
));

src/Mailer/TestEnqueueMailer.php renamed to src/Mailer/TestQueueMailer.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,25 @@
1818
use Cake\Queue\Mailer\QueueTrait;
1919

2020
/**
21-
* Test Enqueue Mailer
21+
* Test Queue Mailer
2222
*/
23-
class TestEnqueueMailer extends Mailer
23+
class TestQueueMailer extends Mailer
2424
{
2525
use QueueTrait;
2626

27-
public const SEND_TEST_ENQUEUE = 'testEnqueue';
27+
public const SEND_TEST_QUEUE = 'testQueue';
2828

2929
/**
3030
* Mailer's name.
3131
*
3232
* @var string
3333
*/
34-
public static $name = 'TestEnqueue';
34+
public static $name = 'TestQueue';
3535

3636
/**
3737
* Send test email
3838
*/
39-
public function testEnqueue(string $emailAddress, ?string $queueConfig = 'default'): void
39+
public function testQueue(string $emailAddress, ?string $queueConfig = 'default'): void
4040
{
4141
$this
4242
->setProfile(Configure::read('QueueMonitor.mailerConfig', 'default'))

src/QueueMonitorPlugin.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
*/
1313
namespace CakeDC\QueueMonitor;
1414

15-
use Cake\Console\CommandCollection;
1615
use Cake\Core\BasePlugin;
1716
use Cake\Core\ContainerInterface;
1817
use CakeDC\QueueMonitor\Command\NotifyCommand;
19-
use CakeDC\QueueMonitor\Command\PurgeCommand;
18+
use CakeDC\QueueMonitor\Command\PurgeLogsCommand;
19+
use CakeDC\QueueMonitor\Command\PurgeQueueCommand;
20+
use CakeDC\QueueMonitor\Service\EnqueueClientService;
2021
use CakeDC\QueueMonitor\Service\QueueMonitoringService;
2122

2223
/**
@@ -39,27 +40,24 @@ class QueueMonitorPlugin extends BasePlugin
3940
*/
4041
protected $middlewareEnabled = false;
4142

42-
/**
43-
* @inheritDoc
44-
*/
45-
public function console(CommandCollection $commands): CommandCollection
46-
{
47-
return parent::console($commands)
48-
->add('queue_monitor purge', PurgeCommand::class)
49-
->add('queue_monitor notify', NotifyCommand::class);
50-
}
51-
5243
/**
5344
* @inheritDoc
5445
*/
5546
public function services(ContainerInterface $container): void
5647
{
5748
$container->add(QueueMonitoringService::class);
49+
$container->addShared(EnqueueClientService::class);
50+
5851
$container
59-
->add(PurgeCommand::class)
52+
->add(PurgeLogsCommand::class)
6053
->addArguments([
6154
QueueMonitoringService::class,
6255
]);
56+
$container
57+
->add(PurgeQueueCommand::class)
58+
->addArguments([
59+
EnqueueClientService::class,
60+
]);
6361
$container
6462
->add(NotifyCommand::class)
6563
->addArguments([

src/Service/EnqueueClientService.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* Copyright 2010 - 2024, Cake Development Corporation (https://www.cakedc.com)
6+
*
7+
* Licensed under The MIT License
8+
* Redistributions of files must retain the above copyright notice.
9+
*
10+
* @copyright Copyright 2010 - 2024, Cake Development Corporation (https://www.cakedc.com)
11+
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
12+
*/
13+
namespace CakeDC\QueueMonitor\Service;
14+
15+
use Cake\Queue\QueueManager;
16+
use CakeDC\QueueMonitor\Exception\QueueMonitorException;
17+
use Enqueue\Client\Config;
18+
use Interop\Queue\Exception\PurgeQueueNotSupportedException;
19+
20+
/**
21+
* Enqueue client service
22+
*/
23+
final class EnqueueClientService
24+
{
25+
/**
26+
* Purge all messages from specified queue
27+
*
28+
* @throws \CakeDC\QueueMonitor\Exception\QueueMonitorException
29+
*/
30+
public function purgeQueue(string $queueConfig): void
31+
{
32+
try {
33+
$simpleClient = QueueManager::engine($queueConfig);
34+
$context = $simpleClient->getDriver()->getContext();
35+
$queueName = $this->getEnqueueInternalQueueName($simpleClient->getDriver()->getConfig());
36+
$queue = $context->createQueue($queueName);
37+
$context->purgeQueue($queue);
38+
} catch (PurgeQueueNotSupportedException $e) {
39+
throw new QueueMonitorException($e->getMessage(), $e->getCode(), $e);
40+
}
41+
}
42+
43+
/**
44+
* Get enqueue internal queue name
45+
*/
46+
private function getEnqueueInternalQueueName(Config $enqueueClientConfig): string
47+
{
48+
return implode('.', [
49+
$enqueueClientConfig->getPrefix(),
50+
$enqueueClientConfig->getApp(),
51+
$enqueueClientConfig->getDefaultQueue()
52+
]);
53+
}
54+
}

0 commit comments

Comments
 (0)