Skip to content

Commit 7f9999f

Browse files
committed
added commands to purge queues and test queues, cc
1 parent 97adf6a commit 7f9999f

File tree

11 files changed

+535
-23
lines changed

11 files changed

+535
-23
lines changed

.gitattributes

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Define the line ending behavior of the different file extensions
2+
# Set default behavior, in case users don't have core.autocrlf set.
3+
* text text=auto eol=lf
4+
5+
.php diff=php
6+
7+
# Declare files that will always have CRLF line endings on checkout.
8+
*.bat eol=crlf
9+
10+
# Declare files that will always have LF line endings on checkout.
11+
*.pem eol=lf
12+
13+
# Denote all files that are truly binary and should not be modified.
14+
*.png binary
15+
*.jpg binary
16+
*.gif binary
17+
*.ico binary
18+
*.mo binary
19+
*.pdf binary
20+
*.phar binary
21+
*.woff binary
22+
*.woff2 binary
23+
*.ttf binary
24+
*.otf binary
25+
*.eot binary
26+
27+
# Remove files for archives generated using `git archive`
28+
.github export-ignore
29+
.phive export-ignore
30+
contrib export-ignore
31+
tests/test_app export-ignore
32+
tests/TestCase export-ignore
33+
34+
.editorconfig export-ignore
35+
.gitattributes export-ignore
36+
.gitignore export-ignore
37+
.mailmap export-ignore
38+
.stickler.yml export-ignore
39+
Makefile export-ignore
40+
phpcs.xml export-ignore
41+
phpstan.neon.dist export-ignore
42+
phpstan-baseline.neon export-ignore
43+
phpunit.xml.dist export-ignore
44+
psalm.xml export-ignore
45+
psalm-baseline.xml export-ignore

README.md

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,43 @@ For each queue configuration add `listener` setting
9797

9898
To set up notifications when there are long running or possible stuck jobs please use command
9999
```shell
100-
bin/cake queue_monitor notify
100+
bin/cake queue-monitor notify
101101
```
102102

103103
This command will send notification emails to recipients specified in `QueueMonitor.notificationRecipients`. Best is
104-
to use it as a cronjob
104+
to use it as a cronjob.
105105

106-
## Purge command
106+
## Test Enqueue command
107+
108+
To quickly test if all queues are running correctly please run this command (replace `your-email@domain.com` with working
109+
email address:
110+
```shell
111+
bin/cake queue-monitor test-enqueue your-email@domain.com
112+
```
113+
114+
This command will send the command through all configured queues.
115+
116+
## Purge queues command
117+
To purge the content of a specified queue you can use the purge queue command:
118+
```shell
119+
bin/cake queue-monitor purge-queue your-queue-name
120+
121+
```
122+
123+
The above command will remove all pending queue jobs from the specified queue.
124+
125+
To purge all queues you can use command:
126+
127+
```shell
128+
bin/cake queue-monitor purge-queue --all
129+
130+
```
131+
132+
## Purge Logs command
107133

108134
The logs table may grow overtime, to keep it slim you can use the purge command:
109135
```shell
110-
bin/cake queue_monitor purge
136+
bin/cake queue-monitor purge-logs
111137
```
112138

113139
This command will purge logs older than value specified in `QueueMonitor.purgeLogsOlderThanDays`, the value is in

src/Command/NotifyCommand.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,15 @@ public function __construct(
4848
*/
4949
public static function defaultName(): string
5050
{
51-
return 'queue_monitor notify';
51+
return 'queue-monitor notify';
52+
}
53+
54+
/**
55+
* @inheritDoc
56+
*/
57+
public static function getDescription(): string
58+
{
59+
return __('Queue Monitoring notifier');
5260
}
5361

5462
/**
@@ -57,7 +65,7 @@ public static function defaultName(): string
5765
public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
5866
{
5967
return parent::buildOptionParser($parser)
60-
->setDescription(__('Queue Monitoring notifier'));
68+
->setDescription(self::getDescription());
6169
}
6270

6371
/**

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
use function Cake\I18n\__;
2626

2727
/**
28-
* Purge command.
28+
* Purge Logs command.
2929
*/
30-
final class PurgeCommand extends Command
30+
final class PurgeLogsCommand extends Command
3131
{
3232
use DisableTrait;
3333
use LogTrait;
@@ -48,7 +48,15 @@ public function __construct(
4848
*/
4949
public static function defaultName(): string
5050
{
51-
return 'queue_monitor purge';
51+
return 'queue-monitor purge-logs';
52+
}
53+
54+
/**
55+
* @inheritDoc
56+
*/
57+
public static function getDescription(): string
58+
{
59+
return __('Queue Monitoring log purger');
5260
}
5361

5462
/**
@@ -57,7 +65,7 @@ public static function defaultName(): string
5765
public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
5866
{
5967
return parent::buildOptionParser($parser)
60-
->setDescription(__('Queue Monitoring log purger'));
68+
->setDescription(self::getDescription());
6169
}
6270

6371
/**
@@ -73,6 +81,7 @@ public function execute(Arguments $args, ConsoleIo $io)
7381

7482
return self::CODE_SUCCESS;
7583
}
84+
7685
$purgeLogsOlderThanDays = (int)Configure::read(
7786
'QueueMonitor.purgeLogsOlderThanDays',
7887
self::DEFAULT_PURGE_DAYS_OLD

src/Command/PurgeQueueCommand.php

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
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+
use function Cake\Collection\collection;
26+
use function Cake\I18n\__;
27+
28+
/**
29+
* Purge command.
30+
*/
31+
final class PurgeQueueCommand extends Command
32+
{
33+
use DisableTrait;
34+
use LogTrait;
35+
36+
/**
37+
* Constructor
38+
*/
39+
public function __construct(
40+
private readonly EnqueueClientService $enqueueClientService,
41+
) {
42+
parent::__construct();
43+
}
44+
45+
/**
46+
* @inheritDoc
47+
*/
48+
public static function defaultName(): string
49+
{
50+
return 'queue-monitor purge-queue';
51+
}
52+
53+
/**
54+
* @inheritDoc
55+
*/
56+
public static function getDescription(): string
57+
{
58+
return __('Purge messages from specified queue');
59+
}
60+
61+
/**
62+
* @inheritDoc
63+
*/
64+
public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
65+
{
66+
return parent::buildOptionParser($parser)
67+
->setDescription(self::getDescription())
68+
->addArgument('queue-config', [
69+
'help' => __('Queue configuration key'),
70+
])
71+
->addOption('all', [
72+
'help' => __('All messages will be purged'),
73+
'short' => 'a',
74+
'boolean' => true,
75+
'default' => false,
76+
])
77+
->addOption('yes', [
78+
'short' => 'y',
79+
'boolean' => true,
80+
'default' => false,
81+
'help' => __('Yes - skip confirmation prompt'),
82+
]);
83+
}
84+
85+
/**
86+
* @inheritDoc
87+
*/
88+
public function execute(Arguments $args, ConsoleIo $io)
89+
{
90+
if ($this->isDisabled()) {
91+
$this->log(
92+
'Logs were not purged because Queue Monitor is disabled.',
93+
LogLevel::WARNING
94+
);
95+
96+
return self::CODE_SUCCESS;
97+
}
98+
99+
if ($args->getOption('all')) {
100+
$this->checkConfirmation(
101+
__('Are you sure you want to purge messages from all queues?'),
102+
$args,
103+
$io
104+
);
105+
106+
collection($this->getConfiguredQueues())->each(function (string $queueConfig) use ($io): void {
107+
try {
108+
$this->enqueueClientService->purgeQueue($queueConfig);
109+
$io->success(__('Queue `{0}` purged successfully', $queueConfig));
110+
} catch (QueueMonitorException $e) {
111+
$io->error(__('Unable to purge queue `{0}`, reason: {1}', $queueConfig, $e->getMessage()));
112+
}
113+
});
114+
} else {
115+
$queueConfig = $args->getArgument('queue-config');
116+
117+
if (!$this->validateQueueConfig($queueConfig)) {
118+
$io->error(__('Queue configuration key is invalid'));
119+
$configuredQueues = $this->getConfiguredQueues();
120+
if ($configuredQueues) {
121+
$io->error(__('Valid configuration keys are: {0}', implode(', ', $configuredQueues)));
122+
} else {
123+
$io->error(__('There are no queue configurations'));
124+
}
125+
$this->displayHelp($this->getOptionParser(), $args, $io);
126+
127+
return self::CODE_ERROR;
128+
}
129+
130+
$this->checkConfirmation(
131+
__('Are you sure you want to purge messages from specified queue?'),
132+
$args,
133+
$io
134+
);
135+
136+
try {
137+
$this->enqueueClientService->purgeQueue($queueConfig);
138+
$io->success(__('Queue `{0}` purged successfully', $queueConfig));
139+
140+
return self::CODE_SUCCESS;
141+
} catch (QueueMonitorException $e) {
142+
$io->error(__('Unable to purge queue `{0}`, reason: {1}', $queueConfig, $e->getMessage()));
143+
144+
return self::CODE_ERROR;
145+
}
146+
}
147+
}
148+
149+
/**
150+
* Validate queue config
151+
*/
152+
private function validateQueueConfig(?string $queueConfig): bool
153+
{
154+
if (empty($queueConfig)) {
155+
return false;
156+
}
157+
158+
$validQueueConfigs = $this->getConfiguredQueues();
159+
160+
if (!in_array($queueConfig, $validQueueConfigs, true)) {
161+
return false;
162+
}
163+
164+
return true;
165+
}
166+
167+
/**
168+
* Get configured queues
169+
*/
170+
private function getConfiguredQueues(): array
171+
{
172+
return array_keys(Configure::read('Queue', []));
173+
}
174+
175+
/**
176+
* Check confirmation
177+
*/
178+
private function checkConfirmation(string $prompt, Arguments $args, ConsoleIo $io): void
179+
{
180+
if (!$args->getOption('yes')) {
181+
$confirmation = $io->askChoice(
182+
$prompt,
183+
[
184+
__('yes'),
185+
__('no'),
186+
],
187+
__('no')
188+
);
189+
190+
if ($confirmation === __('no')) {
191+
$io->abort(__('Aborting'));
192+
}
193+
}
194+
}
195+
}

0 commit comments

Comments
 (0)