From b40f50a8e28a6eafba90c61114ed4a10d38ae7fd Mon Sep 17 00:00:00 2001 From: Adam Rusinowski Date: Sat, 9 Nov 2024 20:37:04 +0100 Subject: [PATCH] added TestEnqueueCommand.php --- README.md | 14 +++- src/Command/NotifyCommand.php | 2 +- src/Command/PurgeCommand.php | 2 +- src/Command/TestEnqueueCommand.php | 103 +++++++++++++++++++++++++++++ src/Mailer/TestEnqueueMailer.php | 50 ++++++++++++++ templates/email/html/test.php | 5 ++ templates/email/text/test.php | 5 ++ 7 files changed, 177 insertions(+), 4 deletions(-) create mode 100644 src/Command/TestEnqueueCommand.php create mode 100644 src/Mailer/TestEnqueueMailer.php create mode 100644 templates/email/html/test.php create mode 100644 templates/email/text/test.php diff --git a/README.md b/README.md index d32f328..64dbec2 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,7 @@ For each queue configuration add `listener` setting To set up notifications when there are long running or possible stuck jobs please use command ```shell -bin/cake queue_monitor notify +bin/cake queue-monitor notify ``` This command will send notification emails to recipients specified in `QueueMonitor.notificationRecipients`. Best is @@ -107,12 +107,22 @@ to use it as a cronjob The logs table may grow overtime, to keep it slim you can use the purge command: ```shell -bin/cake queue_monitor purge +bin/cake queue-monitor purge ``` This command will purge logs older than value specified in `QueueMonitor.purgeLogsOlderThanDays`, the value is in days, the default is 30 days. Best is to use it as a cronjob +## Test Enqueue command + +To quickly test if all queues are running correctly please run this command (replace `your-email@domain.com` with working +email address: +```shell +bin/cake queue-monitor test-enqueue your-email@domain.com +``` + +This command will send the command through all configured queues. + ## Important Make sure your Job classes have a property value of maxAttempts because if it's missing, the log table can quickly diff --git a/src/Command/NotifyCommand.php b/src/Command/NotifyCommand.php index 6ec4f17..0650df8 100644 --- a/src/Command/NotifyCommand.php +++ b/src/Command/NotifyCommand.php @@ -47,7 +47,7 @@ public function __construct( */ public static function defaultName(): string { - return 'queue_monitor notify'; + return 'queue-monitor notify'; } /** diff --git a/src/Command/PurgeCommand.php b/src/Command/PurgeCommand.php index d99b81e..1b93713 100644 --- a/src/Command/PurgeCommand.php +++ b/src/Command/PurgeCommand.php @@ -47,7 +47,7 @@ public function __construct( */ public static function defaultName(): string { - return 'queue_monitor purge'; + return 'queue-monitor purge'; } /** diff --git a/src/Command/TestEnqueueCommand.php b/src/Command/TestEnqueueCommand.php new file mode 100644 index 0000000..784f30b --- /dev/null +++ b/src/Command/TestEnqueueCommand.php @@ -0,0 +1,103 @@ +setDescription(__('Enqueue test email')) + ->addArgument($this::ARGUMENT_EMAIL, [ + 'help' => __('Email to send to'), + 'required' => true, + ]); + } + + /** + * @inheritDoc + */ + public function execute(Arguments $args, ConsoleIo $io) + { + $email = $args->getArgument(self::ARGUMENT_EMAIL); + if (!Validation::email($email)) { + $io->error(__('Invalid email')); + + return $this::CODE_ERROR; + } + + collection(Configure::read('Queue', [])) + ->each(function ( + array $queueConfig, + string $queueConfigKey + ) use ( + $email, + $io + ): void { + /** @var \CakeDC\QueueMonitor\Mailer\TestEnqueueMailer $mailer */ + $mailer = $this->getMailer('QueueMonitor.TestEnqueue'); + /** @uses \CakeDC\QueueMonitor\Mailer\TestEnqueueMailer::testEnqueue() */ + $mailer->push( + action: $mailer::SEND_TEST_ENQUEUE, + args: [ + $email, + $queueConfigKey, + ], + options: [ + 'config' => $queueConfigKey, + ] + ); + $io->info(__( + 'Enqueued test email `{0}` in queue `{1}`', + $email, + $queueConfigKey + )); + }); + + return $this::CODE_SUCCESS; + } +} diff --git a/src/Mailer/TestEnqueueMailer.php b/src/Mailer/TestEnqueueMailer.php new file mode 100644 index 0000000..a657bf4 --- /dev/null +++ b/src/Mailer/TestEnqueueMailer.php @@ -0,0 +1,50 @@ +setProfile(Configure::read('QueueMonitor.mailerConfig', 'default')) + ->setTo($emailAddress) + ->setSubject(__('Test enqueue from queue `{0}`', $queueConfig)) + ->setEmailFormat(Message::MESSAGE_BOTH) + ->viewBuilder() + ->disableAutoLayout() + ->setTemplate('QueueMonitor.test_enqueue'); + } +} diff --git a/templates/email/html/test.php b/templates/email/html/test.php new file mode 100644 index 0000000..515332a --- /dev/null +++ b/templates/email/html/test.php @@ -0,0 +1,5 @@ + + +Test email diff --git a/templates/email/text/test.php b/templates/email/text/test.php new file mode 100644 index 0000000..515332a --- /dev/null +++ b/templates/email/text/test.php @@ -0,0 +1,5 @@ + + +Test email