Skip to content

[Messenger] [Redis] Fixed problem where worker stops handling messages on first empty message #48837

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

Merged
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 @@ -14,10 +14,14 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Bridge\Redis\Tests\Fixtures\DummyMessage;
use Symfony\Component\Messenger\Bridge\Redis\Transport\Connection;
use Symfony\Component\Messenger\Bridge\Redis\Transport\RedisReceiver;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\TransportException;
use Symfony\Component\Messenger\Transport\Serialization\Serializer;

/**
* @requires extension redis
*
* @group time-sensitive
* @group integration
*/
Expand Down Expand Up @@ -318,6 +322,30 @@ public function testGetAfterReject()
$redis->del('messenger-rejectthenget');
}

public function testItProperlyHandlesEmptyMessages()
{
$redisReceiver = new RedisReceiver($this->connection, new Serializer());

$this->connection->add('{"message": "Hi1"}', ['type' => DummyMessage::class]);
$this->connection->add('{"message": "Hi2"}', ['type' => DummyMessage::class]);

$redisReceiver->get();
$this->redis->xtrim('messages', 1);

// The consumer died during handling a message while performing xtrim in parallel process
$this->redis = new \Redis();
$this->connection = Connection::fromDsn(getenv('MESSENGER_REDIS_DSN'), ['delete_after_ack' => true], $this->redis);
$redisReceiver = new RedisReceiver($this->connection, new Serializer());

/** @var Envelope[] $envelope */
$envelope = $redisReceiver->get();
$this->assertCount(1, $envelope);

$message = $envelope[0]->getMessage();
$this->assertInstanceOf(DummyMessage::class, $message);
$this->assertEquals('Hi2', $message->getMessage());
}

private function getConnectionGroup(Connection $connection): string
{
$property = (new \ReflectionClass(Connection::class))->getProperty('group');
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\Exception\TransportException;
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface;
use Symfony\Component\Messenger\Transport\Serialization\PhpSerializer;
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
Expand Down Expand Up @@ -44,6 +45,18 @@ public function get(): iterable
return [];
}

if (null === $message['data']) {
try {
$this->connection->reject($message['id']);
} catch (TransportException $e) {
if ($e->getPrevious()) {
throw $e;
}
}

return $this->get();
}

$redisEnvelope = json_decode($message['data']['message'] ?? '', true);

if (null === $redisEnvelope) {
Expand Down