From 927319c428d340e6d203d0374c96b7a1d7303b97 Mon Sep 17 00:00:00 2001 From: Andrei Neagu <5694077+GitHK@users.noreply.github.com> Date: Fri, 8 Nov 2024 07:16:48 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Refactor=20storage=20setup=20fun?= =?UTF-8?q?ctions=20to=20avoid=20errors=20(#6686)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Andrei Neagu --- .../storage/src/simcore_service_storage/s3.py | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/services/storage/src/simcore_service_storage/s3.py b/services/storage/src/simcore_service_storage/s3.py index 8814db3757a..f5e07f5c6ea 100644 --- a/services/storage/src/simcore_service_storage/s3.py +++ b/services/storage/src/simcore_service_storage/s3.py @@ -21,12 +21,13 @@ async def setup_s3_client(app) -> AsyncGenerator[None, None]: - with log_context(log, logging.DEBUG, msg=f"setup {__name__}.setup.cleanup_ctx"): + client = None + + with log_context(log, logging.DEBUG, msg="setup.s3_client.cleanup_ctx"): storage_settings: Settings = app[APP_CONFIG_KEY] storage_s3_settings = storage_settings.STORAGE_S3 assert storage_s3_settings # nosec - client = None async for attempt in AsyncRetrying( wait=wait_fixed(RETRY_WAIT_SECS), before_sleep=before_sleep_log(log, logging.WARNING), @@ -45,17 +46,21 @@ async def setup_s3_client(app) -> AsyncGenerator[None, None]: assert client # nosec app[APP_S3_KEY] = client - yield - # tear-down + yield + + with log_context(log, logging.DEBUG, msg="teardown.s3_client.cleanup_ctx"): + if client: await client.close() async def setup_s3_bucket(app: web.Application): - storage_s3_settings = app[APP_CONFIG_KEY].STORAGE_S3 - client = get_s3_client(app) - await client.create_bucket( - bucket=storage_s3_settings.S3_BUCKET_NAME, region=storage_s3_settings.S3_REGION - ) + with log_context(log, logging.DEBUG, msg="setup.s3_bucket.cleanup_ctx"): + storage_s3_settings = app[APP_CONFIG_KEY].STORAGE_S3 + client = get_s3_client(app) + await client.create_bucket( + bucket=storage_s3_settings.S3_BUCKET_NAME, + region=storage_s3_settings.S3_REGION, + ) yield