1
0
forked from erp-dev/erp

feat: disabled sse module and added health check to api_v2 for caddy2

This commit is contained in:
2025-12-27 09:04:12 +08:00
parent 67698dfa71
commit 69c3daa919
5 changed files with 103 additions and 48 deletions

39
api_v2/views/healthy.py Normal file
View File

@@ -0,0 +1,39 @@
import logging
import os
from django.db import connections, transaction
from rest_framework.response import Response
from rest_framework.views import APIView
logger = logging.getLogger(__name__)
class HealthCheckView(APIView):
"""
GET /api/v2/health/
仅做最轻量的 DB 查询SELECT 1
若失败(含超时):直接退出当前进程,让容器重启。
"""
authentication_classes: list = []
permission_classes: list = []
# 单条查询超时(毫秒)。避免 health check 卡死占用 worker。
STATEMENT_TIMEOUT_MS = 2000
def get(self, request):
try:
# 用事务包住 SET LOCAL确保 statement_timeout 只对本次请求生效
with transaction.atomic(using="default"):
with connections["default"].cursor() as cursor:
cursor.execute("SET LOCAL statement_timeout = %s;", [self.STATEMENT_TIMEOUT_MS])
cursor.execute("SELECT 1;")
cursor.fetchone()
except Exception:
logger.exception("api_v2 health check failed; exiting process")
os._exit(1) # 等价于 Go 的 os.Exit(1):立即退出进程
return Response({"status": "ok"})