Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Levijatan
committed
Dec 13, 2023
1 parent
c1b9412
commit 769332c
Showing
3 changed files
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'), | ||
] |