Skip to content

Commit

Permalink
OpenConceptLab/ocl_issues#927 Redis clustering, fixing redis healthcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
rkorytkowski committed Jul 25, 2023
1 parent a07ae87 commit 027d394
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
28 changes: 27 additions & 1 deletion core/common/healthcheck/healthcheck.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from celery.app import default_app as celery_app
from django.conf import settings
from health_check.backends import BaseHealthCheckBackend
from django_redis import get_redis_connection
from health_check.backends import BaseHealthCheckBackend, logger
from health_check.contrib.redis.backends import RedisHealthCheck
from health_check.exceptions import ServiceReturnedUnexpectedResult, ServiceUnavailable
from pydash import get
from redis import exceptions

from core.common.utils import flower_get, es_get

Expand All @@ -18,6 +21,29 @@ def check_status(self):
raise NotImplementedError


class RedisHealthCheck(BaseHealthCheck):

def check_status(self):
"""Check Redis service by pinging the redis instance with a redis connection."""
try:
# conn is used as a context to release opened resources later
with get_redis_connection('default') as conn:
conn.ping() # exceptions may be raised upon 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.")

def identifier(self):
return "Redis"


class FlowerHealthCheck(BaseHealthCheck):
critical_service = False

Expand Down
3 changes: 1 addition & 2 deletions core/common/healthcheck/views.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from health_check.contrib.redis.backends import RedisHealthCheck
from health_check.db.backends import DatabaseBackend
from health_check.views import MainView

from core.common.healthcheck.healthcheck import FlowerHealthCheck, CeleryDefaultQueueHealthCheck, \
CeleryBulkImport0QueueHealthCheck, CeleryBulkImportRootQueueHealthCheck, CeleryBulkImport3QueueHealthCheck, \
CeleryBulkImport2QueueHealthCheck, CeleryBulkImport1QueueHealthCheck, CeleryConcurrentThreadsHealthCheck, \
ESHealthCheck, CeleryIndexingQueueHealthCheck
ESHealthCheck, CeleryIndexingQueueHealthCheck, RedisHealthCheck


class BaseHealthcheckView(MainView):
Expand Down
1 change: 0 additions & 1 deletion core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@
'health_check', # required
'health_check.db', # stock Django health checkers
# 'health_check.contrib.celery_ping', # requires celery
'health_check.contrib.redis', # requires Redis broker
'core.common.apps.CommonConfig',
'core.users',
'core.orgs',
Expand Down

0 comments on commit 027d394

Please sign in to comment.