Files
mtusi2026lab5/src/application/api/health/endpoints.py
T

50 lines
1.7 KiB
Python

from fastapi import APIRouter, Depends, Request, status
from domain import HealthSchema
from domain.errors import UnavailableError
from infrastructure.dependencies import (
DatabasePoolManager,
ObjectStorageManager,
get_database,
get_storage,
)
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),
storage: ObjectStorageManager = Depends(get_storage),
) -> HealthSchema:
"""
Приложение не завершается, PostgreSQL и S3 отвечают: с lab4 загрузка
и скачивание вложений входят в обязательный синхронный путь.
Valkey не критичен: без него сессии читаются из PostgreSQL. Без S3
недоступны только вложения — nginx не исключает реплику по readiness,
поэтому сообщения продолжают работать, а реплика честно сообщает 503.
"""
if not getattr(request.app.state, "is_ready", False):
raise UnavailableError()
if not await database.check():
raise UnavailableError("бд недоступна")
if not await storage.check():
raise UnavailableError("хранилище файлов недоступно")
return HealthSchema()