Skip to content

Commit

Permalink
Features/448 health check v2 (#489)
Browse files Browse the repository at this point in the history
* Convert REDIS_USE_SSL into a boolean

* Update main health check to throw 503's if mysql or redis (optional) is down.

* Debug

* Remove debug, and fix env -> bool conversion

* Fix tests

* Remove print
  • Loading branch information
MelissaAutumn authored Jun 27, 2024
1 parent efa014a commit e5eb6ef
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 8 deletions.
4 changes: 2 additions & 2 deletions backend/.env.test
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# -- GENERAL --
# Logging level: DEBUG|INFO|WARNING|ERROR|CRITICAL
LOG_LEVEL=ERROR
LOG_LEVEL=DEBUG
LOG_USE_STREAM=1

# -- FRONTEND --
Expand Down Expand Up @@ -90,7 +90,7 @@ GOOGLE_TEST_PASS=
TEST_USER_EMAIL=[email protected]

# -- Redis --
REDIS_URL=localhost
REDIS_URL
REDIS_PORT=6379
REDIS_DB=0
# No value = Python None
Expand Down
2 changes: 1 addition & 1 deletion backend/src/appointment/dependencies/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def get_redis() -> Redis | RedisCluster | None:
port = int(os.getenv('REDIS_PORT'))
db = os.getenv('REDIS_DB')
password = os.getenv('REDIS_PASSWORD')
ssl = os.getenv('REDIS_USE_SSL')
ssl = True if os.getenv('REDIS_USE_SSL') and (os.getenv('REDIS_USE_SSL').lower() == 'true' or os.getenv('REDIS_USE_SSL').lower() == '1') else False

if os.getenv('REDIS_USE_CLUSTER'):
return RedisCluster(
Expand Down
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
3 changes: 2 additions & 1 deletion backend/src/appointment/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@

def _common_setup():
# load any available .env into env
load_dotenv()
if os.getenv('APP_ENV') != APP_ENV_TEST:
load_dotenv()

# This needs to be ran before any other imports
normalize_secrets()
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
2 changes: 1 addition & 1 deletion backend/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from factory.invite_factory import make_invite # noqa: F401

# Load our env
load_dotenv(find_dotenv('.env.test'))
load_dotenv(find_dotenv('.env.test'), override=True)

from appointment.main import server # noqa: E402
from appointment.database import models, repo, schemas # noqa: E402
Expand Down

0 comments on commit e5eb6ef

Please sign in to comment.