diff --git a/app/__init__.py b/app/__init__.py index 7704e7375..2361199c2 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -99,7 +99,7 @@ aws_sns_client = AwsSnsClient() aws_cloudwatch_client = AwsCloudwatchClient() aws_pinpoint_client = AwsPinpointClient() encryption = Encryption() -zendesk_client = ZendeskClient() +zendesk_client = None redis_store = RedisClient() document_download_client = DocumentDownloadClient() @@ -118,6 +118,16 @@ api_user = LocalProxy(lambda: g.api_user) authenticated_service = LocalProxy(lambda: g.authenticated_service) +def get_zendesk_client(): + global zendesk_client + # Our unit tests mock anyway + if os.environ.get("NOTIFY_ENVIRONMENT") == "test": + return None + if zendesk_client is None: + raise RuntimeError(f"Celery not initialized zendesk_client: {zendesk_client}") + return zendesk_client + + def create_app(application): from app.config import configs @@ -136,7 +146,6 @@ def create_app(application): request_helper.init_app(application) db.init_app(application) migrate.init_app(application, db=db) - zendesk_client.init_app(application) logging.init_app(application) aws_sns_client.init_app(application) @@ -144,6 +153,12 @@ def create_app(application): aws_ses_stub_client.init_app(stub_url=application.config["SES_STUB_URL"]) aws_cloudwatch_client.init_app(application) aws_pinpoint_client.init_app(application) + + # start lazy initialization for gevent + zendesk_client = ZendeskClient() + zendesk_client.init_app(application) + # end lazy initialization + # 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] diff --git a/app/celery/scheduled_tasks.py b/app/celery/scheduled_tasks.py index 05d4f72a7..a8a097030 100644 --- a/app/celery/scheduled_tasks.py +++ b/app/celery/scheduled_tasks.py @@ -5,7 +5,7 @@ from flask import current_app from sqlalchemy import between, select, union from sqlalchemy.exc import SQLAlchemyError -from app import db, notify_celery, redis_store, zendesk_client +from app import db, get_zendesk_client, notify_celery, redis_store from app.celery.tasks import ( get_recipient_csv_and_template_and_sender_id, process_incomplete_jobs, @@ -44,6 +44,8 @@ from notifications_utils.clients.zendesk.zendesk_client import NotifySupportTick MAX_NOTIFICATION_FAILS = 10000 +zendesk_client = get_zendesk_client() + @notify_celery.task(name="run-scheduled-jobs") def run_scheduled_jobs(): diff --git a/setup.cfg b/setup.cfg index f6dc999cb..c3af9dc69 100644 --- a/setup.cfg +++ b/setup.cfg @@ -6,7 +6,7 @@ xfail_strict=true exclude = venv*,__pycache__,node_modules,cache,migrations,build,sample_cap_xml_documents.py max-line-length = 120 # W504 line break after binary operator -extend_ignore=B306, W504, E203 +extend_ignore=B306, W504, E203, F824 [isort] profile = black