Files
lincodex/ml/src/lincodex_ml/api/health.py
T

40 lines
1.3 KiB
Python

from fastapi import APIRouter, Request, Response, status
from lincodex_ml.api.models import MLHealthResponse, MLReadinessResponse
from lincodex_ml.inference.runtime import EmbeddingRuntime, RuntimeState
router = APIRouter(tags=["health"])
@router.get(
"/healthz",
operation_id="GetMLHealthz",
response_model=MLHealthResponse,
status_code=status.HTTP_200_OK,
summary="Check whether the ML process is responding.",
)
async def get_healthz() -> MLHealthResponse:
return MLHealthResponse(status="ok")
@router.get(
"/readyz",
operation_id="GetMLReadyz",
response_model=MLReadinessResponse,
responses={
status.HTTP_503_SERVICE_UNAVAILABLE: {
"model": MLReadinessResponse,
"description": "No inference runtime is available.",
}
},
summary="Check whether an inference runtime is available.",
)
async def get_readyz(request: Request, response: Response) -> MLReadinessResponse:
# Ready only after the model is loaded and a warm-up encode finished;
# a disabled or failed runtime stays not ready.
runtime: EmbeddingRuntime = request.app.state.embedding_runtime
if runtime.state is RuntimeState.READY:
return MLReadinessResponse(status="ready")
response.status_code = status.HTTP_503_SERVICE_UNAVAILABLE
return MLReadinessResponse(status="not_ready")