Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

settings for redis sentinel #3266

Merged
merged 3 commits into from
May 10, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/ralph/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use str_to_bool here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

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(';')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider naming it REDIS_SENTINEL_HOSTS

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

REDIS_CLUSTER_NAME = os.environ['REDIS_CLUSTER_NAME']

sentinel = Sentinel(
[tuple(s_host.split(':')) for s_host in SENTINEL_HOSTS],
socket_timeout=0.2
Copy link
Contributor

@romcheg romcheg May 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be better allow to parameterize this timeout as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

)
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not REDIS_MASTER_IP or os.environ.get('REDIS_HOST', 'localhost')?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right, or looks better

'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
Expand Down