Skip to content

Commit

Permalink
added TestEnqueueCommand.php
Browse files Browse the repository at this point in the history
  • Loading branch information
arusinowski committed Nov 9, 2024
1 parent 0357744 commit b40f50a
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 4 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 `[email protected]` with working
email address:
```shell
bin/cake queue-monitor test-enqueue [email protected]
```

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
Expand Down
2 changes: 1 addition & 1 deletion src/Command/NotifyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function __construct(
*/
public static function defaultName(): string
{
return 'queue_monitor notify';
return 'queue-monitor notify';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Command/PurgeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function __construct(
*/
public static function defaultName(): string
{
return 'queue_monitor purge';
return 'queue-monitor purge';
}

/**
Expand Down
103 changes: 103 additions & 0 deletions src/Command/TestEnqueueCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?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;

/**
* Test Enqueue Command
*/
final class TestEnqueueCommand extends Command
{
use MailerAwareTrait;

private const ARGUMENT_EMAIL = 'email';

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

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

/**
* @inheritDoc
*/
public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
{
return parent::buildOptionParser($parser)
->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;
}
}
50 changes: 50 additions & 0 deletions src/Mailer/TestEnqueueMailer.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 Enqueue Mailer
*/
class TestEnqueueMailer extends Mailer
{
use QueueTrait;

public const SEND_TEST_ENQUEUE = 'testEnqueue';

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

/**
* Send test email
*/
public function testEnqueue(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('QueueMonitor.test_enqueue');
}
}
5 changes: 5 additions & 0 deletions templates/email/html/test.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.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
declare(strict_types=1);
?>

Test email

0 comments on commit b40f50a

Please sign in to comment.