Refactor: Relocate bootstrap and validation logic into lifespan context manager

This commit is contained in:
Scott Idem
2026-01-15 17:10:42 -05:00
parent 3f276a42e1
commit 5ece1d34e3

View File

@@ -1,4 +1,4 @@
import datetime, json, os, pytz, random, secrets # , uvicorn import datetime, json, os, pytz, random, secrets, contextlib # , uvicorn
from enum import Enum from enum import Enum
#from datetime import datetime, time, timedelta #from datetime import datetime, time, timedelta
@@ -34,23 +34,16 @@ log = logging.getLogger(__name__)
#format='[%(asctime)s] %(levelname)s @ %(module)s.%(funcName)s()#%(lineno)d: %(message)s' #format='[%(asctime)s] %(levelname)s @ %(module)s.%(funcName)s()#%(lineno)d: %(message)s'
#) #)
@contextlib.asynccontextmanager
print('### **** *** ** * Aether API v4 using FastAPI - About to try FastAPI() while loading... * ** *** **** ###') async def lifespan(app: FastAPI):
app = FastAPI( """
# debug = True, Handles application startup and shutdown lifecycle.
title = 'Aether API', """
description = 'One Sky IT\'s Aether API v4 using FastAPI.',
version = '4.9.0',
operationsSorter = 'method',
)
log.setLevel(logging.INFO) log.setLevel(logging.INFO)
# log.debug(config.settings) log.info('### **** *** ** * Aether API v4 using FastAPI - Startup Lifespan Initiated * ** *** **** ###')
print('### **** *** ** * Aether API v4 using FastAPI - Bootstrapping Configuration... * ** *** **** ###') # 1. Bootstrapping Configuration from DB with robust error handling
log.info("Bootstrapping Configuration...")
# Sync settings from DB with robust error handling
try: try:
if bootstrap_db_config(config.settings): if bootstrap_db_config(config.settings):
log.info("Successfully bootstrapped configuration from database.") log.info("Successfully bootstrapped configuration from database.")
@@ -64,11 +57,28 @@ try:
except Exception as e: except Exception as e:
log.error(f"Unexpected error during configuration bootstrap: {e}. Falling back to .env settings.") log.error(f"Unexpected error during configuration bootstrap: {e}. Falling back to .env settings.")
# Perform final validation of critical infrastructure # 2. Final validation of critical infrastructure
validate_critical_config(config.settings) validate_critical_config(config.settings)
print('### **** *** ** * Aether API v4 using FastAPI - Finished Configuration Phase * ** *** **** ###' ) log.info('### **** *** ** * Aether API v4 using FastAPI - Startup Sequence Complete * ** *** **** ###')
log.debug(config.settings)
yield
# Shutdown logic
log.info('### **** *** ** * Aether API v4 using FastAPI - Shutdown Lifespan Initiated * ** *** **** ###')
log.info('The Aether FastAPI API is shutting down...')
print('### **** *** ** * Aether API v4 using FastAPI - About to try FastAPI() while loading... * ** *** **** ###')
app = FastAPI(
# debug = True,
title = 'Aether API',
description = 'One Sky IT\'s Aether API v4 using FastAPI.',
version = '4.9.0',
operationsSorter = 'method',
lifespan = lifespan,
)
# @lru_cache() # @lru_cache()
# def get_settings(): # def get_settings():