Skip to content

Commit

Permalink
Merge pull request #283 from uktrade/add-simple-health-check
Browse files Browse the repository at this point in the history
LTD-5658 Add simple health check
  • Loading branch information
markj0hnst0n authored Nov 22, 2024
2 parents ffcb02d + 9824e57 commit b6c9665
Show file tree
Hide file tree
Showing 7 changed files with 780 additions and 716 deletions.
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ psycopg2-binary = "~=2.9.3"
setuptools = "~=70.0.0"
celery = "~=5.3.0"
redis = "~=4.0.2"
django-health-check = "~=3.17.0"
django-health-check = "~=3.18.1"
database-sanitizer = ">=1.1.0"
factory-boy = "~=2.12.0"
boto3 = "~=1.26.17"
Expand Down
1,437 changes: 732 additions & 705 deletions Pipfile.lock

Large diffs are not rendered by default.

13 changes: 5 additions & 8 deletions conf/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,16 @@
"django.contrib.staticfiles",
"mail",
"health_check",
"health_check.db",
"health_check.cache",
"health_check.storage",
"health_check.contrib.celery",
"health_check.contrib.celery_ping",
"django_db_anonymiser.db_anonymiser",
"healthcheck",
"health_check.contrib.migrations",
]

if not IS_ENV_DBT_PLATFORM:
INSTALLED_APPS += [
"health_check.db",
"health_check.cache",
"health_check.storage",
"health_check.contrib.celery",
"health_check.contrib.celery_ping",
]

MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
Expand Down
3 changes: 2 additions & 1 deletion conf/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
from django.contrib import admin
from django.urls import include, path

from healthcheck.views import HealthCheckPingdomView
from healthcheck.views import HealthCheckPingdomView, ServiceAvailableHealthCheckView

urlpatterns = [
path("admin/", admin.site.urls),
path("mail/", include("mail.urls")),
path("healthcheck/", include("health_check.urls")),
path("pingdom/ping.xml", HealthCheckPingdomView.as_view(), name="healthcheck-pingdom"),
path("service-available-check/", ServiceAvailableHealthCheckView.as_view(), name="service-available-check"),
]

if settings.ENABLE_MOCK_HMRC_SERVICE: # pragma: no cover
Expand Down
6 changes: 5 additions & 1 deletion healthcheck/tests/test_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
from health_check.exceptions import HealthCheckException
from parameterized import parameterized

from healthcheck.checks import LicencePayloadsHealthCheck, MailboxAuthenticationHealthCheck, PendingMailHealthCheck
from healthcheck.checks import (
LicencePayloadsHealthCheck,
MailboxAuthenticationHealthCheck,
PendingMailHealthCheck,
)
from mail.enums import LicenceActionEnum, ReceptionStatusEnum
from mail.models import LicencePayload, Mail

Expand Down
25 changes: 25 additions & 0 deletions healthcheck/tests/test_healthcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,28 @@ def test_healthcheck_down(client, backends):
b"</status><response_time>0.23</response_time>"
b"</pingdom_http_custom_check>\n"
)


"""
The tests below expect a 200 response whether healthchecks produce a healthy
response or not as the url is used by DBT platform pipeline to check that
the django app is alive.
"""


def test_service_available_check_broken(client, backends):
backends.reset()
backends.register(HealthCheckBroken)
url = reverse("service-available-check")
response = client.get(url)

assert response.status_code == 200


def test_service_available_check_ok(client, backends):
backends.reset()
backends.register(HealthCheckOk)
url = reverse("service-available-check")
response = client.get(url)

assert response.status_code == 200
10 changes: 10 additions & 0 deletions healthcheck/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from health_check.views import MainView
from django.http import HttpResponse
from rest_framework import status


class HealthCheckPingdomView(MainView):
Expand All @@ -8,3 +10,11 @@ def render_to_response(self, context, status):
context["errored_plugins"] = [plugin for plugin in context["plugins"] if plugin.errors]
context["total_response_time"] = sum([plugin.time_taken for plugin in context["plugins"]])
return super().render_to_response(context=context, status=status, content_type="text/xml")


class ServiceAvailableHealthCheckView(MainView):
def get(self, request, *args, **kwargs):
return self.render_to_response()

def render_to_response(self):
return HttpResponse(status.HTTP_200_OK)

0 comments on commit b6c9665

Please sign in to comment.