Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for disabling queue monitor commands #2

Merged
merged 1 commit into from
Oct 17, 2024
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
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);
}
}