33 lines
935 B
Python
33 lines
935 B
Python
import asyncio
|
|
|
|
from fastapi import APIRouter, Request
|
|
|
|
from app.core.database import check_database
|
|
from app.core.redis import check_redis
|
|
from app.errors.exceptions import unavailable
|
|
from app.schemas.base import Health
|
|
|
|
router = APIRouter(prefix="/health", tags=["health"])
|
|
|
|
|
|
@router.get("/live")
|
|
async def liveness() -> Health:
|
|
return Health(status="ok")
|
|
|
|
|
|
@router.get("/ready")
|
|
async def readiness(request: Request) -> Health:
|
|
if request.app.state.lifecycle.draining:
|
|
raise unavailable()
|
|
try:
|
|
async with asyncio.timeout(request.app.state.settings.readiness_timeout_seconds):
|
|
dependencies = await asyncio.gather(
|
|
check_database(request.app.state.engine),
|
|
check_redis(request.app.state.redis),
|
|
)
|
|
except TimeoutError:
|
|
raise unavailable() from None
|
|
if not all(dependencies):
|
|
raise unavailable()
|
|
return Health(status="ok")
|