Skip to content

Commit 4aab0c3

Browse files
fix: concurrent bucket creation
1 parent 4289fb0 commit 4aab0c3

File tree

1 file changed

+23
-7
lines changed
  • services/storage/src/simcore_service_storage/modules

1 file changed

+23
-7
lines changed

services/storage/src/simcore_service_storage/modules/s3.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from fastapi import FastAPI
99
from pydantic import TypeAdapter
1010
from servicelib.logging_utils import log_context
11+
from tenacity import wait_exponential
1112
from tenacity.asyncio import AsyncRetrying
1213
from tenacity.before_sleep import before_sleep_log
1314
from tenacity.wait import wait_fixed
@@ -44,14 +45,29 @@ async def _on_startup() -> None:
4445
assert client # nosec
4546
app.state.s3_client = client
4647

47-
with log_context(_logger, logging.DEBUG, msg="setup.s3_bucket.cleanup_ctx"):
48+
async for attempt in AsyncRetrying(
49+
wait=wait_exponential(min=1), # 1, 2, 4, 8, ...
50+
before_sleep=before_sleep_log(_logger, logging.WARNING),
51+
reraise=True,
52+
):
4853
assert settings.STORAGE_S3 # nosec
49-
await client.create_bucket(
50-
bucket=settings.STORAGE_S3.S3_BUCKET_NAME,
51-
region=TypeAdapter(
52-
BucketLocationConstraintType | Literal["us-east-1"]
53-
).validate_python(settings.STORAGE_S3.S3_REGION),
54-
)
54+
with attempt, log_context(
55+
_logger, logging.DEBUG, msg="setup.s3_bucket.cleanup_ctx"
56+
):
57+
if await client.bucket_exists(
58+
bucket=settings.STORAGE_S3.S3_BUCKET_NAME
59+
):
60+
_logger.info(
61+
"S3 bucket %s exists already, skipping creation",
62+
settings.STORAGE_S3.S3_BUCKET_NAME,
63+
)
64+
break
65+
await client.create_bucket(
66+
bucket=settings.STORAGE_S3.S3_BUCKET_NAME,
67+
region=TypeAdapter(
68+
BucketLocationConstraintType | Literal["us-east-1"]
69+
).validate_python(settings.STORAGE_S3.S3_REGION),
70+
)
5571

5672
async def _on_shutdown() -> None:
5773
if app.state.s3_client:

0 commit comments

Comments
 (0)