From af51110fe6e4094e12f4c063f15ed9a522d48e91 Mon Sep 17 00:00:00 2001 From: Adam Rusinowski Date: Thu, 17 Oct 2024 15:44:31 +0200 Subject: [PATCH] added `QueueMonitor.enabled` option --- .gitattributes | 45 +++++++++++++++++++++++++++ README.md | 4 +++ src/Listener/QueueMonitorListener.php | 21 +++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..aa4dad0 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,45 @@ +# Define the line ending behavior of the different file extensions +# Set default behavior, in case users don't have core.autocrlf set. +* text text=auto eol=lf + +.php diff=php + +# Declare files that will always have CRLF line endings on checkout. +*.bat eol=crlf + +# Declare files that will always have LF line endings on checkout. +*.pem eol=lf + +# Denote all files that are truly binary and should not be modified. +*.png binary +*.jpg binary +*.gif binary +*.ico binary +*.mo binary +*.pdf binary +*.phar binary +*.woff binary +*.woff2 binary +*.ttf binary +*.otf binary +*.eot binary + +# Remove files for archives generated using `git archive` +.github export-ignore +.phive export-ignore +contrib export-ignore +tests/test_app export-ignore +tests/TestCase export-ignore + +.editorconfig export-ignore +.gitattributes export-ignore +.gitignore export-ignore +.mailmap export-ignore +.stickler.yml export-ignore +Makefile export-ignore +phpcs.xml export-ignore +phpstan.neon.dist export-ignore +phpstan-baseline.neon export-ignore +phpunit.xml.dist export-ignore +psalm.xml export-ignore +psalm-baseline.xml export-ignore diff --git a/README.md b/README.md index f4f70c9..dffbf0f 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,10 @@ Set up the QueueMonitor configuration in your `config/app_local.php`: ```php // ... 'QueueMonitor' => [ + // With this setting you can enable or disable the queue monitoring without + // restarting the workers, the queue monitoring is enabled by default + 'disable' => false, + // mailer config, the default is `default` mailer, you can ommit // this setting if you use default value 'mailerConfig' => 'myCustomMailer', diff --git a/src/Listener/QueueMonitorListener.php b/src/Listener/QueueMonitorListener.php index 3c5dbf9..bd7d1a5 100644 --- a/src/Listener/QueueMonitorListener.php +++ b/src/Listener/QueueMonitorListener.php @@ -12,6 +12,7 @@ */ namespace CakeDC\QueueMonitor\Listener; +use Cake\Core\Configure; use Cake\Event\EventInterface; use Cake\Event\EventListenerInterface; use Cake\I18n\FrozenTime; @@ -74,6 +75,10 @@ public function implementedEvents(): array */ public function handleException(EventInterface $event, ?Message $message, ?Throwable $exception = null): void { + if ($this->isDisabled()) { + return; + } + try { $message = $this->validateQueueMessage($message); @@ -104,6 +109,10 @@ public function handleException(EventInterface $event, ?Message $message, ?Throw */ public function handleMessageEvent(EventInterface $event, ?Message $message): void { + if ($this->isDisabled()) { + return; + } + try { $message = $this->validateQueueMessage($message); @@ -123,6 +132,10 @@ public function handleMessageEvent(EventInterface $event, ?Message $message): vo */ public function handleSeen(EventInterface $event, ?QueueMessage $queueMessage): void { + if ($this->isDisabled()) { + return; + } + try { $queueMessage = $this->validateInteropQueueMessage($queueMessage); $messageBody = json_decode($queueMessage->getBody(), true); @@ -213,4 +226,12 @@ 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); + } }