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

feat(Redis): Add Redis to the health check endpoint #62

Merged
merged 4 commits into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added bc/core/utils/__init__.py
Empty file.
12 changes: 12 additions & 0 deletions bc/core/utils.py → bc/core/utils/connections.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import redis
from django.db import OperationalError, connections

from .redis import make_redis_interface


def check_postgresql() -> bool:
"""Just check if we can connect to postgresql"""
Expand All @@ -11,3 +14,12 @@ def check_postgresql() -> bool:
except OperationalError:
return False
return True


def check_redis() -> bool:
r = make_redis_interface("QUEUE")
try:
r.ping()
except (redis.exceptions.ConnectionError, ConnectionRefusedError):
return False
return True
26 changes: 26 additions & 0 deletions bc/core/utils/redis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from typing import Literal, cast

from django.conf import settings
from redis import Redis


def make_redis_interface(
db_name: str,
decode_responses: bool = True,
) -> Redis:
"""Create a redis connection object

:param db_name: The name of the database to use, as defined in our settings
:param decode_responses: Whether to decode responses with utf-8. If you're
putting binary data (like a picked object) into redis, don't try to decode
it.
:return Redis interface using django settings
"""

return Redis.from_url(
url=f"{settings.REDIS_HOST}:{settings.REDIS_PORT}",
db=int(settings.REDIS_DATABASES[db_name]),
decode_responses=cast(
Literal[True] | Literal[False], decode_responses
),
)
7 changes: 4 additions & 3 deletions bc/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.http import HttpRequest, HttpResponse, JsonResponse

from .tasks import fail_task
from .utils import check_postgresql
from .utils.connections import check_postgresql, check_redis


def sentry_fail(request: HttpRequest) -> HttpResponse:
Expand All @@ -13,15 +13,16 @@ def sentry_fail(request: HttpRequest) -> HttpResponse:

def health_check(request: HttpRequest) -> JsonResponse:
"""Check if we can connect to various services."""

is_redis_up = check_redis()
is_postgresql_up = check_postgresql()

status = HTTPStatus.OK
if not is_postgresql_up:
if not all([is_postgresql_up, is_redis_up]):
status = HTTPStatus.INTERNAL_SERVER_ERROR

return JsonResponse(
{
"is_redis_up": is_redis_up,
"is_postgresql_up": is_postgresql_up,
},
status=status,
Expand Down
31 changes: 31 additions & 0 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pre-commit = "^3.0.4"
flynt = "^0.77"
django-stubs = "^1.14.0"
types-requests = "^2.28.11.8"
types-redis = "^4.4.0.6"

[tool.black]
include = '''.*\.pyi?$'''
Expand Down