-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Richard O'Dwyer
committed
Jan 6, 2024
1 parent
4da1fd9
commit 714a912
Showing
3 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import django | ||
|
||
if django.VERSION < (3, 2): | ||
default_app_config = "health_check.contrib.redis_sentinel.apps.HealthCheckConfig" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from django.apps import AppConfig | ||
|
||
from health_check.plugins import plugin_dir | ||
|
||
|
||
class HealthCheckConfig(AppConfig): | ||
name = "health_check.contrib.redis_sentinel" | ||
|
||
def ready(self): | ||
from .backends import RedisSentinelHealthCheck | ||
|
||
plugin_dir.register(RedisSentinelHealthCheck) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import logging | ||
|
||
from django.conf import settings | ||
from redis import exceptions, Sentinel | ||
|
||
from health_check.backends import BaseHealthCheckBackend | ||
from health_check.exceptions import ServiceUnavailable | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class RedisSentinelHealthCheck(BaseHealthCheckBackend): | ||
"""Health check for Redis Sentinel.""" | ||
|
||
redis_sentinels = getattr(settings, 'REDIS_SENTINELS', ()) | ||
redis_master_name = getattr(settings, 'REDIS_MASTER_NAME', 'mymaster') | ||
redis_sentinels_healthcheck_socket_timeout = getattr( | ||
settings, 'REDIS_SENTINELS_HEALTHCHECK_SOCKET_TIMEOUT', 1000 | ||
) | ||
|
||
def check_status(self, subset=None): | ||
"""Check Redis service by pinging the redis instance with a redis connection.""" | ||
logger.debug( | ||
'Got %s as the redis_sentinels. Connecting to redis...', | ||
self.redis_sentinels, | ||
) | ||
|
||
logger.debug('Attempting to connect to redis...') | ||
try: | ||
client = self._get_redis_client( | ||
sentinels=self.redis_sentinels, | ||
master_name=self.redis_master_name, | ||
timeout=self.redis_sentinels_healthcheck_socket_timeout, | ||
) | ||
client.ping() | ||
except ConnectionRefusedError as e: | ||
self.add_error( | ||
ServiceUnavailable( | ||
'Unable to connect to Redis: Connection was refused.' | ||
), | ||
e, | ||
) | ||
except exceptions.TimeoutError as e: | ||
self.add_error( | ||
ServiceUnavailable('Unable to connect to Redis: Timeout.'), e | ||
) | ||
except exceptions.ConnectionError as e: | ||
self.add_error( | ||
ServiceUnavailable( | ||
'Unable to connect to Redis: Connection Error' | ||
), | ||
e, | ||
) | ||
except BaseException as e: | ||
self.add_error(ServiceUnavailable('Unknown error'), e) | ||
else: | ||
logger.debug('Connection established. Redis is healthy.') | ||
|
||
@staticmethod | ||
def _get_redis_client(sentinels, master_name, timeout): | ||
sentinel = Sentinel(sentinels, socket_timeout=timeout) | ||
return sentinel.master_for(master_name, socket_timeout=timeout) |