From 6fb1c570d6e6c520dfcdac38a23c8283c579e167 Mon Sep 17 00:00:00 2001 From: Marek Bleschke Date: Wed, 9 May 2018 09:58:57 +0200 Subject: [PATCH 1/3] settings for redis sentinel --- src/ralph/settings/base.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/ralph/settings/base.py b/src/ralph/settings/base.py index 41f30dca76..0d4a7dd4ff 100644 --- a/src/ralph/settings/base.py +++ b/src/ralph/settings/base.py @@ -287,9 +287,28 @@ def str_to_bool(s: str) -> bool: } }) +REDIS_MASTER_IP = None +REDIS_MASTER_PORT = None + +REDIS_SENTINEL_ENABLED = os.environ.get('REDIS_SENTINEL_ENABLED', 'false').lower() == 'true' # noqa: E501 +if REDIS_SENTINEL_ENABLED: + from redis.sentinel import Sentinel + + # SENTINEL_HOSTS env variable format: host_1:port;host_2:port + SENTINEL_HOSTS = os.environ['SENTINEL_HOSTS'].split(';') + REDIS_CLUSTER_NAME = os.environ['REDIS_CLUSTER_NAME'] + + sentinel = Sentinel( + [tuple(s_host.split(':')) for s_host in SENTINEL_HOSTS], + socket_timeout=0.2 + ) + REDIS_MASTER_IP, REDIS_MASTER_PORT = sentinel.discover_master( + REDIS_CLUSTER_NAME + ) + REDIS_CONNECTION = { - 'HOST': os.environ.get('REDIS_HOST', 'localhost'), - 'PORT': os.environ.get('REDIS_PORT', '6379'), + 'HOST': REDIS_MASTER_IP if REDIS_MASTER_IP else os.environ.get('REDIS_HOST', 'localhost'), # noqa: E501 + 'PORT': REDIS_MASTER_PORT if REDIS_MASTER_PORT else os.environ.get('REDIS_PORT', '6379'), # noqa: E501 'DB': int(os.environ.get('REDIS_DB', 0)), 'PASSWORD': os.environ.get('REDIS_PASSWORD', ''), # timeout for executing commands From 0a8b2f4046a48ab3baf5649e554400098def268d Mon Sep 17 00:00:00 2001 From: Marek Bleschke Date: Wed, 9 May 2018 11:06:18 +0200 Subject: [PATCH 2/3] code style fixses from review --- src/ralph/settings/base.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ralph/settings/base.py b/src/ralph/settings/base.py index 0d4a7dd4ff..eafbd4150f 100644 --- a/src/ralph/settings/base.py +++ b/src/ralph/settings/base.py @@ -290,7 +290,7 @@ def str_to_bool(s: str) -> bool: REDIS_MASTER_IP = None REDIS_MASTER_PORT = None -REDIS_SENTINEL_ENABLED = os.environ.get('REDIS_SENTINEL_ENABLED', 'false').lower() == 'true' # noqa: E501 +REDIS_SENTINEL_ENABLED = bool_from_env('REDIS_SENTINEL_ENABLED', False) if REDIS_SENTINEL_ENABLED: from redis.sentinel import Sentinel @@ -307,8 +307,8 @@ def str_to_bool(s: str) -> bool: ) REDIS_CONNECTION = { - 'HOST': REDIS_MASTER_IP if REDIS_MASTER_IP else os.environ.get('REDIS_HOST', 'localhost'), # noqa: E501 - 'PORT': REDIS_MASTER_PORT if REDIS_MASTER_PORT else os.environ.get('REDIS_PORT', '6379'), # noqa: E501 + 'HOST': REDIS_MASTER_IP or os.environ.get('REDIS_HOST', 'localhost'), + 'PORT': REDIS_MASTER_PORT or os.environ.get('REDIS_PORT', '6379'), 'DB': int(os.environ.get('REDIS_DB', 0)), 'PASSWORD': os.environ.get('REDIS_PASSWORD', ''), # timeout for executing commands From 6718742b179b2c1d17301644ed137301bcf94d9d Mon Sep 17 00:00:00 2001 From: Marek Bleschke Date: Wed, 9 May 2018 11:40:09 +0200 Subject: [PATCH 3/3] possibility to set redis sentinel socket timout through env var --- src/ralph/settings/base.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/ralph/settings/base.py b/src/ralph/settings/base.py index eafbd4150f..966071df0f 100644 --- a/src/ralph/settings/base.py +++ b/src/ralph/settings/base.py @@ -294,13 +294,15 @@ def str_to_bool(s: str) -> bool: if REDIS_SENTINEL_ENABLED: from redis.sentinel import Sentinel - # SENTINEL_HOSTS env variable format: host_1:port;host_2:port - SENTINEL_HOSTS = os.environ['SENTINEL_HOSTS'].split(';') + # REDIS_SENTINEL_HOSTS env variable format: host_1:port;host_2:port + REDIS_SENTINEL_HOSTS = os.environ['REDIS_SENTINEL_HOSTS'].split(';') REDIS_CLUSTER_NAME = os.environ['REDIS_CLUSTER_NAME'] sentinel = Sentinel( - [tuple(s_host.split(':')) for s_host in SENTINEL_HOSTS], - socket_timeout=0.2 + [tuple(s_host.split(':')) for s_host in REDIS_SENTINEL_HOSTS], + socket_timeout=float( + os.environ.get('REDIS_SENTINEL_SOCKET_TIMEOUT', 0.2) + ) ) REDIS_MASTER_IP, REDIS_MASTER_PORT = sentinel.discover_master( REDIS_CLUSTER_NAME