Skip to content

Commit

Permalink
Merge pull request #2 from CakeDC/feature/disable-option
Browse files Browse the repository at this point in the history
added support for disabling queue monitor commands
  • Loading branch information
arusinowski authored Oct 17, 2024
2 parents 09820e0 + 510616c commit f440c9e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
8 changes: 8 additions & 0 deletions src/Command/NotifyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Cake\Console\ConsoleOptionParser;
use Cake\Core\Configure;
use Cake\Log\LogTrait;
use CakeDC\QueueMonitor\Core\DisableTrait;
use CakeDC\QueueMonitor\Service\QueueMonitoringService;
use Exception;

Expand All @@ -26,6 +27,7 @@
*/
final class NotifyCommand extends Command
{
use DisableTrait;
use LogTrait;

private const DEFAULT_LONG_JOB_IN_MINUTES = 30;
Expand Down Expand Up @@ -61,6 +63,12 @@ public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionPar
*/
public function execute(Arguments $args, ConsoleIo $io)
{
if ($this->isDisabled()) {
$this->log('Notification were not sent because Queue Monitor is disabled.');

return self::CODE_SUCCESS;
}

try {
$this->queueMonitoringService->notifyAboutLongRunningJobs(
(int)Configure::read(
Expand Down
8 changes: 8 additions & 0 deletions src/Command/PurgeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Cake\Console\ConsoleOptionParser;
use Cake\Core\Configure;
use Cake\Log\LogTrait;
use CakeDC\QueueMonitor\Core\DisableTrait;
use CakeDC\QueueMonitor\Service\QueueMonitoringService;
use Exception;
use Psr\Log\LogLevel;
Expand All @@ -27,6 +28,7 @@
*/
final class PurgeCommand extends Command
{
use DisableTrait;
use LogTrait;

private const DEFAULT_PURGE_DAYS_OLD = 30;
Expand Down Expand Up @@ -62,6 +64,12 @@ public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionPar
*/
public function execute(Arguments $args, ConsoleIo $io)
{
if ($this->isDisabled()) {
$this->log('Logs were not purged because Queue Monitor is disabled.');

return self::CODE_SUCCESS;
}

$purgeToDate = $this->queueMonitoringService->getPurgeToDate(
(int)Configure::read(
'QueueMonitor.purgeLogsOlderThanDays',
Expand Down
20 changes: 20 additions & 0 deletions src/Core/DisableTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);

namespace CakeDC\QueueMonitor\Core;

use Cake\Core\Configure;

/**
* Disable trait
*/
trait DisableTrait
{
/**
* Check if queue monitoring is disabled by configuration
*/
protected function isDisabled(): bool
{
return (bool)Configure::read('QueueMonitor.disabled', false);
}
}
11 changes: 2 additions & 9 deletions src/Listener/QueueMonitorListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/
namespace CakeDC\QueueMonitor\Listener;

use Cake\Core\Configure;
use Cake\Event\EventInterface;
use Cake\Event\EventListenerInterface;
use Cake\I18n\FrozenTime;
Expand All @@ -22,6 +21,7 @@
use Cake\ORM\Table;
use Cake\Queue\Job\Message;
use Cake\Utility\Hash;
use CakeDC\QueueMonitor\Core\DisableTrait;
use CakeDC\QueueMonitor\Exception\QueueMonitorException;
use CakeDC\QueueMonitor\Model\Status\MessageEvent;
use CakeDC\QueueMonitor\Model\Table\LogsTable;
Expand All @@ -36,6 +36,7 @@
*/
final class QueueMonitorListener implements EventListenerInterface
{
use DisableTrait;
use LocatorAwareTrait;
use LogTrait;

Expand Down Expand Up @@ -226,12 +227,4 @@ public function validateInteropQueueMessage(?QueueMessage $queueMessage): QueueM

return $queueMessage;
}

/**
* Check if queue monitoring is disabled by configuration
*/
private function isDisabled(): bool
{
return (bool)Configure::read('QueueMonitor.disabled', false);
}
}

0 comments on commit f440c9e

Please sign in to comment.