Created
March 26, 2020 23:38
-
-
Save eon01/34e4ac7fb4c2751e35a0300435a10165 to your computer and use it in GitHub Desktop.
A simple view to create a /readiness for Kubernetes probes. Example based on connectivity with DB. It can be customized.
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
# views.py | |
# Example inspired by: https://www.ianlewis.org/en/kubernetes-health-checks-django | |
def readiness(request): | |
try: | |
from django.db import connections | |
for name in connections: | |
cursor = connections[name].cursor() | |
cursor.execute("SELECT 1;") | |
row = cursor.fetchone() | |
if row is None: | |
return HttpResponseServerError("db: invalid response") | |
except Exception as e: | |
logger.exception(e) | |
return HttpResponseServerError("db: cannot connect to database.") | |
response = HttpResponse() | |
response.status_code = 200 | |
response.write("OK") | |
return response | |
# urls.py | |
from app.views import readiness | |
urlpatterns += [ | |
url(r'^readiness/$', readiness, name = 'readiness'), | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment