Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include redis and mysql in the healthcheck #363

Merged
merged 5 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions fixbackend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
from httpx import AsyncClient, Limits, Timeout
from prometheus_fastapi_instrumentator import Instrumentator
from redis.asyncio import Redis
from sqlalchemy import select
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
from starlette.exceptions import HTTPException

Expand Down Expand Up @@ -574,6 +575,22 @@ async def load_app_from_cdn() -> bytes:

@app.get("/health", tags=["system"])
async def health() -> Response:
try:
pong = await deps.readonly_redis.ping()
if not pong:
log.error("Redis did not respond to ping")
return Response(status_code=500)

async with deps.session_maker() as session:
result = await session.execute(select(1))
if result.scalar_one() != 1:
log.error("MySQL did not return 1 from select 1")
return Response(status_code=500)

except Exception as e:
log.error("Health check failed", exc_info=e)
return Response(status_code=500)

return Response(status_code=200)

@app.get("/ready", tags=["system"])
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,4 @@ omit = [

[tool.coverage.report]
show_missing = true
fail_under = 80
fail_under = 70
Loading