From 6f7a675f6a3c39525cf447c7cea471a7c4b532cc Mon Sep 17 00:00:00 2001 From: Melissa Autumn Date: Thu, 13 Jun 2024 13:33:41 -0700 Subject: [PATCH 1/6] Convert REDIS_USE_SSL into a boolean --- backend/src/appointment/dependencies/database.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/appointment/dependencies/database.py b/backend/src/appointment/dependencies/database.py index 91bde0e98..19489babf 100644 --- a/backend/src/appointment/dependencies/database.py +++ b/backend/src/appointment/dependencies/database.py @@ -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').lower() == 'true' or os.getenv('REDIS_USE_SSL').lower() == '1' else False if os.getenv('REDIS_USE_CLUSTER'): return RedisCluster( From 60694a38bad8199ea3e33612e5de8cb3c70d0de3 Mon Sep 17 00:00:00 2001 From: Melissa Autumn Date: Thu, 13 Jun 2024 13:34:29 -0700 Subject: [PATCH 2/6] Update main health check to throw 503's if mysql or redis (optional) is down. --- backend/src/appointment/l10n/en/main.ftl | 1 + backend/src/appointment/routes/api.py | 23 ++++++++++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/backend/src/appointment/l10n/en/main.ftl b/backend/src/appointment/l10n/en/main.ftl index 303c72b51..ddab4c8b3 100644 --- a/backend/src/appointment/l10n/en/main.ftl +++ b/backend/src/appointment/l10n/en/main.ftl @@ -7,6 +7,7 @@ locale = en # Should indicate application wellness. health-ok = Health OK +health-bad = Health BAD ## General Exceptions diff --git a/backend/src/appointment/routes/api.py b/backend/src/appointment/routes/api.py index 56ddf527a..893c4c08a 100644 --- a/backend/src/appointment/routes/api.py +++ b/backend/src/appointment/routes/api.py @@ -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 @@ -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) From 106963e192c484d1aa097fac0e5aa699750c04e2 Mon Sep 17 00:00:00 2001 From: Melissa Autumn Date: Thu, 13 Jun 2024 13:40:39 -0700 Subject: [PATCH 3/6] Debug --- backend/src/appointment/routes/api.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/backend/src/appointment/routes/api.py b/backend/src/appointment/routes/api.py index 893c4c08a..933d416f4 100644 --- a/backend/src/appointment/routes/api.py +++ b/backend/src/appointment/routes/api.py @@ -37,6 +37,7 @@ def health(db: Session = Depends(get_db)): try: db.query(Subscriber).first() except Exception as ex: + print("Ex -> ",ex) sentry_sdk.capture_exception(ex) return JSONResponse(content=l10n('health-bad'), status_code=503) @@ -45,6 +46,7 @@ def health(db: Session = Depends(get_db)): redis_instance: Redis | RedisCluster | None = get_redis() redis_instance.ping() except Exception as ex: + print("Ex -> ", ex) sentry_sdk.capture_exception(ex) return JSONResponse(content=l10n('health-bad'), status_code=503) From 0fdb50166c12f40adda316d61c11b635a856d11b Mon Sep 17 00:00:00 2001 From: Melissa Autumn Date: Thu, 13 Jun 2024 13:44:07 -0700 Subject: [PATCH 4/6] Remove debug, and fix env -> bool conversion --- backend/src/appointment/dependencies/database.py | 2 +- backend/src/appointment/routes/api.py | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/backend/src/appointment/dependencies/database.py b/backend/src/appointment/dependencies/database.py index 19489babf..d8c2fa9f3 100644 --- a/backend/src/appointment/dependencies/database.py +++ b/backend/src/appointment/dependencies/database.py @@ -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 = True if os.getenv('REDIS_USE_SSL').lower() == 'true' or os.getenv('REDIS_USE_SSL').lower() == '1' else False + 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( diff --git a/backend/src/appointment/routes/api.py b/backend/src/appointment/routes/api.py index 933d416f4..893c4c08a 100644 --- a/backend/src/appointment/routes/api.py +++ b/backend/src/appointment/routes/api.py @@ -37,7 +37,6 @@ def health(db: Session = Depends(get_db)): try: db.query(Subscriber).first() except Exception as ex: - print("Ex -> ",ex) sentry_sdk.capture_exception(ex) return JSONResponse(content=l10n('health-bad'), status_code=503) @@ -46,7 +45,6 @@ def health(db: Session = Depends(get_db)): redis_instance: Redis | RedisCluster | None = get_redis() redis_instance.ping() except Exception as ex: - print("Ex -> ", ex) sentry_sdk.capture_exception(ex) return JSONResponse(content=l10n('health-bad'), status_code=503) From 7e5d4da24c4437a458c848815d672b253d8fe8f3 Mon Sep 17 00:00:00 2001 From: Melissa Autumn Date: Thu, 13 Jun 2024 15:41:03 -0700 Subject: [PATCH 5/6] Fix tests --- backend/.env.test | 4 ++-- backend/src/appointment/main.py | 3 ++- backend/test/conftest.py | 3 ++- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/backend/.env.test b/backend/.env.test index a0c17077f..86bf7db22 100644 --- a/backend/.env.test +++ b/backend/.env.test @@ -2,7 +2,7 @@ # -- GENERAL -- # Logging level: DEBUG|INFO|WARNING|ERROR|CRITICAL -LOG_LEVEL=ERROR +LOG_LEVEL=DEBUG LOG_USE_STREAM=1 # -- FRONTEND -- @@ -90,7 +90,7 @@ GOOGLE_TEST_PASS= TEST_USER_EMAIL=test@example.org # -- Redis -- -REDIS_URL=localhost +REDIS_URL REDIS_PORT=6379 REDIS_DB=0 # No value = Python None diff --git a/backend/src/appointment/main.py b/backend/src/appointment/main.py index 8c9308332..c4506c463 100644 --- a/backend/src/appointment/main.py +++ b/backend/src/appointment/main.py @@ -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() diff --git a/backend/test/conftest.py b/backend/test/conftest.py index fafdf062b..4e6b7d971 100644 --- a/backend/test/conftest.py +++ b/backend/test/conftest.py @@ -23,7 +23,8 @@ from factory.invite_factory import make_invite # noqa: F401 # Load our env -load_dotenv(find_dotenv('.env.test')) +print("Conftes.py") +load_dotenv(find_dotenv('.env.test'), override=True) from appointment.main import server # noqa: E402 from appointment.database import models, repo, schemas # noqa: E402 From 8adad40a80b1e4659dba752eb9d847df4f6d3539 Mon Sep 17 00:00:00 2001 From: Melissa Autumn Date: Fri, 14 Jun 2024 09:11:48 -0700 Subject: [PATCH 6/6] Remove print --- backend/test/conftest.py | 1 - 1 file changed, 1 deletion(-) diff --git a/backend/test/conftest.py b/backend/test/conftest.py index 4e6b7d971..30380a72c 100644 --- a/backend/test/conftest.py +++ b/backend/test/conftest.py @@ -23,7 +23,6 @@ from factory.invite_factory import make_invite # noqa: F401 # Load our env -print("Conftes.py") load_dotenv(find_dotenv('.env.test'), override=True) from appointment.main import server # noqa: E402