Files
Nexus/server/api/health.py
T

45 lines
1.3 KiB
Python
Raw Normal View History

"""Nexus — Health Check Endpoint
External monitoring endpoint for Shell health_monitor.sh and Supervisor.
"""
from fastapi import APIRouter
from sqlalchemy import text
from server.infrastructure.database.session import AsyncSessionLocal
from server.infrastructure.redis.client import get_redis
from server.api.websocket import connected_clients
router = APIRouter()
@router.get("/health")
async def health_check():
"""Health check endpoint — used by external Shell script to verify Python is alive.
Returns:
- python: always "ok" (if this endpoint responds, Python is alive)
- redis: "ok" or "unavailable"
- mysql: "ok" or "error"
- websocket_clients: number of connected WS clients
"""
checks = {"python": "ok"}
# Redis check
try:
redis = await get_redis()
checks["redis"] = "ok" if redis else "unavailable"
except Exception:
checks["redis"] = "error"
# MySQL check
try:
async with AsyncSessionLocal() as session:
await session.execute(text("SELECT 1"))
checks["mysql"] = "ok"
except Exception:
checks["mysql"] = "error"
# WebSocket connections count
checks["websocket_clients"] = len(connected_clients)
return {"status": "ok", "checks": checks}