Skip to content

Commit

Permalink
Health check api endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Levijatan committed Dec 13, 2023
1 parent c1b9412 commit 769332c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions trix/project/default/default_urls.py
Expand Up @@ -12,5 +12,6 @@
re_path(r'^admin/', admin.site.urls),
re_path(r'^c/', include('trix.trix_course.urls')),
re_path(r'^a/', include(CrAdminInstance.urls())),
re_path(r'^trix_core/', include('trix.trix_core.urls')),
re_path(r'^', include('trix.trix_student.urls')),
]
31 changes: 31 additions & 0 deletions trix/trix_core/api.py
@@ -0,0 +1,31 @@
from django.db import connection
from django.http import HttpResponse
from django.views import View


class ReadyCheck(View):
"""
Is the application ready to accept connections?
Checks if it's possible to fetch from the database. This endpoint
should only be called until it returns 200.
NOTE: If it's needed to continously check if the application is alive,
use the "trix_core/_api/application-state/alive" endpoint.
Returns 200 OK response if ready, otherwise 503 is returned.
"""
def get(self, request, *args, **kwargs):
with connection.cursor() as cursor:
cursor.execute("SELECT 1")
one = cursor.fetchone()[0]
if one != 1:
return HttpResponse(status=503)
return HttpResponse()


class LiveCheck(View):
"""
Check if application is "alive".
Returns 200 response.
"""
def get(self, request, *args, **kwargs):
return HttpResponse()
8 changes: 8 additions & 0 deletions trix/trix_core/urls.py
@@ -0,0 +1,8 @@
from django.urls import re_path

from .api import ReadyCheck, LiveCheck

urlpatterns = [
re_path('_api/application-state/ready', ReadyCheck.as_view(), name='trix_core_application_state_ready'),
re_path('_api/application-state/alive', LiveCheck.as_view(), name='trix_core_application_state_alive'),
]

0 comments on commit 769332c

Please sign in to comment.