feat: better healthcheck

This commit is contained in:
swve 2024-11-23 20:52:24 +01:00
parent 6cd1cf7e9c
commit 46f016f661
8 changed files with 112 additions and 9 deletions

View file

@ -1,5 +1,6 @@
import os
from fastapi import APIRouter, Depends
from src.routers import health
from src.routers import usergroups
from src.routers import dev, trail, users, auth, orgs, roles
from src.routers.ai import ai
@ -42,6 +43,8 @@ if os.environ.get("CLOUD_INTERNAL_KEY"):
dependencies=[Depends(cloud_internal.check_internal_cloud_key)],
)
v1_router.include_router(health.router, prefix="/health", tags=["health"])
# Dev Routes
v1_router.include_router(
dev.router,

View file

@ -0,0 +1,11 @@
from fastapi import Depends, APIRouter
from sqlmodel import Session
from src.services.health.health import check_health
from src.core.events.database import get_db_session
router = APIRouter()
@router.get("")
async def health(db_session: Session = Depends(get_db_session)):
return await check_health(db_session)

View file

View file

@ -0,0 +1,21 @@
from fastapi import HTTPException
from sqlmodel import Session, select
from src.db.organizations import Organization
async def check_database_health(db_session: Session) -> bool:
statement = select(Organization)
result = db_session.exec(statement)
if not result:
return False
return True
async def check_health(db_session: Session) -> bool:
# Check database health
database_healthy = await check_database_health(db_session)
if not database_healthy:
raise HTTPException(status_code=503, detail="Database is not healthy")
return True