fix
This commit is contained in:
parent
0ce6e263e5
commit
82261671a1
|
|
@ -8,12 +8,14 @@ import logging
|
||||||
import time
|
import time
|
||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
from typing import Dict, Any
|
from typing import Dict, Any
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
from fastapi import FastAPI, Request, HTTPException
|
from fastapi import FastAPI, Request, HTTPException
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
from fastapi.middleware.trustedhost import TrustedHostMiddleware
|
from fastapi.middleware.trustedhost import TrustedHostMiddleware
|
||||||
from fastapi.responses import JSONResponse
|
from fastapi.responses import JSONResponse
|
||||||
from fastapi.exceptions import RequestValidationError
|
from fastapi.exceptions import RequestValidationError
|
||||||
|
from sqlalchemy import text
|
||||||
import uvicorn
|
import uvicorn
|
||||||
|
|
||||||
# Импорт компонентов приложения
|
# Импорт компонентов приложения
|
||||||
|
|
@ -318,6 +320,63 @@ app = create_fastapi_app()
|
||||||
|
|
||||||
# Дополнительные корневые эндпоинты для совместимости
|
# Дополнительные корневые эндпоинты для совместимости
|
||||||
|
|
||||||
|
@app.get("/health")
|
||||||
|
async def simple_health_check():
|
||||||
|
"""
|
||||||
|
Простой health check endpoint для совместимости со скриптами
|
||||||
|
Дублирует функциональность /api/system/health
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
# Проверяем подключение к базе данных
|
||||||
|
db_status = "healthy"
|
||||||
|
try:
|
||||||
|
async with db_manager.get_session() as session:
|
||||||
|
await session.execute(text("SELECT 1"))
|
||||||
|
except Exception:
|
||||||
|
db_status = "unhealthy"
|
||||||
|
|
||||||
|
# Проверяем кэш
|
||||||
|
cache_status = "healthy"
|
||||||
|
try:
|
||||||
|
cache_manager = await get_cache_manager()
|
||||||
|
await cache_manager.set("health_check", "ok", ttl=10)
|
||||||
|
except Exception:
|
||||||
|
cache_status = "unhealthy"
|
||||||
|
|
||||||
|
# Определяем общий статус
|
||||||
|
overall_status = "healthy"
|
||||||
|
if db_status == "unhealthy" or cache_status == "unhealthy":
|
||||||
|
overall_status = "unhealthy"
|
||||||
|
|
||||||
|
health_data = {
|
||||||
|
"status": overall_status,
|
||||||
|
"timestamp": datetime.utcnow().isoformat(),
|
||||||
|
"database": db_status,
|
||||||
|
"cache": cache_status,
|
||||||
|
"uptime_seconds": int(time.time() - _app_start_time)
|
||||||
|
}
|
||||||
|
|
||||||
|
# Возвращаем статус с соответствующим HTTP кодом
|
||||||
|
status_code = 200 if overall_status == "healthy" else 503
|
||||||
|
|
||||||
|
return JSONResponse(
|
||||||
|
content=health_data,
|
||||||
|
status_code=status_code
|
||||||
|
)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger = get_logger(__name__)
|
||||||
|
await logger.aerror("Simple health check failed", error=str(e))
|
||||||
|
return JSONResponse(
|
||||||
|
content={
|
||||||
|
"status": "unhealthy",
|
||||||
|
"error": "Health check system failure",
|
||||||
|
"timestamp": datetime.utcnow().isoformat()
|
||||||
|
},
|
||||||
|
status_code=503
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@app.get("/")
|
@app.get("/")
|
||||||
async def root():
|
async def root():
|
||||||
"""Корневой эндпоинт"""
|
"""Корневой эндпоинт"""
|
||||||
|
|
@ -328,7 +387,7 @@ async def root():
|
||||||
"status": "running",
|
"status": "running",
|
||||||
"uptime_seconds": int(time.time() - _app_start_time),
|
"uptime_seconds": int(time.time() - _app_start_time),
|
||||||
"api_docs": "/docs",
|
"api_docs": "/docs",
|
||||||
"health_check": "/api/system/health"
|
"health_check": "/health"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue