from typing import Any from sqlalchemy import text from app.core.config import get_settings from app.infrastructure.db import SessionFactory class HealthService: async def ready(self) -> dict[str, Any]: checks: dict[str, Any] = {} try: async with SessionFactory() as session: await session.execute(text("SELECT 1")) checks["mysql"] = True except Exception: checks["mysql"] = False checks["redis"] = await self._redis_ready() # Milvus remains an optional projection; its dedicated adapter reports degraded reads. checks["milvus"] = True return {"status": "ready" if all(checks.values()) else "degraded", "checks": checks} async def _redis_ready(self) -> bool: client = None try: from redis.asyncio import Redis settings = get_settings() client = Redis.from_url( settings.redis_url, socket_connect_timeout=settings.redis_connect_timeout_seconds, socket_timeout=settings.redis_connect_timeout_seconds, decode_responses=True, ) return bool(await client.ping()) except Exception: return False finally: if client is not None: try: await client.close() except Exception: pass