Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit f0e4395

Browse files
authored
Add database connectivity to healthcheck endpoint (#1083)
1 parent 624df07 commit f0e4395

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

api/catalog/api/views/health_views.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from django.conf import settings
2+
from django.db import connection
23
from rest_framework import status
34
from rest_framework.exceptions import APIException
45
from rest_framework.request import Request
@@ -21,19 +22,33 @@ class HealthCheck(APIView):
2122

2223
swagger_schema = None
2324

24-
def _check_es(self) -> Response | None:
25-
"""Check ES cluster health and raise an exception if ES is not healthy."""
25+
@staticmethod
26+
def _check_db() -> None:
27+
"""
28+
Check that the database is available.
2629
30+
Returns nothing if everything is OK, throws error otherwise.
31+
"""
32+
connection.ensure_connection()
33+
34+
@staticmethod
35+
def _check_es() -> None:
36+
"""
37+
Check Elasticsearch cluster health.
38+
39+
Raises an exception if ES is not healthy.
40+
"""
2741
es_health = settings.ES.cluster.health(timeout="5s")
2842

2943
if es_health["timed_out"]:
3044
raise ElasticsearchHealthcheckException("es_timed_out")
3145

32-
if (status := es_health["status"]) != "green":
33-
raise ElasticsearchHealthcheckException(f"es_status_{status}")
46+
if (es_status := es_health["status"]) != "green":
47+
raise ElasticsearchHealthcheckException(f"es_status_{es_status}")
3448

3549
def get(self, request: Request):
3650
if "check_es" in request.query_params:
3751
self._check_es()
52+
self._check_db()
3853

3954
return Response({"status": "200 OK"}, status=200)

api/test/unit/views/health_views_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from unittest import mock
2+
13
import pook
24
import pytest
35

@@ -16,11 +18,21 @@ def mock_health_response(status="green", timed_out=False):
1618
)
1719

1820

21+
@pytest.mark.django_db
1922
def test_health_check_plain(api_client):
2023
res = api_client.get("/healthcheck/")
2124
assert res.status_code == 200
2225

2326

27+
def test_health_check_calls__check_db(api_client):
28+
with mock.patch(
29+
"catalog.api.views.health_views.HealthCheck._check_db"
30+
) as mock_check_db:
31+
res = api_client.get("/healthcheck/")
32+
assert res.status_code == 200
33+
mock_check_db.assert_called_once()
34+
35+
2436
def test_health_check_es_timed_out(api_client):
2537
mock_health_response(timed_out=True)
2638
pook.on()
@@ -42,6 +54,7 @@ def test_health_check_es_status_bad(status, api_client):
4254
assert res.json()["detail"] == f"es_status_{status}"
4355

4456

57+
@pytest.mark.django_db
4558
def test_health_check_es_all_good(api_client):
4659
mock_health_response(status="green")
4760
pook.on()

0 commit comments

Comments
 (0)