From 73ce35044b2fe0dd580723520ab364452f6616a2 Mon Sep 17 00:00:00 2001 From: Jeroeny Date: Fri, 26 Nov 2021 17:57:14 +0100 Subject: [PATCH 1/3] add consumer strategy for poll mechanism of receivers --- .../Messenger/Command/ConsumeMessagesCommand.php | 7 +++++++ src/Symfony/Component/Messenger/Worker.php | 9 ++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Messenger/Command/ConsumeMessagesCommand.php b/src/Symfony/Component/Messenger/Command/ConsumeMessagesCommand.php index 4fe905df7dbf8..34642b8d32c50 100644 --- a/src/Symfony/Component/Messenger/Command/ConsumeMessagesCommand.php +++ b/src/Symfony/Component/Messenger/Command/ConsumeMessagesCommand.php @@ -31,6 +31,7 @@ use Symfony\Component\Messenger\EventListener\StopWorkerOnMemoryLimitListener; use Symfony\Component\Messenger\EventListener\StopWorkerOnMessageLimitListener; use Symfony\Component\Messenger\EventListener\StopWorkerOnTimeLimitListener; +use Symfony\Component\Messenger\Exception\InvalidArgumentException; use Symfony\Component\Messenger\RoutableMessageBus; use Symfony\Component\Messenger\Worker; @@ -79,6 +80,7 @@ protected function configure(): void new InputOption('bus', 'b', InputOption::VALUE_REQUIRED, 'Name of the bus to which received messages should be dispatched (if not passed, bus is determined automatically)'), new InputOption('queues', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Limit receivers to only consume from the specified queues'), new InputOption('no-reset', null, InputOption::VALUE_NONE, 'Do not reset container services after each message'), + new InputOption('strategy', null, InputOption::VALUE_REQUIRED, 'The strategy to use to consume from the different receivers: "priority", "ordered", "random"', 'priority'), ]) ->setHelp(<<<'EOF' The %command.name% command consumes messages and dispatches them to the message bus. @@ -212,10 +214,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int } $bus = $input->getOption('bus') ? $this->routableBus->getMessageBus($input->getOption('bus')) : $this->routableBus; + $strategies = ['priority', 'ordered', 'random']; + if (! in_array($input->getOption('strategy'), $strategies, true)) { + throw new InvalidArgumentException(sprintf('The "%s" strategy does not exist. Available strategies are: "%s".', $input->getOption('strategy'), implode('", "', $strategies))); + } $worker = new Worker($receivers, $bus, $this->eventDispatcher, $this->logger); $options = [ 'sleep' => $input->getOption('sleep') * 1000000, + 'strategy' => $input->getOption('strategy'), ]; if ($queues = $input->getOption('queues')) { $options['queues'] = $queues; diff --git a/src/Symfony/Component/Messenger/Worker.php b/src/Symfony/Component/Messenger/Worker.php index 247a0eadf25ec..afbd440135a69 100644 --- a/src/Symfony/Component/Messenger/Worker.php +++ b/src/Symfony/Component/Messenger/Worker.php @@ -38,6 +38,10 @@ */ class Worker { + private const STRATEGY_PRIORITY = 'priority'; + private const STRATEGY_ORDERED = 'ordered'; + private const STRATEGY_RANDOM = 'random'; + private array $receivers; private MessageBusInterface $bus; private ?EventDispatcherInterface $eventDispatcher; @@ -73,6 +77,7 @@ public function run(array $options = []): void { $options = array_merge([ 'sleep' => 1000000, + 'strategy' => self::STRATEGY_PRIORITY, ], $options); $queueNames = $options['queues'] ?? null; @@ -92,6 +97,8 @@ public function run(array $options = []): void while (!$this->shouldStop) { $envelopeHandled = false; $envelopeHandledStart = microtime(true); + $receivers = $this->receivers; + $receivers = $options['strategy'] === self::STRATEGY_RANDOM ? shuffle($receivers) : $receivers; foreach ($this->receivers as $transportName => $receiver) { if ($queueNames) { $envelopes = $receiver->getFromQueues($queueNames); @@ -113,7 +120,7 @@ public function run(array $options = []): void // after handling a single receiver, quit and start the loop again // this should prevent multiple lower priority receivers from // blocking too long before the higher priority are checked - if ($envelopeHandled) { + if ($envelopeHandled && $options['strategy'] === self::STRATEGY_PRIORITY) { break; } } From 08ef2d4097c5f426696005cd8032f566bea90534 Mon Sep 17 00:00:00 2001 From: Jeroeny Date: Fri, 26 Nov 2021 18:10:29 +0100 Subject: [PATCH 2/3] replace last comma with 'or' --- .../Component/Messenger/Command/ConsumeMessagesCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Messenger/Command/ConsumeMessagesCommand.php b/src/Symfony/Component/Messenger/Command/ConsumeMessagesCommand.php index 34642b8d32c50..6994bf621dcc7 100644 --- a/src/Symfony/Component/Messenger/Command/ConsumeMessagesCommand.php +++ b/src/Symfony/Component/Messenger/Command/ConsumeMessagesCommand.php @@ -80,7 +80,7 @@ protected function configure(): void new InputOption('bus', 'b', InputOption::VALUE_REQUIRED, 'Name of the bus to which received messages should be dispatched (if not passed, bus is determined automatically)'), new InputOption('queues', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Limit receivers to only consume from the specified queues'), new InputOption('no-reset', null, InputOption::VALUE_NONE, 'Do not reset container services after each message'), - new InputOption('strategy', null, InputOption::VALUE_REQUIRED, 'The strategy to use to consume from the different receivers: "priority", "ordered", "random"', 'priority'), + new InputOption('strategy', null, InputOption::VALUE_REQUIRED, 'The strategy to use to consume from the different receivers: "priority", "ordered" or "random"', 'priority'), ]) ->setHelp(<<<'EOF' The %command.name% command consumes messages and dispatches them to the message bus. From 2533553ab7cb2142a0feae7af67105c8293b5be5 Mon Sep 17 00:00:00 2001 From: Jeroeny Date: Fri, 26 Nov 2021 18:12:57 +0100 Subject: [PATCH 3/3] apply codestyle patch --- .../Component/Messenger/Command/ConsumeMessagesCommand.php | 2 +- src/Symfony/Component/Messenger/Worker.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Messenger/Command/ConsumeMessagesCommand.php b/src/Symfony/Component/Messenger/Command/ConsumeMessagesCommand.php index 6994bf621dcc7..2e225fcc6e05d 100644 --- a/src/Symfony/Component/Messenger/Command/ConsumeMessagesCommand.php +++ b/src/Symfony/Component/Messenger/Command/ConsumeMessagesCommand.php @@ -215,7 +215,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $bus = $input->getOption('bus') ? $this->routableBus->getMessageBus($input->getOption('bus')) : $this->routableBus; $strategies = ['priority', 'ordered', 'random']; - if (! in_array($input->getOption('strategy'), $strategies, true)) { + if (!\in_array($input->getOption('strategy'), $strategies, true)) { throw new InvalidArgumentException(sprintf('The "%s" strategy does not exist. Available strategies are: "%s".', $input->getOption('strategy'), implode('", "', $strategies))); } diff --git a/src/Symfony/Component/Messenger/Worker.php b/src/Symfony/Component/Messenger/Worker.php index afbd440135a69..9fc03cb253c09 100644 --- a/src/Symfony/Component/Messenger/Worker.php +++ b/src/Symfony/Component/Messenger/Worker.php @@ -98,7 +98,7 @@ public function run(array $options = []): void $envelopeHandled = false; $envelopeHandledStart = microtime(true); $receivers = $this->receivers; - $receivers = $options['strategy'] === self::STRATEGY_RANDOM ? shuffle($receivers) : $receivers; + $receivers = self::STRATEGY_RANDOM === $options['strategy'] ? shuffle($receivers) : $receivers; foreach ($this->receivers as $transportName => $receiver) { if ($queueNames) { $envelopes = $receiver->getFromQueues($queueNames); @@ -120,7 +120,7 @@ public function run(array $options = []): void // after handling a single receiver, quit and start the loop again // this should prevent multiple lower priority receivers from // blocking too long before the higher priority are checked - if ($envelopeHandled && $options['strategy'] === self::STRATEGY_PRIORITY) { + if ($envelopeHandled && self::STRATEGY_PRIORITY === $options['strategy']) { break; } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy