8000 [WIP][Messenger] Multiple queue support + prioritization by weaverryan · Pull Request #30699 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[WIP][Messenger] Multiple queue support + prioritization #30699

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 2 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
1 change: 1 addition & 0 deletions src/Symfony/Component/Messenger/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
4.3.0
-----

* [BC BREAK]: The `Connection::exchange()` method for Amqp was made private.
* New classes: `RoutableMessageBus`, `AddBusNameStampMiddleware`
and `BusNameStamp` were added, which allow you to add a bus identifier
to the `Envelope` then find the correct bus when receiving from
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ protected function configure(): void
new InputOption('memory-limit', 'm', InputOption::VALUE_REQUIRED, 'The memory limit the worker can consume'),
new InputOption('time-limit', 't', InputOption::VALUE_REQUIRED, 'The time limit in seconds the worker can run'),
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, 'comma-separated list of queue names in order of priority'),
])
->setDescription('Consumes messages')
->setHelp(<<<'EOF'
Expand Down Expand Up @@ -183,7 +184,15 @@ protected function execute(InputInterface $input, OutputInterface $output): void
$io->comment('Re-run the command with a -vv option to see logs about consumed messages.');
}

$worker = new Worker($receiver, $bus, $receiverName, $retryStrategy, $this->eventDispatcher, $this->logger);
// TODO - make "default" equal to null?
$queues = [];
if (null !== $input->getOption('queues')) {
$queues = array_map(function ($queue) {
return trim($queue);
}, explode(',', $input->getOption('queues')));
}

$worker = new Worker($receiver, $bus, $queues, $receiverName, $retryStrategy, $this->eventDispatcher, $this->logger);
$worker->run();
}

Expand Down
30 changes: 30 additions & 0 deletions src/Symfony/Component/Messenger/Stamp/QueueNameStamp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Messenger\Stamp;

/**
* @author Ryan Weaver <ryan@symfonycasts.com>
*/
class QueueNameStamp implements StampInterface
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about defining it as final? 🤔

{
private $queueName;

public function __construct(string $queueName)
{
$this->queueName = $queueName;
}

public function getQueueName(): string
{
return $this->queueName;
}
}
15 changes: 13 additions & 2 deletions src/Symfony/Component/Messenger/Transport/AmqpExt/AmqpReceiver.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,22 @@ public function __construct(Connection $connection, SerializerInterface $seriali
/**
* {@inheritdoc}
*/
public function receive(callable $handler): void
public function receive(callable $handler, array $queues = []): void
{
if (0 === \count($queues)) {
$queues[] = null;
}

while (!$this->shouldStop) {
try {
$amqpEnvelope = $this->connection->get();
foreach ($queues as $queue) {
$amqpEnvelope = $this->connection->get($queue);

// stop when you find one
if (null !== $amqpEnvelope) {
break;
}
}
} catch (\AMQPException $exception) {
throw new TransportException($exception->getMessage(), 0, $exception);
}
Expand Down
12 changes: 7 additions & 5 deletions src/Symfony/Component/Messenger/Transport/AmqpExt/AmqpSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\TransportException;
use Symfony\Component\Messenger\Stamp\DelayStamp;
use Symfony\Component\Messenger\Stamp\QueueNameStamp;
use Symfony\Component\Messenger\Transport\Sender\SenderInterface;
use Symfony\Component\Messenger\Transport\Serialization\PhpSerializer;
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
Expand Down Expand Up @@ -43,15 +44,16 @@ public function send(Envelope $envelope): Envelope
{
$encodedMessage = $this->serializer->encode($envelope);

/** @var QueueNameStamp|null $queueNameStamp */
$queueNameStamp = $envelope->last(QueueNameStamp::class);
$queueName = null !== $queueNameStamp ? $queueNameStamp->getQueueName() : null;

/** @var DelayStamp|null $delayStamp */
$delayStamp = $envelope->last(DelayStamp::class);
$delay = 0;
if (null !== $delayStamp) {
$delay = $delayStamp->getDelay();
}
$delay = null !== $delayStamp ? $delayStamp->getDelay() : 0;

try {
$this->connection->publish($encodedMessage['body'], $encodedMessage['headers'] ?? [], $delay);
$this->connection->publish($encodedMessage['body'], $encodedMessage['headers'] ?? [], $queueName, $delay);
} catch (\AMQPException $e) {
throw new TransportException($e->getMessage(), 0, $e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public function __construct(Connection $connection, SerializerInterface $seriali
/**
* {@inheritdoc}
*/
public function receive(callable $handler): void
public function receive(callable $handler, array $queues = null): void
{
($this->receiver ?? $this->getReceiver())->receive($handler);
($this->receiver ?? $this->getReceiver())->receive($handler, $queues);
}

/**
Expand Down
Loading
0