forked from mtusi2026/mtusi2026lab2
+ добавила функционал на 4 + тесты
This commit is contained in:
+18
-3
@@ -1,5 +1,7 @@
|
||||
from fastapi import APIRouter
|
||||
from fastapi import APIRouter, Response
|
||||
from ..models.schemas import Health
|
||||
from ..storage import db as _db
|
||||
from ..storage.sessions import _redis_client, _store
|
||||
|
||||
|
||||
router = APIRouter(tags=["health"])
|
||||
@@ -20,6 +22,19 @@ def liveness() -> Health:
|
||||
summary="Проверка готовности принимать трафик",
|
||||
response_model=Health,
|
||||
)
|
||||
def readiness() -> Health:
|
||||
"""Готов принимать трафик. В lab1 критичных зависимостей нет."""
|
||||
def readiness(response: Response) -> Health:
|
||||
"""200, если PostgreSQL и (в режиме redis) Redis доступны; иначе 503."""
|
||||
try:
|
||||
with _db.connection() as conn, conn.cursor() as cur:
|
||||
cur.execute("SELECT 1")
|
||||
cur.fetchone()
|
||||
except Exception:
|
||||
response.status_code = 503
|
||||
return Health(status="ok")
|
||||
if _store() == "redis":
|
||||
try:
|
||||
_redis_client().ping()
|
||||
except Exception:
|
||||
response.status_code = 503
|
||||
return Health(status="ok")
|
||||
return Health(status="ok")
|
||||
Reference in New Issue
Block a user