Files
mtusi2026lab5/src/app/routes/health.py
T
2026-11-20 11:53:35 +03:00

42 lines
1.2 KiB
Python

from fastapi import APIRouter, Request
from fastapi.responses import JSONResponse
import socket
from urllib.parse import urlparse
from ..config import get_settings
from ..db import connection
from ..errors import error_response
from ..s3 import get_s3_client
router = APIRouter()
@router.get("/health/live")
def live():
return {"status": "ok"}
@router.get("/health/ready")
def ready(request: Request):
try:
with connection() as conn:
with conn.cursor() as cur:
cur.execute("SELECT 1")
cur.fetchone()
get_s3_client().head_bucket(Bucket=get_settings().s3_bucket)
# 3. Broker (простая проверка TCP-порта)
settings = get_settings()
parsed = urlparse(settings.broker_url)
host = parsed.hostname or "broker"
port = parsed.port or 5672
with socket.create_connection((host, port), timeout=2):
pass
except Exception as e:
print(f"Readiness check failed: {e}")
return error_response(
request, 503, "unavailable", "Зависимость недоступна"
)
return {"status": "ok"}