forked from mtusi2026/mtusi2026lab2
37 lines
1.2 KiB
Python
37 lines
1.2 KiB
Python
from fastapi import APIRouter, Depends, Request, status
|
|
|
|
from domain import HealthSchema
|
|
from domain.errors import UnavailableError
|
|
from infrastructure.dependencies import DatabasePoolManager, get_database
|
|
|
|
health_router = APIRouter(prefix="/health", tags=["Health"])
|
|
|
|
|
|
@health_router.get(
|
|
"/live",
|
|
status_code=status.HTTP_200_OK,
|
|
response_model=HealthSchema,
|
|
)
|
|
async def liveness() -> HealthSchema:
|
|
"""Процесс жив и обрабатывает хттп"""
|
|
return HealthSchema()
|
|
|
|
|
|
@health_router.get(
|
|
"/ready",
|
|
status_code=status.HTTP_200_OK,
|
|
response_model=HealthSchema,
|
|
)
|
|
async def readiness(
|
|
request: Request, database: DatabasePoolManager = Depends(get_database)
|
|
) -> HealthSchema:
|
|
"""
|
|
Приложение не завершается и PostgreSQL отвечает. Valkey не критичен:
|
|
без него сессии читаются из PostgreSQL, поэтому он не влияет на готовность.
|
|
"""
|
|
if not getattr(request.app.state, "is_ready", False):
|
|
raise UnavailableError()
|
|
if not await database.check():
|
|
raise UnavailableError("бд недоступна")
|
|
return HealthSchema()
|