Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b7a472b

Browse files
committed
Adding SizeAwareReceiverInterface to see how many messages are waiting on each receiver
1 parent 343d28e commit b7a472b

File tree

6 files changed

+76
-2
lines changed

6 files changed

+76
-2
lines changed

src/Symfony/Component/Messenger/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ CHANGELOG
44
4.3.0
55
-----
66

7+
* Added optional `MessageCountAwareInterface` that receivers can implement
8+
to give information about how many messages are waiting to be processed.
79
* [BC BREAK] The `Envelope::__construct()` signature changed:
810
you can no longer pass an unlimited number of stamps as the second,
911
third, fourth, arguments etc: stamps are now an array passed to the

src/Symfony/Component/Messenger/Tests/Transport/AmqpExt/AmqpExtIntegrationTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,24 @@ public function testItReceivesSignals()
167167
, $process->getOutput());
168168
}
169169

170+
public function testItCountsMessagesInQueue()
171+
{
172+
$serializer = $this->createSerializer();
173+
174+
$connection = Connection::fromDsn(getenv('MESSENGER_AMQP_DSN'));
175+
$connection->setup();
176+
$connection->queue()->purge();
177+
178+
$sender = new AmqpSender($connection, $serializer);
179+
180+
$sender->send($first = new Envelope(new DummyMessage('First')));
181+
$sender->send($second = new Envelope(new DummyMessage('Second')));
182+
$sender->send($second = new Envelope(new DummyMessage('Third')));
183+
184+
sleep(1); // give amqp a moment to have the messages ready
185+
$this->assertSame(3, $connection->countMessagesInQueue());
186+
}
187+
170188
private function waitForOutput(Process $process, string $output, $timeoutInSeconds = 10)
171189
{
172190
$timedOutTime = time() + $timeoutInSeconds;

src/Symfony/Component/Messenger/Transport/AmqpExt/AmqpReceiver.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Messenger\Exception\LogicException;
1616
use Symfony\Component\Messenger\Exception\MessageDecodingFailedException;
1717
use Symfony\Component\Messenger\Exception\TransportException;
18+
use Symfony\Component\Messenger\Transport\Receiver\MessageCountAwareInterface;
1819
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface;
1920
use Symfony\Component\Messenger\Transport\Serialization\PhpSerializer;
2021
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
@@ -26,7 +27,7 @@
2627
*
2728
* @experimental in 4.2
2829
*/
29-
class AmqpReceiver implements ReceiverInterface
30+
class AmqpReceiver implements ReceiverInterface, MessageCountAwareInterface
3031
{
3132
private $serializer;
3233
private $connection;
@@ -87,6 +88,14 @@ public function reject(Envelope $envelope): void
8788
$this->rejectAmqpEnvelope($this->findAmqpEnvelope($envelope));
8889
}
8990

91+
/**
92+
* {@inheritdoc}
93+
*/
94+
public function getMessageCount(): int
95+
{
96+
return $this->connection->countMessagesInQueue();
97+
}
98+
9099
private function rejectAmqpEnvelope(\AMQPEnvelope $amqpEnvelope): void
91100
{
92101
try {

src/Symfony/Component/Messenger/Transport/AmqpExt/AmqpTransport.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Messenger\Transport\AmqpExt;
1313

1414
use Symfony\Component\Messenger\Envelope;
15+
use Symfony\Component\Messenger\Transport\Receiver\MessageCountAwareInterface;
1516
use Symfony\Component\Messenger\Transport\Serialization\PhpSerializer;
1617
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
1718
use Symfony\Component\Messenger\Transport\SetupableTransportInterface;
@@ -22,7 +23,7 @@
2223
*
2324
* @experimental in 4.2
2425
*/
25-
class AmqpTransport implements TransportInterface, SetupableTransportInterface
26+
class AmqpTransport implements TransportInterface, SetupableTransportInterface, MessageCountAwareInterface
2627
{
2728
private $serializer;
2829
private $connection;
@@ -75,6 +76,14 @@ public function setup(): void
7576
$this->connection->setup();
7677
}
7778

79+
/**
80+
* {@inheritdoc}
81+
*/
82+
public function getMessageCount(): int
83+
{
84+
($this->receiver ?? $this->getReceiver())->getMessageCount();
85+
}
86+
7887
private function getReceiver()
7988
{
8089
return $this->receiver = new AmqpReceiver($this->connection, $this->serializer);

src/Symfony/Component/Messenger/Transport/AmqpExt/Connection.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,14 @@ public function publish(string $body, array $headers = [], int $delay = 0): void
184184
$this->exchange()->publish($body, $this->queueConfiguration['routing_key'] ?? null, $flags, $attributes);
185185
}
186186

187+
/**
188+
* Returns an approximate count of the messages in a queue.
189+
*/
190+
public function countMessagesInQueue(): int
191+
{
192+
return $this->queue()->declareQueue();
193+
}
194+
187195
/**
188196
* @throws \AMQPException
189197
*/
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Messenger\Transport\Receiver;
13+
14+
/**
15+
* @author Samuel Roze <[email protected]>
16+
* @author Ryan Weaver <[email protected]>
17+
*
18+
* @experimental in 4.3
19+
*/
20+
interface MessageCountAwareInterface
21+
{
22+
/**
23+
* Returns the number of messages waiting to be handled.
24+
*
25+
* In some systems, this may be an approximate number.
26+
*/
27+
public function getMessageCount(): int;
28+
}

0 commit comments

Comments
 (0)