Skip to content

Commit

Permalink
Update main health check to throw 503's if mysql or redis (optional) …
Browse files Browse the repository at this point in the history
…is down.
  • Loading branch information
MelissaAutumn committed Jun 13, 2024
1 parent 6f7a675 commit 60694a3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions backend/src/appointment/l10n/en/main.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ locale = en

# Should indicate application wellness.
health-ok = Health OK
health-bad = Health BAD
## General Exceptions

Expand Down
23 changes: 20 additions & 3 deletions backend/src/appointment/routes/api.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import enum
import os
import secrets
from enum import Enum

import requests.exceptions
import sentry_sdk
from redis import Redis, RedisCluster

# database
from sqlalchemy.orm import Session
from starlette.responses import HTMLResponse
from starlette.responses import HTMLResponse, JSONResponse

from .. import utils
from ..database import repo, schemas
Expand All @@ -29,9 +32,23 @@


@router.get('/')
def health():
def health(db: Session = Depends(get_db)):
"""Small route with no processing that will be used for health checks"""
return l10n('health-ok')
try:
db.query(Subscriber).first()
except Exception as ex:
sentry_sdk.capture_exception(ex)
return JSONResponse(content=l10n('health-bad'), status_code=503)

if os.getenv('REDIS_URL'):
try:
redis_instance: Redis | RedisCluster | None = get_redis()
redis_instance.ping()
except Exception as ex:
sentry_sdk.capture_exception(ex)
return JSONResponse(content=l10n('health-bad'), status_code=503)

return JSONResponse(l10n('health-ok'), status_code=200)


@router.put('/me', response_model=schemas.SubscriberBase)
Expand Down

0 comments on commit 60694a3

Please sign in to comment.