Skip to content

Commit b40f50a

Browse files
committed
added TestEnqueueCommand.php
1 parent 0357744 commit b40f50a

File tree

7 files changed

+177
-4
lines changed

7 files changed

+177
-4
lines changed

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ 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
@@ -107,12 +107,22 @@ to use it as a cronjob
107107

108108
The logs table may grow overtime, to keep it slim you can use the purge command:
109109
```shell
110-
bin/cake queue_monitor purge
110+
bin/cake queue-monitor purge
111111
```
112112

113113
This command will purge logs older than value specified in `QueueMonitor.purgeLogsOlderThanDays`, the value is in
114114
days, the default is 30 days. Best is to use it as a cronjob
115115

116+
## Test Enqueue command
117+
118+
To quickly test if all queues are running correctly please run this command (replace `[email protected]` with working
119+
email address:
120+
```shell
121+
bin/cake queue-monitor test-enqueue [email protected]
122+
```
123+
124+
This command will send the command through all configured queues.
125+
116126
## Important
117127

118128
Make sure your Job classes have a property value of maxAttempts because if it's missing, the log table can quickly

src/Command/NotifyCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function __construct(
4747
*/
4848
public static function defaultName(): string
4949
{
50-
return 'queue_monitor notify';
50+
return 'queue-monitor notify';
5151
}
5252

5353
/**

src/Command/PurgeCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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';
5151
}
5252

5353
/**

src/Command/TestEnqueueCommand.php

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

src/Mailer/TestEnqueueMailer.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 Enqueue Mailer
22+
*/
23+
class TestEnqueueMailer extends Mailer
24+
{
25+
use QueueTrait;
26+
27+
public const SEND_TEST_ENQUEUE = 'testEnqueue';
28+
29+
/**
30+
* Mailer's name.
31+
*
32+
* @var string
33+
*/
34+
public static $name = 'TestEnqueue';
35+
36+
/**
37+
* Send test email
38+
*/
39+
public function testEnqueue(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)