Skip to content

Commit

Permalink
Merge pull request #261 from uktrade/LTD-4964-dbt-healthcheck
Browse files Browse the repository at this point in the history
 LTD-4964-dbt-healthcheck
  • Loading branch information
depsiatwal authored Jun 27, 2024
2 parents af09bb9 + 74682c4 commit 7127ea3
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 0 deletions.
3 changes: 3 additions & 0 deletions conf/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
from django.contrib import admin
from django.urls import include, path

from healthcheck.views import HealthCheckPingdomView

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"),
]

if settings.ENABLE_MOCK_HMRC_SERVICE: # pragma: no cover
Expand Down
7 changes: 7 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ services:
- 8000
command: pipenv run ./manage.py runserver 0.0.0.0:8000

healthcheck:
test: [ "CMD-SHELL", "python -m dbt_copilot_python.celery_health_check.healthcheck" ]
interval: 10s
timeout: 5s
retries: 2
start_period: 5s

mailhog:
ports:
- 8025:8025 # HTTP
Expand Down
65 changes: 65 additions & 0 deletions healthcheck/tests/test_healthcheck.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import pytest

from django.urls import reverse

from health_check.backends import BaseHealthCheckBackend
from health_check.exceptions import HealthCheckException
from health_check.plugins import plugin_dir


@pytest.fixture(autouse=True)
def backends():
unaltered_value = plugin_dir._registry
yield plugin_dir
plugin_dir._registry = unaltered_value


class HealthCheckOk(BaseHealthCheckBackend):
def check_status(self):
return True

def run_check(self):
super().run_check()
self.time_taken = 0.33


class HealthCheckBroken(BaseHealthCheckBackend):
def check_status(self):
raise HealthCheckException("error")

def run_check(self):
super().run_check()
self.time_taken = 0.23


def test_healthcheck_ok(client, backends):
backends.reset()
backends.register(HealthCheckOk)
url = reverse("healthcheck-pingdom")
response = client.get(url)
assert response.status_code == 200
assert response.render().content == (
b'<?xml version="1.0" encoding="UTF-8"?>'
b"<pingdom_http_custom_check><status>\n \n "
b"OK\n \n "
b"</status>"
b"<response_time>0.33</response_time>"
b"</pingdom_http_custom_check>\n"
)


def test_healthcheck_down(client, backends):
backends.reset()
backends.register(HealthCheckBroken)
url = reverse("healthcheck-pingdom")
response = client.get(url)

assert response.status_code == 500
assert response.render().content == (
b'<?xml version="1.0" encoding="UTF-8"?>'
b"<pingdom_http_custom_check>"
b"<status>\n \n "
b"HealthCheckBroken: unknown error: error\n \n "
b"</status><response_time>0.23</response_time>"
b"</pingdom_http_custom_check>\n"
)
10 changes: 10 additions & 0 deletions healthcheck/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from health_check.views import MainView


class HealthCheckPingdomView(MainView):
template_name = "pingdom.xml"

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")
13 changes: 13 additions & 0 deletions templates/pingdom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% spaceless %}
<?xml version="1.0" encoding="UTF-8"?>
<pingdom_http_custom_check>
<status>
{% for plugin in errored_plugins %}
{{ plugin.identifier }}: {{ plugin.pretty_status }}
{% empty %}
OK
{% endfor %}
</status>
<response_time>{{ total_response_time }}</response_time>
</pingdom_http_custom_check>
{% endspaceless %}

0 comments on commit 7127ea3

Please sign in to comment.