28 lines
868 B
Python
28 lines
868 B
Python
"""健康检查:服务存活与数据库连通性。"""
|
|
|
|
from django.db import connection
|
|
from django.db.utils import OperationalError
|
|
from rest_framework.decorators import api_view, permission_classes
|
|
from rest_framework.permissions import AllowAny
|
|
from rest_framework.response import Response
|
|
|
|
|
|
@api_view(['GET', 'HEAD'])
|
|
@permission_classes([AllowAny])
|
|
def ping(request):
|
|
"""服务存活探测,通则返回 ok。"""
|
|
return Response('ok')
|
|
|
|
|
|
@api_view(['GET', 'HEAD'])
|
|
@permission_classes([AllowAny])
|
|
def test_mysql(request):
|
|
"""MySQL 连通探测:使用 Django 连接池建立连接,通则 ok,否则返回没连通。"""
|
|
try:
|
|
connection.ensure_connection()
|
|
except OperationalError:
|
|
return Response('没连通', status=503)
|
|
except Exception:
|
|
return Response('没连通', status=503)
|
|
return Response('ok')
|