Skip to content

Commit

Permalink
Update heath check
Browse files Browse the repository at this point in the history
  • Loading branch information
sudan45 committed Nov 8, 2024
1 parent f5ba320 commit 6d99474
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 34 deletions.
8 changes: 8 additions & 0 deletions deep/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,14 @@
'drf_yasg', # API Documentation
'graphene_django',
'graphene_graphiql_explorer',
# heath check
'health_check',
'health_check.db', # stock Django health checkers
'health_check.cache',
'health_check.storage',
'health_check.contrib.migrations',
'health_check.contrib.celery', # requires celery
'health_check.contrib.celery_ping', # requires celery
] + [
'{}.{}.apps.{}Config'.format(
APPS_DIR.split('/')[-1],
Expand Down
21 changes: 0 additions & 21 deletions deep/tests/test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@
)

import datetime
from django.urls import reverse
from django.test import override_settings
from django.utils import timezone
from django.conf import settings

from unittest.mock import patch


from deep.middleware import _set_current_request as _set_middleware_current_request
from user.models import User
Expand Down Expand Up @@ -358,21 +355,3 @@ def get_aware_datetime(self, *args, **kwargs):

def get_aware_datetime_str(self, *args, **kwargs):
return self.get_datetime_str(self.get_aware_datetime(*args, **kwargs))


class HealthCheckViewTests(test.APITestCase):
@patch('deep.views.current_app.control.ping')
def test_health_check_healthy(self, mock_ping):
mock_ping.return_value = [{'celery@worker1': {'ok': 'pong'}}]

response = self.client.get(reverse('celery_health_check'))
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data, {"status": "healthy"})

@patch('deep.views.current_app.control.ping')
def test_health_check_unhealthy(self, mock_ping):
mock_ping.return_value = []

response = self.client.get(reverse('celery_health_check'))
self.assertEqual(response.status_code, status.HTTP_503_SERVICE_UNAVAILABLE)
self.assertEqual(response.data, {"status": "unhealthy"})
4 changes: 2 additions & 2 deletions deep/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@
PasswordChanged,
get_frontend_url,
graphql_docs,
HealthCheckView,
)
from organization.views import (
OrganizationViewSet,
Expand Down Expand Up @@ -620,6 +619,8 @@ def get_api_path(path):
name="favicon"),

re_path('ses-bounce/?$', ses_bounce_handler_view, name='ses_bounce'),
re_path(r'ht/', include('health_check.urls')),

] + [
# graphql patterns
re_path('^graphql/?$', csrf_exempt(CustomGraphQLView.as_view())),
Expand Down Expand Up @@ -648,7 +649,6 @@ def get_api_path(path):
re_path(r'^ec-email/$', EntryCommentEmail.as_view()),
re_path(r'^erc-email/$', EntryReviewCommentEmail.as_view()),
re_path(r'^render-debug/$', RenderChart.as_view()),
re_path(r'celery-health/$', HealthCheckView.as_view(), name='celery_health_check'),

]

Expand Down
9 changes: 0 additions & 9 deletions deep/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
status,
response,
)
from celery import current_app

from django.core.exceptions import PermissionDenied as DjPermissionDenied
from django.views.decorators.clickjacking import xframe_options_exempt
Expand Down Expand Up @@ -279,11 +278,3 @@ def format_error(error):
extensions['errorCode'] = str(status.HTTP_500_INTERNAL_SERVER_ERROR)
formatted_error['extensions'] = extensions
return formatted_error


class HealthCheckView(views.APIView):
def get(self, request):
active_worker = current_app.control.ping()
if active_worker:
return response.Response({"status": "healthy"}, status=200)
return response.Response({"status": "unhealthy"}, status=503)
23 changes: 21 additions & 2 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 @@ -72,6 +72,7 @@ django-environ = "^0.8.1"
uwsgi = "*"
django-admin-autocomplete-filter = "^0.7.1"
aws-sns-message-validator = "0.0.5 "
django-health-check = "^3.18.3"

# Thumbnail
# Shapely = "==1.7.1"
Expand Down

0 comments on commit 6d99474

Please sign in to comment.