From 82261671a14e8e110dcf4b48d0a8fe35cb48b51f Mon Sep 17 00:00:00 2001 From: user Date: Mon, 28 Jul 2025 09:26:43 +0300 Subject: [PATCH] fix --- app/fastapi_main.py | 61 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/app/fastapi_main.py b/app/fastapi_main.py index 2644a81..5557bb3 100644 --- a/app/fastapi_main.py +++ b/app/fastapi_main.py @@ -8,12 +8,14 @@ import logging import time from contextlib import asynccontextmanager from typing import Dict, Any +from datetime import datetime from fastapi import FastAPI, Request, HTTPException from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.trustedhost import TrustedHostMiddleware from fastapi.responses import JSONResponse from fastapi.exceptions import RequestValidationError +from sqlalchemy import text 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("/") async def root(): """Корневой эндпоинт""" @@ -328,7 +387,7 @@ async def root(): "status": "running", "uptime_seconds": int(time.time() - _app_start_time), "api_docs": "/docs", - "health_check": "/api/system/health" + "health_check": "/health" }