Skip to content

[Messenger] Add consumer strategy for poll mechanism of receivers #44294

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

Closed
wants to merge 3 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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" or "random"', 'priority'),
])
->setHelp(<<<'EOF'
The <info>%command.name%</info> command consumes messages and dispatches them to the message bus.
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 8 additions & 1 deletion src/Symfony/Component/Messenger/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -73,6 +77,7 @@ public function run(array $options = []): void
{
$options = array_merge([
'sleep' => 1000000,
'strategy' => self::STRATEGY_PRIORITY,
], $options);
$queueNames = $options['queues'] ?? null;

Expand All @@ -92,6 +97,8 @@ public function run(array $options = []): void
while (!$this->shouldStop) {
$envelopeHandled = false;
$envelopeHandledStart = microtime(true);
$receivers = $this->receivers;
$receivers = self::STRATEGY_RANDOM === $options['strategy'] ? shuffle($receivers) : $receivers;
foreach ($this->receivers as $transportName => $receiver) {
if ($queueNames) {
$envelopes = $receiver->getFromQueues($queueNames);
Expand All @@ -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 && self::STRATEGY_PRIORITY === $options['strategy']) {
break;
}
}
Expand Down
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