From 76b5928e198b9ecad21a072fbd343b7586cf4bc9 Mon Sep 17 00:00:00 2001 From: Peter Schutt Date: Thu, 12 Jan 2023 10:57:24 +1000 Subject: [PATCH] feat(app): toggle service readiness checks via environment. Closes #219 --- .env.example | 2 ++ src/starlite_saqlalchemy/lifespan.py | 8 +++++--- src/starlite_saqlalchemy/settings.py | 4 ++++ 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/.env.example b/.env.example index 86ec042e..c4bf9754 100644 --- a/.env.example +++ b/.env.example @@ -1,5 +1,7 @@ # App BUILD_NUMBER= +CHECK_DB_READY=true +CHECK_REDIS_READY=true DEBUG=true ENVIRONMENT=local NAME=my-starlite-app diff --git a/src/starlite_saqlalchemy/lifespan.py b/src/starlite_saqlalchemy/lifespan.py index 5c0c253a..cebb251b 100644 --- a/src/starlite_saqlalchemy/lifespan.py +++ b/src/starlite_saqlalchemy/lifespan.py @@ -6,7 +6,7 @@ import starlite from sqlalchemy import text -from starlite_saqlalchemy import redis +from starlite_saqlalchemy import redis, settings from starlite_saqlalchemy.db import engine logger = logging.getLogger(__name__) @@ -41,5 +41,7 @@ async def _redis_ready() -> None: async def before_startup_handler(_: starlite.Starlite) -> None: """Do things before the app starts up.""" - await _db_ready() - await _redis_ready() + if settings.app.CHECK_DB_READY: + await _db_ready() + if settings.app.CHECK_REDIS_READY: + await _redis_ready() diff --git a/src/starlite_saqlalchemy/settings.py b/src/starlite_saqlalchemy/settings.py index 4c8e75df..466bf899 100644 --- a/src/starlite_saqlalchemy/settings.py +++ b/src/starlite_saqlalchemy/settings.py @@ -27,6 +27,10 @@ class Config: BUILD_NUMBER: str = "" """Identifier for CI build.""" + CHECK_DB_READY: bool = True + """Check for database readiness on startup.""" + CHECK_REDIS_READY: bool = True + """Check for redis readiness on startup.""" DEBUG: bool = False """Run `Starlite` with `debug=True`.""" ENVIRONMENT: str = "prod"