Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ bin/cake queue_monitor notify
This command will send notification emails to recipients specified in `QueueMonitor.notificationRecipients`. 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.

## Purge command

The logs table may grow overtime, to keep it slim you can use the purge command:
Expand Down
117 changes: 117 additions & 0 deletions src/Command/TestQueueCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php
declare(strict_types=1);

/**
* Copyright 2010 - 2024, Cake Development Corporation (https://www.cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2024, Cake Development Corporation (https://www.cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace CakeDC\QueueMonitor\Command;

use Cake\Command\Command;
use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
use Cake\Console\ConsoleOptionParser;
use Cake\Core\Configure;
use Cake\Mailer\MailerAwareTrait;
use Cake\Validation\Validation;
use CakeDC\QueueMonitor\Core\DisableTrait;
use Psr\Log\LogLevel;
use function Cake\Collection\collection;
use function Cake\I18n\__;

/**
* Test Queue Command
*/
final class TestQueueCommand extends Command
{
use DisableTrait;
use MailerAwareTrait;

private const ARGUMENT_EMAIL = 'email';

/**
* @inheritDoc
*/
public static function defaultName(): string
{
return 'queue-monitor test-queue';
}

/**
* @inheritDoc
*/
public static function getDescription(): string
{
return __('Enqueue test email');
}

/**
* @inheritDoc
*/
public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
{
return parent::buildOptionParser($parser)
->setDescription(self::getDescription())
->addArgument($this::ARGUMENT_EMAIL, [
'help' => __('Email to send to'),
'required' => true,
]);
}

/**
* @inheritDoc
*/
public function execute(Arguments $args, ConsoleIo $io)
{
if ($this->isDisabled()) {
$this->log(
'Test Enqueue was not performed because Queue Monitor is disabled.',
LogLevel::WARNING
);

return self::CODE_SUCCESS;
}

$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\TestQueueMailer $mailer */
$mailer = $this->getMailer('CakeDC/QueueMonitor.TestQueue');
/** @uses \CakeDC\QueueMonitor\Mailer\TestQueueMailer::testQueue() */
$mailer->push(
action: $mailer::SEND_TEST_QUEUE,
args: [
$email,
$queueConfigKey,
],
options: [
'config' => $queueConfigKey,
]
);
$io->info(__(
'Queued test email `{0}` in queue `{1}`',
$email,
$queueConfigKey
));
});

return $this::CODE_SUCCESS;
}
}
50 changes: 50 additions & 0 deletions src/Mailer/TestQueueMailer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
declare(strict_types=1);

/**
* Copyright 2010 - 2024, Cake Development Corporation (https://www.cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2024, Cake Development Corporation (https://www.cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace CakeDC\QueueMonitor\Mailer;

use Cake\Core\Configure;
use Cake\Mailer\Mailer;
use Cake\Mailer\Message;
use Cake\Queue\Mailer\QueueTrait;

/**
* Test Queue Mailer
*/
class TestQueueMailer extends Mailer
{
use QueueTrait;

public const SEND_TEST_QUEUE = 'testQueue';

/**
* Mailer's name.
*
* @var string
*/
public static $name = 'TestQueue';

/**
* Send test email
*/
public function testQueue(string $emailAddress, ?string $queueConfig = 'default'): void
{
$this
->setProfile(Configure::read('QueueMonitor.mailerConfig', 'default'))
->setTo($emailAddress)
->setSubject(__('Test enqueue from queue `{0}`', $queueConfig))
->setEmailFormat(Message::MESSAGE_BOTH)
->viewBuilder()
->disableAutoLayout()
->setTemplate('CakeDC/QueueMonitor.test_enqueue');
}
}
2 changes: 2 additions & 0 deletions src/QueueMonitorPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Cake\Core\ContainerInterface;
use CakeDC\QueueMonitor\Command\NotifyCommand;
use CakeDC\QueueMonitor\Command\PurgeCommand;
use CakeDC\QueueMonitor\Command\TestQueueCommand;
use CakeDC\QueueMonitor\Service\QueueMonitoringService;

/**
Expand Down Expand Up @@ -45,6 +46,7 @@ class QueueMonitorPlugin extends BasePlugin
public function console(CommandCollection $commands): CommandCollection
{
return parent::console($commands)
->add('queue-monitor test-queue', TestQueueCommand::class)
->add('queue_monitor purge', PurgeCommand::class)
->add('queue_monitor notify', NotifyCommand::class);
}
Expand Down
5 changes: 5 additions & 0 deletions templates/email/html/test_enqueue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
declare(strict_types=1);
?>

Test email
5 changes: 5 additions & 0 deletions templates/email/text/test_enqueue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
declare(strict_types=1);
?>

Test email