8000 :sparkles: [53543] Implement support for resetting messenger transports · symfony/symfony@63e87bd · GitHub
[go: up one dir, main page]

Skip to content

Commit 63e87bd

Browse files
committed
✨ [53543] Implement support for resetting messenger transports
1 parent 0a10839 commit 63e87bd

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2368,6 +2368,7 @@ private function registerMessengerConfiguration(array $config, ContainerBuilder
23682368
'alias' => $name,
23692369
'is_failure_transport' => \in_array($name, $failureTransports, true),
23702370
])
2371+
->addTag('kernel.reset', ['method' => 'reset', 'on_invalid' => 'ignore'])
23712372
;
23722373
$container->setDefinition($transportId = 'messenger.transport.'.$name, $transportDefinition);
23732374
$senderAliases[$name] = $transportId;

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,7 @@ public function testMessengerTransports()
948948
$container = $this->createContainerFromFile('messenger_transports');
949949
$this->assertTrue($container->hasDefinition('messenger.transport.default'));
950950
$this->assertTrue($container->getDefinition('messenger.transport.default')->hasTag('messenger.receiver'));
951+
$this->assertTrue($container->getDefinition('messenger.transport.default')->hasTag('kernel.reset'));
951952
$this->assertEquals([
952953
['alias' => 'default', 'is_failure_transport' => false], ], $container->getDefinition('messenger.transport.default')->getTag('messenger.receiver'));
953954
$transportArguments = $container->getDefinition('messenger.transport.default')->getArguments();

src/Symfony/Component/Messenger/Bridge/Redis/Transport/Connection.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Messenger\Exception\InvalidArgumentException;
1717
use Symfony\Component\Messenger\Exception\LogicException;
1818
use Symfony\Component\Messenger\Exception\TransportException;
19+
use Symfony\Contracts\Service\ResetInterface;
1920

2021
/**
2122
* A Redis connection.
@@ -28,7 +29,7 @@
2829
*
2930
* @final
3031
*/
31-
class Connection
32+
class Connection implements ResetInterface
3233
{
3334
private const DEFAULT_OPTIONS = [
3435
'host' => '127.0.0.1',
@@ -55,7 +56,8 @@ class Connection
5556
'ssl' => null, // see https://php.net/context.ssl
5657
];
5758

58-
private \Redis|Relay|\RedisCluster|\Closure $redis;
59+
private \Redis|Relay|\RedisCluster|null $redis = null;
60+
private \Closure $redisInitializer;
5961
private string $stream;
6062
private string $queue;
6163
private string $group;
@@ -112,9 +114,9 @@ public function __construct(array $options, \Redis|Relay|\RedisCluster|null $red
112114

113115
if ((\is_array($host) && null === $sentinelMaster) || $redis instanceof \RedisCluster) {
114116
$hosts = \is_string($host) ? [$host.':'.$port] : $host; // Always ensure we have an array
115-
$this->redis = static fn () => self::initializeRedisCluster($redis, $hosts, $auth, $options);
117+
$this->redisInitializer = static fn () => self::initializeRedisCluster($redis, $hosts, $auth, $options);
116118
} else {
117-
$this->redis = static function () use ($redis, $sentinelMaster, $host, $port, $options, $auth) {
119+
$this->redisInitializer = static function () use ($redis, $sentinelMaster, $host, $port, $options, $auth) {
118120
if (null !== $sentinelMaster) {
119121
$sentinelClass = \extension_loaded('redis') ? \RedisSentinel::class : Sentinel::class;
120122
$hostIndex = 0;
@@ -737,10 +739,15 @@ private function rawCommand(string $command, ...$arguments): mixed
737739

738740
private function getRedis(): \Redis|Relay|\RedisCluster
739741
{
740-
if ($this->redis instanceof \Closure) {
741-
$this->redis = ($this->redis)();
742+
if (!$this->redis) {
743+
$this->redis = ($this->redisInitializer)();
742744
}
743745

744746
return $this->redis;
745747
}
748+
749+
public function reset(): void
750+
{
751+
$this->redis = null;
752+
}
746753
}

0 commit comments

Comments
 (0)
0