""" Health check endpoint for Docker orchestration. Verifies DB and Redis connectivity. """ import logging from fastapi import APIRouter from fastapi.responses import JSONResponse from sqlalchemy import text log = logging.getLogger(__name__) router = APIRouter() @router.get('/health', tags=['Root'], include_in_schema=True) async def health_check(): """ Checks liveness of the DB and Redis connections. Returns 200 if both are reachable, 503 otherwise. """ status = {'db': False, 'redis': False} # --- DB check --- try: from app.lib_sql_core import engine with engine.connect() as conn: conn.execute(text('SELECT 1')) status['db'] = True except Exception as e: log.error(f'Health check: DB ping failed: {e}') # --- Redis check --- try: from app.lib_redis_helpers import redis_client redis_client.ping() status['redis'] = True except Exception as e: log.error(f'Health check: Redis ping failed: {e}') all_ok = all(status.values()) http_status = 200 if all_ok else 503 return JSONResponse(content={'status': 'ok' if all_ok else 'degraded', **status}, status_code=http_status)