From d9abd87aadad06b74d9f4a07ad0cd73dd21597df Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Tue, 7 Oct 2025 15:10:47 -0700 Subject: [PATCH] try to lazy init db another way --- app/__init__.py | 18 ++++++++++-------- tests/app/dao/test_annual_billing_dao.py | 4 +--- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 6e571a0c0..8451df6f8 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -88,9 +88,10 @@ encryption = None zendesk_client = None redis_store = RedisClient() document_download_client = None +notification_provider_clients = None -notification_provider_clients = NotificationProviderClients() - +# LocalProxy doesn't evaluate the target immediately, but defers +# resolution to runtime. So there is no monkeypatching concern. api_user = LocalProxy(lambda: g.api_user) authenticated_service = LocalProxy(lambda: g.authenticated_service) @@ -181,10 +182,12 @@ def create_app(application): logging.init_app(application) # start lazy initialization for gevent + # NOTE: notify_celery and redis_store are safe to construct here + # because all entry points (gunicorn_entry.py, run_celery.py) apply + # monkey.patch_all() first. + # Do NOT access or use them before create_app() is called and don't + # call create_app() in multiple places. - # Set db engine settings here for now. - # They were not being set previous (despite environmental variables with appropriate - # sounding names) and were defaulting to low values db = SQLAlchemy( engine_options={ "pool_size": config.Config.SQLALCHEMY_POOL_SIZE, @@ -213,9 +216,7 @@ def create_app(application): aws_sns_client.init_app(application) encryption = Encryption() encryption.init_app(application) - - # end lazy initialization - + notification_provider_clients = NotificationProviderClients() # If a stub url is provided for SES, then use the stub client rather than the real SES boto client email_clients = ( [aws_ses_stub_client] @@ -225,6 +226,7 @@ def create_app(application): notification_provider_clients.init_app( sms_clients=[aws_sns_client], email_clients=email_clients ) + # end lazy initialization notify_celery.init_app(application) redis_store.init_app(application) diff --git a/tests/app/dao/test_annual_billing_dao.py b/tests/app/dao/test_annual_billing_dao.py index f585ab614..72a7d3a3a 100644 --- a/tests/app/dao/test_annual_billing_dao.py +++ b/tests/app/dao/test_annual_billing_dao.py @@ -2,8 +2,7 @@ import pytest from freezegun import freeze_time from sqlalchemy import select -import application -from app import create_app, db +from app import db from app.dao.annual_billing_dao import ( dao_create_or_update_annual_billing_for_year, dao_get_all_free_sms_fragment_limit, @@ -19,7 +18,6 @@ from tests.app.db import create_annual_billing, create_service def test_dao_update_free_sms_fragment_limit(notify_db_session, sample_service): - create_app(application) new_limit = 9999 year = get_current_calendar_year_start_year() dao_create_or_update_annual_billing_for_year(sample_service.id, new_limit, year)