chore(prod): startup migrations, readiness checks and backend healthcheck
- add backend entrypoint that can run alembic upgrade head on startup - add RUN_MIGRATIONS_ON_STARTUP setting and compose wiring - add /health/live and /health/ready endpoints with db+redis checks - add backend container healthcheck against readiness endpoint - document readiness and startup migration behavior
This commit is contained in:
@@ -8,6 +8,7 @@ class Settings(BaseSettings):
|
||||
debug: bool = True
|
||||
api_v1_prefix: str = "/api/v1"
|
||||
auto_create_tables: bool = True
|
||||
run_migrations_on_startup: bool = False
|
||||
|
||||
secret_key: str = Field(default="change-me-please-12345", min_length=16)
|
||||
access_token_expire_minutes: int = 30
|
||||
|
||||
35
app/main.py
35
app/main.py
@@ -1,6 +1,7 @@
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi import FastAPI, HTTPException, status
|
||||
from sqlalchemy import text
|
||||
|
||||
from app.auth.router import router as auth_router
|
||||
from app.chats.router import router as chats_router
|
||||
@@ -14,7 +15,7 @@ from app.notifications.router import router as notifications_router
|
||||
from app.realtime.router import router as realtime_router
|
||||
from app.realtime.service import realtime_gateway
|
||||
from app.users.router import router as users_router
|
||||
from app.utils.redis_client import close_redis_client
|
||||
from app.utils.redis_client import close_redis_client, get_redis_client
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
@@ -36,6 +37,36 @@ async def health() -> dict[str, str]:
|
||||
return {"status": "ok"}
|
||||
|
||||
|
||||
@app.get("/health/live", tags=["health"])
|
||||
async def health_live() -> dict[str, str]:
|
||||
return {"status": "ok"}
|
||||
|
||||
|
||||
@app.get("/health/ready", tags=["health"])
|
||||
async def health_ready() -> dict[str, str]:
|
||||
db_ok = False
|
||||
redis_ok = False
|
||||
try:
|
||||
async with engine.connect() as conn:
|
||||
await conn.execute(text("SELECT 1"))
|
||||
db_ok = True
|
||||
except Exception:
|
||||
db_ok = False
|
||||
try:
|
||||
redis = get_redis_client()
|
||||
pong = await redis.ping()
|
||||
redis_ok = bool(pong)
|
||||
except Exception:
|
||||
redis_ok = False
|
||||
|
||||
if not db_ok or not redis_ok:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||
detail={"status": "not_ready", "db": db_ok, "redis": redis_ok},
|
||||
)
|
||||
return {"status": "ready", "db": "ok", "redis": "ok"}
|
||||
|
||||
|
||||
app.include_router(auth_router, prefix=settings.api_v1_prefix)
|
||||
app.include_router(users_router, prefix=settings.api_v1_prefix)
|
||||
app.include_router(chats_router, prefix=settings.api_v1_prefix)
|
||||
|
||||
Reference in New Issue
Block a user