Skip to content

Commit 4ae7f6f

Browse files
committed
Revert "@pcrespov use fastapi lifespan"
This reverts commit 6847d11.
1 parent 6847d11 commit 4ae7f6f

File tree

2 files changed

+34
-33
lines changed

2 files changed

+34
-33
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 rabbitmq_lifespan
35+
from ..modules.rabbitmq import setup as setup_rabbitmq
3636
from ..modules.redis import setup as setup_redis
3737
from ..modules.s3 import setup_s3
3838
from .settings import ApplicationSettings
@@ -69,7 +69,6 @@ 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,
7372
)
7473
override_fastapi_openapi_method(app)
7574

@@ -80,6 +79,7 @@ def create_app(settings: ApplicationSettings) -> FastAPI:
8079
setup_s3(app)
8180
setup_client_session(app)
8281

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: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import logging
2-
from contextlib import asynccontextmanager
32
from typing import cast
43

54
from fastapi import FastAPI
@@ -16,38 +15,40 @@
1615
_logger = logging.getLogger(__name__)
1716

1817

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"
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
3134
)
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-
)
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()
3949

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()
50+
app.add_event_handler("startup", on_startup)
51+
app.add_event_handler("shutdown", on_shutdown)
5152

5253

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

0 commit comments

Comments
 (0)