-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
658f6f6
commit f7610fe
Showing
3 changed files
with
59 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import logging | ||
|
||
from django.http import HttpResponse, HttpResponseServerError | ||
|
||
logger = logging.getLogger("healthz") | ||
|
||
|
||
class HealthCheckMiddleware: | ||
def __init__(self, get_response): | ||
self.get_response = get_response | ||
# One-time configuration and initialization. | ||
|
||
def __call__(self, request): | ||
if request.method == "GET": | ||
if request.path == "/readiness/": | ||
return self.readiness(request) | ||
elif request.path == "/healthz/": | ||
return self.healthz(request) | ||
return self.get_response(request) | ||
|
||
def healthz(self, request): | ||
""" | ||
Returns that the server is alive. | ||
""" | ||
return HttpResponse("OK") | ||
|
||
def readiness(self, request): | ||
# Connect to each database and do a generic standard SQL query | ||
# that doesn't write any data and doesn't depend on any tables | ||
# being present. | ||
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.") | ||
|
||
# Call get_stats() to connect to each memcached instance and get it's stats. | ||
# This can effectively check if each is online. | ||
try: | ||
from django.core.cache import caches | ||
from django.core.cache.backends.memcached import BaseMemcachedCache | ||
for cache in caches.all(): | ||
if isinstance(cache, BaseMemcachedCache): | ||
stats = cache._cache.get_stats() | ||
if len(stats) != len(cache._servers): | ||
return HttpResponseServerError("cache: cannot connect to cache.") | ||
except Exception as e: | ||
logger.exception(e) | ||
return HttpResponseServerError("cache: cannot connect to cache.") | ||
|
||
return HttpResponse("OK") |
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