From 7c1bfcf0ae3562012954c0d04b42828a050f27f0 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Mon, 4 Dec 2023 22:20:56 +0100 Subject: [PATCH] make both options redis_sentinel and sentinel_master available everywhere --- src/Symfony/Component/Cache/CHANGELOG.md | 5 +++++ src/Symfony/Component/Cache/Traits/RedisTrait.php | 6 ++++++ .../Tests/Transport/RedisExtIntegrationTest.php | 13 +++++++++++-- .../Messenger/Bridge/Redis/Transport/Connection.php | 7 ++++++- src/Symfony/Component/Messenger/CHANGELOG.md | 1 + 5 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Cache/CHANGELOG.md b/src/Symfony/Component/Cache/CHANGELOG.md index 3290ae2e38aeb..69e8efb63483e 100644 --- a/src/Symfony/Component/Cache/CHANGELOG.md +++ b/src/Symfony/Component/Cache/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +7.1 +--- + + * Add option `sentinel_master` as an alias for `redis_sentinel` + 7.0 --- diff --git a/src/Symfony/Component/Cache/Traits/RedisTrait.php b/src/Symfony/Component/Cache/Traits/RedisTrait.php index 4928db07f4472..9852484288dc8 100644 --- a/src/Symfony/Component/Cache/Traits/RedisTrait.php +++ b/src/Symfony/Component/Cache/Traits/RedisTrait.php @@ -169,6 +169,12 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra $params += $query + $options + self::$defaultConnectionOptions; + if (isset($params['redis_sentinel']) && isset($params['sentinel_master'])) { + throw new InvalidArgumentException('Cannot use both "redis_sentinel" and "sentinel_master" at the same time.'); + } + + $params['redis_sentinel'] ??= $params['sentinel_master'] ?? null; + if (isset($params['redis_sentinel']) && !class_exists(\Predis\Client::class) && !class_exists(\RedisSentinel::class) && !class_exists(Sentinel::class)) { throw new CacheException('Redis Sentinel support requires one of: "predis/predis", "ext-redis >= 5.2", "ext-relay".'); } diff --git a/src/Symfony/Component/Messenger/Bridge/Redis/Tests/Transport/RedisExtIntegrationTest.php b/src/Symfony/Component/Messenger/Bridge/Redis/Tests/Transport/RedisExtIntegrationTest.php index a80aecd32ecb2..e9b9e80062657 100644 --- a/src/Symfony/Component/Messenger/Bridge/Redis/Tests/Transport/RedisExtIntegrationTest.php +++ b/src/Symfony/Component/Messenger/Bridge/Redis/Tests/Transport/RedisExtIntegrationTest.php @@ -220,7 +220,10 @@ public function testConnectionClaimAndRedeliver() $connection->ack($message['id']); } - public function testSentinel() + /** + * @dataProvider + */ + public function testSentinel(string $sentinelOptionName) { if (!$hosts = getenv('REDIS_SENTINEL_HOSTS')) { $this->markTestSkipped('REDIS_SENTINEL_HOSTS env var is not defined.'); @@ -234,7 +237,7 @@ public function testSentinel() $connection = Connection::fromDsn($dsn, ['delete_after_ack' => true, - 'sentinel_master' => getenv('MESSENGER_REDIS_SENTINEL_MASTER') ?: null, + $sentinelOptionName => getenv('MESSENGER_REDIS_SENTINEL_MASTER') ?: null, ], $this->redis); $connection->add('1', []); @@ -249,6 +252,12 @@ public function testSentinel() $connection->cleanup(); } + public function sentinelOptionNames(): iterable + { + yield 'redis_sentinel'; + yield 'sentinel_master'; + } + public function testLazySentinel() { $connection = Connection::fromDsn(getenv('MESSENGER_REDIS_DSN'), diff --git a/src/Symfony/Component/Messenger/Bridge/Redis/Transport/Connection.php b/src/Symfony/Component/Messenger/Bridge/Redis/Transport/Connection.php index bfdf13b8c119a..471f88375dfd3 100644 --- a/src/Symfony/Component/Messenger/Bridge/Redis/Transport/Connection.php +++ b/src/Symfony/Component/Messenger/Bridge/Redis/Transport/Connection.php @@ -78,7 +78,12 @@ public function __construct(array $options, \Redis|Relay|\RedisCluster $redis = $host = $options['host']; $port = $options['port']; $auth = $options['auth']; - $sentinelMaster = $options['sentinel_master']; + + if (isset($options['redis_sentinel']) && isset($options['sentinel_master'])) { + throw new InvalidArgumentException('Cannot use both "redis_sentinel" and "sentinel_master" at the same time.'); + } + + $sentinelMaster = $options['sentinel_master'] ?? $options['redis_sentinel'] ?? null; if (null !== $sentinelMaster && !class_exists(\RedisSentinel::class) && !class_exists(Sentinel::class)) { throw new InvalidArgumentException('Redis Sentinel support requires ext-redis>=5.2, or ext-relay.'); diff --git a/src/Symfony/Component/Messenger/CHANGELOG.md b/src/Symfony/Component/Messenger/CHANGELOG.md index 1329387596e62..937a9fcb4dd8d 100644 --- a/src/Symfony/Component/Messenger/CHANGELOG.md +++ b/src/Symfony/Component/Messenger/CHANGELOG.md @@ -4,6 +4,7 @@ CHANGELOG 7.1 --- + * Add option `redis_sentinel` as an alias for `sentinel_master` * Add `--all` option to the `messenger:consume` command 7.0