Skip to content

[Messenger] Implement MessageCountAwareInterface in RedisTransport #39127

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
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
5 changes: 5 additions & 0 deletions src/Symfony/Component/Messenger/Bridge/Redis/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

5.3.0
-----

* Implement `MessageCountAwareInterface`

5.2.0
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,4 +399,21 @@ public function testLazy()
$connection->reject($message['id']);
$redis->del('messenger-lazy');
}

public function testGetMessageCount()
{
$redis = new \Redis();
$connection = Connection::fromDsn('redis://localhost/message-count?delete_after_ack=true', [], $redis);
$redis->del('message-count');

$connection->add('first', []);
$this->assertEquals(1, $messageCountStart = $connection->getMessageCount());
$connection->add('second', []);
$this->assertEquals($messageCountStart + 1, $connection->getMessageCount());

$this->assertNotEmpty($message = $connection->get());
$connection->ack($message['id']);
$this->assertEquals($messageCountStart, $connection->getMessageCount());
$redis->del('message-count');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,19 @@ public function setup(): void
$this->autoSetup = false;
}

public function getMessageCount(): int
{
if ($this->autoSetup) {
$this->setup();
}

try {
return $this->connection->xLen($this->stream);
} catch (\RedisException $e) {
throw new TransportException($e->getMessage(), 0, $e);
}
}

private function getCurrentTimeInMilliseconds(): int
{
return (int) (microtime(true) * 1000);
Expand Down
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\LogicException;
use Symfony\Component\Messenger\Exception\MessageDecodingFailedException;
use Symfony\Component\Messenger\Transport\Receiver\MessageCountAwareInterface;
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface;
use Symfony\Component\Messenger\Transport\Serialization\PhpSerializer;
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
Expand All @@ -22,7 +23,7 @@
* @author Alexander Schranz <alexander@sulu.io>
* @author Antoine Bluchet <soyuka@gmail.com>
*/
class RedisReceiver implements ReceiverInterface
class RedisReceiver implements ReceiverInterface, MessageCountAwareInterface
{
private $connection;
private $serializer;
Expand Down Expand Up @@ -74,6 +75,14 @@ public function reject(Envelope $envelope): void
$this->connection->reject($this->findRedisReceivedStamp($envelope)->getId());
}

/**
* {@inheritdoc}
*/
public function getMessageCount(): int
{
return $this->connection->getMessageCount();
}

private function findRedisReceivedStamp(Envelope $envelope): RedisReceivedStamp
{
/** @var RedisReceivedStamp|null $redisReceivedStamp */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Messenger\Bridge\Redis\Transport;

use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Transport\Receiver\MessageCountAwareInterface;
use Symfony\Component\Messenger\Transport\Serialization\PhpSerializer;
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
use Symfony\Component\Messenger\Transport\SetupableTransportInterface;
Expand All @@ -21,7 +22,7 @@
* @author Alexander Schranz <alexander@sulu.io>
* @author Antoine Bluchet <soyuka@gmail.com>
*/
class RedisTransport implements TransportInterface, SetupableTransportInterface
class RedisTransport implements TransportInterface, SetupableTransportInterface, MessageCountAwareInterface
{
private $serializer;
private $connection;
Expand Down Expand Up @@ -58,6 +59,14 @@ public function reject(Envelope $envelope): void
($this->receiver ?? $this->getReceiver())->reject($envelope);
}

/**
* {@inheritdoc}
*/
public function getMessageCount(): int
{
return ($this->receiver ?? $this->getReceiver())->getMessageCount();
}

/**
* {@inheritdoc}
*/
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Messenger/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
-----

* `InMemoryTransport` can perform message serialization through dsn `in-memory://?serialize=true`.
* `RedisTransport` implement `MessageCountAwareInterface`

5.2.0
-----
Expand Down