diff --git a/src/ralph/settings/base.py b/src/ralph/settings/base.py index 41f30dca76..966071df0f 100644 --- a/src/ralph/settings/base.py +++ b/src/ralph/settings/base.py @@ -287,9 +287,30 @@ def str_to_bool(s: str) -> bool: } }) +REDIS_MASTER_IP = None +REDIS_MASTER_PORT = None + +REDIS_SENTINEL_ENABLED = bool_from_env('REDIS_SENTINEL_ENABLED', False) +if REDIS_SENTINEL_ENABLED: + from redis.sentinel import Sentinel + + # 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 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 + ) + REDIS_CONNECTION = { - 'HOST': os.environ.get('REDIS_HOST', 'localhost'), - 'PORT': os.environ.get('REDIS_PORT', '6379'), + '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