Skip to content

Commit f7f3b24

Browse files
committed
✨ Refactor import statements and update deprecated function usage in FastAPI lifespan management
1 parent b7a9039 commit f7f3b24

File tree

6 files changed

+26
-16
lines changed

6 files changed

+26
-16
lines changed

packages/service-library/src/servicelib/fastapi/lifespan_utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
from common_library.errors_classes import OsparcErrorMixin
66
from fastapi import FastAPI
77
from fastapi_lifespan_manager import State
8-
from servicelib.logging_utils import log_context
8+
9+
from ..logging_utils import log_context
910

1011

1112
class LifespanError(OsparcErrorMixin, RuntimeError): ...

packages/service-library/src/servicelib/fastapi/postgres_lifespan.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55

66
from fastapi import FastAPI
77
from fastapi_lifespan_manager import State
8-
from servicelib.logging_utils import log_catch
98
from settings_library.postgres import PostgresSettings
109
from sqlalchemy.ext.asyncio import AsyncEngine
1110

1211
from ..db_asyncpg_utils import create_async_engine_and_database_ready
12+
from ..logging_utils import log_catch
1313
from .lifespan_utils import LifespanOnStartupError, lifespan_context
1414

1515
_logger = logging.getLogger(__name__)

packages/service-library/src/servicelib/fastapi/rabbitmq.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import warnings
23

34
from fastapi import FastAPI
45
from models_library.rabbitmq_messages import RabbitMessageBase
@@ -55,6 +56,13 @@ def setup_rabbit(
5556
settings -- Rabbit settings or if None, the connection to rabbit is not done upon startup
5657
name -- name for the rmq client name
5758
"""
59+
warnings.warn(
60+
"The 'setup_rabbit' function is deprecated and will be removed in a future release. "
61+
"Please use 'rabbitmq_lifespan' for managing RabbitMQ connections.",
62+
DeprecationWarning,
63+
stacklevel=2,
64+
)
65+
5866
app.state.rabbitmq_client = None # RabbitMQClient | None
5967
app.state.rabbitmq_client_name = name
6068
app.state.rabbitmq_settings = settings

packages/service-library/src/servicelib/fastapi/rabbitmq_lifespan.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
from fastapi import FastAPI
55
from fastapi_lifespan_manager import State
66
from pydantic import BaseModel, ValidationError
7-
from servicelib.rabbitmq import wait_till_rabbitmq_responsive
87
from settings_library.rabbit import RabbitSettings
98

9+
from ..rabbitmq import wait_till_rabbitmq_responsive
1010
from .lifespan_utils import (
1111
LifespanOnStartupError,
1212
lifespan_context,

packages/service-library/src/servicelib/fastapi/redis_lifespan.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
from fastapi import FastAPI
77
from fastapi_lifespan_manager import State
88
from pydantic import BaseModel, StringConstraints, ValidationError
9-
from servicelib.logging_utils import log_catch, log_context
109
from settings_library.redis import RedisDatabase, RedisSettings
1110

11+
from ..logging_utils import log_catch, log_context
1212
from ..redis import RedisClientSDK
1313
from .lifespan_utils import LifespanOnStartupError, lifespan_context
1414

@@ -25,8 +25,8 @@ class RedisLifespanState(BaseModel):
2525
REDIS_CLIENT_DB: RedisDatabase
2626

2727

28-
async def redis_database_lifespan(_: FastAPI, state: State) -> AsyncIterator[State]:
29-
_lifespan_name = f"{__name__}.{redis_database_lifespan.__name__}"
28+
async def redis_client_sdk_lifespan(_: FastAPI, state: State) -> AsyncIterator[State]:
29+
_lifespan_name = f"{__name__}.{redis_client_sdk_lifespan.__name__}"
3030

3131
with lifespan_context(_logger, logging.INFO, _lifespan_name, state) as called_state:
3232

@@ -45,6 +45,8 @@ async def redis_database_lifespan(_: FastAPI, state: State) -> AsyncIterator[Sta
4545
logging.INFO,
4646
f"Creating redis client with name={redis_state.REDIS_CLIENT_NAME}",
4747
):
48+
# NOTE: sdk integrats waiting until connection is ready
49+
# and will raise an exception if it cannot connect
4850
redis_client = RedisClientSDK(
4951
redis_dsn_with_secrets,
5052
client_name=redis_state.REDIS_CLIENT_NAME,
@@ -54,10 +56,9 @@ async def redis_database_lifespan(_: FastAPI, state: State) -> AsyncIterator[Sta
5456
yield {"REDIS_CLIENT_SDK": redis_client, **called_state}
5557
finally:
5658
# Teardown client
57-
if redis_client:
58-
with log_catch(_logger, reraise=False):
59-
await asyncio.wait_for(
60-
redis_client.shutdown(),
61-
# NOTE: shutdown already has a _HEALTHCHECK_TASK_TIMEOUT_S of 10s
62-
timeout=20,
63-
)
59+
with log_catch(_logger, reraise=False):
60+
await asyncio.wait_for(
61+
redis_client.shutdown(),
62+
# NOTE: shutdown already has a _HEALTHCHECK_TASK_TIMEOUT_S of 10s
63+
timeout=20,
64+
)

packages/service-library/tests/fastapi/test_redis_lifespan.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from servicelib.fastapi.redis_lifespan import (
2020
RedisConfigurationError,
2121
RedisLifespanState,
22-
redis_database_lifespan,
22+
redis_client_sdk_lifespan,
2323
)
2424
from settings_library.application import BaseApplicationSettings
2525
from settings_library.redis import RedisDatabase, RedisSettings
@@ -65,7 +65,7 @@ async def my_app_settings(app: FastAPI) -> AsyncIterator[State]:
6565

6666
app_lifespan = LifespanManager()
6767
app_lifespan.add(my_app_settings)
68-
app_lifespan.add(redis_database_lifespan)
68+
app_lifespan.add(redis_client_sdk_lifespan)
6969

7070
assert not mock_redis_client_sdk.called
7171

@@ -112,7 +112,7 @@ async def my_app_settings(app: FastAPI) -> AsyncIterator[State]:
112112

113113
app_lifespan = LifespanManager()
114114
app_lifespan.add(my_app_settings)
115-
app_lifespan.add(redis_database_lifespan)
115+
app_lifespan.add(redis_client_sdk_lifespan)
116116

117117
app = FastAPI(lifespan=app_lifespan)
118118

0 commit comments

Comments
 (0)