Skip to content

Commit 6847d11

Browse files
committed
@pcrespov use fastapi lifespan
1 parent 655835b commit 6847d11

File tree

2 files changed

+33
-34
lines changed

2 files changed

+33
-34
lines changed

services/storage/src/simcore_service_storage/core/application.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from ..exceptions.handlers import set_exception_handlers
3333
from ..modules.db import setup_db
3434
from ..modules.long_running_tasks import setup_rest_api_long_running_tasks_for_uploads
35-
from ..modules.rabbitmq import setup as setup_rabbitmq
35+
from ..modules.rabbitmq import rabbitmq_lifespan
3636
from ..modules.redis import setup as setup_redis
3737
from ..modules.s3 import setup_s3
3838
from .settings import ApplicationSettings
@@ -69,6 +69,7 @@ def create_app(settings: ApplicationSettings) -> FastAPI:
6969
openapi_url=f"/api/{API_VTAG}/openapi.json",
7070
docs_url="/dev/doc",
7171
redoc_url=None, # default disabled
72+
lifespan=rabbitmq_lifespan,
7273
)
7374
override_fastapi_openapi_method(app)
7475

@@ -79,7 +80,6 @@ def create_app(settings: ApplicationSettings) -> FastAPI:
7980
setup_s3(app)
8081
setup_client_session(app)
8182

82-
setup_rabbitmq(app)
8383
setup_rpc_api_routes(app)
8484
setup_rest_api_long_running_tasks_for_uploads(app)
8585
setup_rest_api_routes(app, API_VTAG)

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

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
from contextlib import asynccontextmanager
23
from typing import cast
34

45
from fastapi import FastAPI
@@ -15,40 +16,38 @@
1516
_logger = logging.getLogger(__name__)
1617

1718

18-
def setup(app: FastAPI) -> None:
19-
async def on_startup() -> None:
20-
with log_context(
21-
_logger,
22-
logging.INFO,
23-
msg="Storage startup Rabbitmq",
24-
):
25-
app.state.rabbitmq_client = None
26-
rabbit_settings: RabbitSettings | None = app.state.settings.STORAGE_RABBITMQ
27-
if not rabbit_settings:
28-
raise ConfigurationError(
29-
msg="RabbitMQ client is de-activated in the settings"
30-
)
31-
await wait_till_rabbitmq_responsive(rabbit_settings.dsn)
32-
app.state.rabbitmq_client = RabbitMQClient(
33-
client_name="storage", settings=rabbit_settings
19+
@asynccontextmanager
20+
async def rabbitmq_lifespan(app: FastAPI):
21+
with log_context(
22+
_logger,
23+
logging.INFO,
24+
msg="Storage startup Rabbitmq",
25+
):
26+
app.state.rabbitmq_client = None
27+
rabbit_settings: RabbitSettings | None = app.state.settings.STORAGE_RABBITMQ
28+
if not rabbit_settings:
29+
raise ConfigurationError(
30+
msg="RabbitMQ client is de-activated in the settings"
3431
)
35-
app.state.rabbitmq_rpc_server = await RabbitMQRPCClient.create(
36-
client_name="storage_rpc_server", settings=rabbit_settings
37-
)
38-
39-
async def on_shutdown() -> None:
40-
with log_context(
41-
_logger,
42-
logging.INFO,
43-
msg="Storage shutdown Rabbitmq",
44-
):
45-
if app.state.rabbitmq_client:
46-
await app.state.rabbitmq_client.close()
47-
if app.state.rabbitmq_rpc_server:
48-
await app.state.rabbitmq_rpc_server.close()
32+
await wait_till_rabbitmq_responsive(rabbit_settings.dsn)
33+
app.state.rabbitmq_client = RabbitMQClient(
34+
client_name="storage", settings=rabbit_settings
35+
)
36+
app.state.rabbitmq_rpc_server = await RabbitMQRPCClient.create(
37+
client_name="storage_rpc_server", settings=rabbit_settings
38+
)
4939

50-
app.add_event_handler("startup", on_startup)
51-
app.add_event_handler("shutdown", on_shutdown)
40+
yield
41+
42+
with log_context(
43+
_logger,
44+
logging.INFO,
45+
msg="Storage shutdown Rabbitmq",
46+
):
47+
if app.state.rabbitmq_client:
48+
await app.state.rabbitmq_client.close()
49+
if app.state.rabbitmq_rpc_server:
50+
await app.state.rabbitmq_rpc_server.close()
5251

5352

5453
def get_rabbitmq_client(app: FastAPI) -> RabbitMQClient:

0 commit comments

Comments
 (0)