Skip to content

Commit 0dbc457

Browse files
committed
added test queue command
1 parent 0357744 commit 0dbc457

File tree

5 files changed

+187
-0
lines changed

5 files changed

+187
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,16 @@ bin/cake queue_monitor notify
103103
This command will send notification emails to recipients specified in `QueueMonitor.notificationRecipients`. Best is
104104
to use it as a cronjob
105105

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+
106116
## Purge command
107117

108118
The logs table may grow overtime, to keep it slim you can use the purge command:

src/Command/TestQueueCommand.php

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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\Mailer\MailerAwareTrait;
21+
use Cake\Validation\Validation;
22+
use CakeDC\QueueMonitor\Core\DisableTrait;
23+
use Psr\Log\LogLevel;
24+
use function Cake\Collection\collection;
25+
use function Cake\I18n\__;
26+
27+
/**
28+
* Test Queue Command
29+
*/
30+
final class TestQueueCommand extends Command
31+
{
32+
use DisableTrait;
33+
use MailerAwareTrait;
34+
35+
private const ARGUMENT_EMAIL = 'email';
36+
37+
/**
38+
* @inheritDoc
39+
*/
40+
public static function defaultName(): string
41+
{
42+
return 'queue-monitor test-queue';
43+
}
44+
45+
/**
46+
* @inheritDoc
47+
*/
48+
public static function getDescription(): string
49+
{
50+
return __('Enqueue test email');
51+
}
52+
53+
/**
54+
* @inheritDoc
55+
*/
56+
public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
57+
{
58+
return parent::buildOptionParser($parser)
59+
->setDescription(self::getDescription())
60+
->addArgument($this::ARGUMENT_EMAIL, [
61+
'help' => __('Email to send to'),
62+
'required' => true,
63+
]);
64+
}
65+
66+
/**
67+
* @inheritDoc
68+
*/
69+
public function execute(Arguments $args, ConsoleIo $io)
70+
{
71+
if ($this->isDisabled()) {
72+
$this->log(
73+
'Test Enqueue was not performed because Queue Monitor is disabled.',
74+
LogLevel::WARNING
75+
);
76+
77+
return self::CODE_SUCCESS;
78+
}
79+
80+
$email = $args->getArgument(self::ARGUMENT_EMAIL);
81+
if (!Validation::email($email)) {
82+
$io->error(__('Invalid email'));
83+
84+
return $this::CODE_ERROR;
85+
}
86+
87+
collection(Configure::read('Queue', []))
88+
->each(function (
89+
array $queueConfig,
90+
string $queueConfigKey
91+
) use (
92+
$email,
93+
$io
94+
): void {
95+
/** @var \CakeDC\QueueMonitor\Mailer\TestQueueMailer $mailer */
96+
$mailer = $this->getMailer('CakeDC/QueueMonitor.TestQueue');
97+
/** @uses \CakeDC\QueueMonitor\Mailer\TestQueueMailer::testQueue() */
98+
$mailer->push(
99+
action: $mailer::SEND_TEST_QUEUE,
100+
args: [
101+
$email,
102+
$queueConfigKey,
103+
],
104+
options: [
105+
'config' => $queueConfigKey,
106+
]
107+
);
108+
$io->info(__(
109+
'Queued test email `{0}` in queue `{1}`',
110+
$email,
111+
$queueConfigKey
112+
));
113+
});
114+
115+
return $this::CODE_SUCCESS;
116+
}
117+
}

src/Mailer/TestQueueMailer.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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\Mailer;
14+
15+
use Cake\Core\Configure;
16+
use Cake\Mailer\Mailer;
17+
use Cake\Mailer\Message;
18+
use Cake\Queue\Mailer\QueueTrait;
19+
20+
/**
21+
* Test Queue Mailer
22+
*/
23+
class TestQueueMailer extends Mailer
24+
{
25+
use QueueTrait;
26+
27+
public const SEND_TEST_QUEUE = 'testQueue';
28+
29+
/**
30+
* Mailer's name.
31+
*
32+
* @var string
33+
*/
34+
public static $name = 'TestQueue';
35+
36+
/**
37+
* Send test email
38+
*/
39+
public function testQueue(string $emailAddress, ?string $queueConfig = 'default'): void
40+
{
41+
$this
42+
->setProfile(Configure::read('QueueMonitor.mailerConfig', 'default'))
43+
->setTo($emailAddress)
44+
->setSubject(__('Test enqueue from queue `{0}`', $queueConfig))
45+
->setEmailFormat(Message::MESSAGE_BOTH)
46+
->viewBuilder()
47+
->disableAutoLayout()
48+
->setTemplate('QueueMonitor.test_enqueue');
49+
}
50+
}

templates/email/html/test.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
declare(strict_types=1);
3+
?>
4+
5+
Test email

templates/email/text/test.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
declare(strict_types=1);
3+
?>
4+
5+
Test email

0 commit comments

Comments
 (0)