Files
group_fqcd_jr/app/service/health_service.py
T

44 lines
1.4 KiB
Python
Raw Normal View History

2026-09-09 21:55:37 +08:00
from typing import Any
from sqlalchemy import text
2026-09-09 23:40:35 +08:00
from app.core.config import get_settings
2026-09-09 21:55:37 +08:00
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
2026-09-09 23:40:35 +08:00
checks["redis"] = await self._redis_ready()
# Milvus remains an optional projection; its dedicated adapter reports degraded reads.
2026-09-09 21:55:37 +08:00
checks["milvus"] = True
return {"status": "ready" if all(checks.values()) else "degraded", "checks": checks}
2026-09-09 23:40:35 +08:00
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()
2026-09-09 23:40:35 +08:00
except Exception:
pass