From 561b2aed2798e6a4ae33cd19166055e991fd174d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Sun, 17 Nov 2024 19:10:28 +0100 Subject: [PATCH] Add the /healthcheck path which returns an HTTP200 OK response for checking the helath of the container after startup --- Dockerfile | 2 +- compose.yaml | 6 ++++++ shimatta_kenkyusho/parts/urls.py | 1 + shimatta_kenkyusho/parts/views.py | 12 +++++++++++- 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index f9ba421..80c50d1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ FROM alpine:latest -RUN apk add --no-cache python3 py3-pip python3-dev py3-setuptools gcc python3-dev jpeg-dev zlib-dev musl-dev py3-gunicorn +RUN apk add --no-cache python3 py3-pip python3-dev py3-setuptools gcc python3-dev jpeg-dev zlib-dev musl-dev py3-gunicorn curl COPY . /home/shimatta/kenkyusho WORKDIR /home/shimatta/kenkyusho RUN python3 -m venv /home/shimatta/kenkyusho/.venv && . /home/shimatta/kenkyusho/.venv/bin/activate && pip install -r requirements.txt diff --git a/compose.yaml b/compose.yaml index 806ca0c..b9c9bb4 100644 --- a/compose.yaml +++ b/compose.yaml @@ -22,6 +22,12 @@ services: depends_on: shimatta-kenkyusho-db: condition: service_healthy + healthcheck: + test: ["CMD-SHELL", "curl -f localhost:8000/healthcheck"] + interval: 5s + timeout: 5s + retries: 5 + start_period: 30s shimatta-kenkyusho-db: image: postgres:16.5-alpine diff --git a/shimatta_kenkyusho/parts/urls.py b/shimatta_kenkyusho/parts/urls.py index a693f79..8e9fd1b 100644 --- a/shimatta_kenkyusho/parts/urls.py +++ b/shimatta_kenkyusho/parts/urls.py @@ -16,4 +16,5 @@ urlpatterns = [ path('distributors//', parts_views.DistributorDetailView.as_view(), name='parts-distributors-detail'), path('manufacturers/', parts_views.ManufacturersViewSet.as_view(), name='parts-manufacturers'), path("manufacturers//", parts_views.ManufacturerDetailViewSet.as_view(), name='parts-manufacturers-detail'), + path("healthcheck/", parts_views.health_check_view, name='parts-health-check'), ] diff --git a/shimatta_kenkyusho/parts/views.py b/shimatta_kenkyusho/parts/views.py index 6de8672..64a4b57 100644 --- a/shimatta_kenkyusho/parts/views.py +++ b/shimatta_kenkyusho/parts/views.py @@ -18,8 +18,10 @@ from .forms import * from django.db.models import Q from django.db.models.functions import Lower from django.forms import formset_factory +from django.http import HttpResponse import uuid + ParameterSearchFormSet = formset_factory(ComponentParameterSearchForm, extra=1) class QrSearchForm(forms.Form): @@ -953,4 +955,12 @@ class ManufacturerDetailViewSet(LoginRequiredMixin, BaseTemplateMixin, DetailVie elif 'submit-manufacturer-edit' in request.POST: return self.edit_manufacturer(request) - return super().post(request, *args, **kwargs) \ No newline at end of file + return super().post(request, *args, **kwargs) + + +def health_check_view(request) -> HttpResponse: + """ + Health checking view. Returns empty http response with HTTP status OK. + This will be used to check + """ + return HttpResponse(status=200) \ No newline at end of file