Skip to content

Commit

Permalink
Merge pull request #3266 from MarekBleschke/feature/redis_sentinel_cofig
Browse files Browse the repository at this point in the history
settings for redis sentinel
  • Loading branch information
MarekBleschke committed May 10, 2018
2 parents b1fb06b + 6718742 commit a7479ca
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/ralph/settings/base.py
Expand Up @@ -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
Expand Down

0 comments on commit a7479ca

Please sign in to comment.