功能变化: 新增存活检测功能
parent
d42e54b1c2
commit
e4432ea920
|
@ -62,6 +62,7 @@ INSTALLED_APPS = [
|
|||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
"dvadmin.utils.middleware.HealthCheckMiddleware",
|
||||
"django.middleware.security.SecurityMiddleware",
|
||||
"whitenoise.middleware.WhiteNoiseMiddleware",
|
||||
"django.contrib.sessions.middleware.SessionMiddleware",
|
||||
|
|
|
@ -2,9 +2,11 @@
|
|||
日志 django中间件
|
||||
"""
|
||||
import json
|
||||
import logging
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.models import AnonymousUser
|
||||
from django.http import HttpResponse, HttpResponseServerError
|
||||
from django.utils.deprecation import MiddlewareMixin
|
||||
|
||||
from dvadmin.system.models import OperationLog
|
||||
|
@ -87,3 +89,58 @@ class ApiLoggingMiddleware(MiddlewareMixin):
|
|||
if self.methods == 'ALL' or request.method in self.methods:
|
||||
self.__handle_response(request, response)
|
||||
return response
|
||||
|
||||
logger = logging.getLogger("healthz")
|
||||
class HealthCheckMiddleware(object):
|
||||
"""
|
||||
存活检查中间件
|
||||
"""
|
||||
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")
|
||||
|
|
Loading…
Reference in New Issue