Skip to content
This repository has been archived by the owner on Sep 16, 2022. It is now read-only.

Commit

Permalink
Merge pull request #314 from CSCfi/CSCETSIN-61-refactor-redis-config
Browse files Browse the repository at this point in the history
Cscetsin 61 refactor redis config
  • Loading branch information
junsk1 authored Aug 1, 2018
2 parents e81fbfc + 99d50ab commit 0d0ac82
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1445,6 +1445,9 @@
"$ref":"#/definitions/Document"
}
},
"required":[
"access_type"
],
"additionalProperties": false
},
"StructuredIdentifier":{
Expand Down
10 changes: 6 additions & 4 deletions src/metax_api/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,11 +337,8 @@

if not executing_in_travis:
# settings for custom redis-py cache helper in utils/redis.py
REDIS_SENTINEL = {
# at least three are required
'HOSTS': app_config_dict['REDIS']['HOSTS'],
REDIS = {
'PASSWORD': app_config_dict['REDIS']['PASSWORD'],
'SERVICE': app_config_dict['REDIS']['SERVICE'],
'LOCALHOST_PORT': app_config_dict['REDIS']['LOCALHOST_PORT'],

# https://github.com/andymccurdy/redis-py/issues/485#issuecomment-44555664
Expand All @@ -352,6 +349,11 @@

# enables extra logging to console during cache usage
'DEBUG': False,

'SENTINEL': {
'HOSTS': app_config_dict['REDIS']['SENTINEL']['HOSTS'],
'SERVICE': app_config_dict['REDIS']['SENTINEL']['SERVICE']
}
}

if executing_in_travis:
Expand Down
28 changes: 15 additions & 13 deletions src/metax_api/utils/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,21 @@ def __init__(self, db=0, master_only=False, settings=django_settings):
settings: override redis settings in settings.py. easier to use class from outside context of django (i.e. cron)
"""
if not isinstance(settings, dict):
if hasattr(settings, 'REDIS_SENTINEL'):
settings = settings.REDIS_SENTINEL
if hasattr(settings, 'REDIS'):
settings = settings.REDIS
else:
raise Exception('Missing configuration from settings.py: REDIS_SENTINEL')

if not settings.get('HOSTS', None):
raise Exception('Missing configuration from settings for REDIS_SENTINEL: HOSTS')
if not settings.get('SERVICE', None):
raise Exception('Missing configuration from settings for REDIS_SENTINEL: SERVICE')
raise Exception('Missing configuration from settings.py: REDIS')

if not settings.get('SENTINEL', None):
raise Exception('Missing configuration from settings for REDIS: SENTINEL')
if not settings['SENTINEL'].get('HOSTS', None):
raise Exception('Missing configuration from settings for REDIS.SENTINEL: HOSTS')
if not settings['SENTINEL'].get('SERVICE', None):
raise Exception('Missing configuration from settings for REDIS.SENTINEL: SERVICE')
if not settings.get('TEST_DB', None):
raise Exception('Missing configuration from settings for REDIS_SENTINEL: TEST_DB')
if len(settings['HOSTS']) < 3:
raise Exception('Invalid configuration in settings for REDIS_SENTINEL: HOSTS minimum number of hosts is 3')
raise Exception('Missing configuration from settings for REDIS: TEST_DB')
if len(settings['SENTINEL']['HOSTS']) < 3:
raise Exception('Invalid configuration in settings for REDIS.SENTINEL: HOSTS minimum number of hosts is 3')

if executing_test_case():
db = settings['TEST_DB']
Expand All @@ -72,13 +74,13 @@ def __init__(self, db=0, master_only=False, settings=django_settings):
)

self._sentinel = Sentinel(
settings['HOSTS'],
settings['SENTINEL']['HOSTS'],
password=settings['PASSWORD'],
socket_timeout=settings.get('SOCKET_TIMEOUT', 0.1),
db=db
)

self._service_name = settings['SERVICE']
self._service_name = settings['SENTINEL']['SERVICE']
self._DEBUG = settings.get('DEBUG', False)
self._read_from_master_only = master_only
self._node_count = self._count_nodes()
Expand Down

0 comments on commit 0d0ac82

Please sign in to comment.