Skip to content

Commit 89669b0

Browse files
committed
✨ Add lifespan call tracking for PostgreSQL database in FastAPI
1 parent 647576c commit 89669b0

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from sqlalchemy.ext.asyncio import AsyncEngine
1111

1212
from ..db_asyncpg_utils import create_async_engine_and_database_ready
13-
from .lifespan_utils import LifespanOnStartupError
13+
from .lifespan_utils import LifespanOnStartupError, record_lifespan_called_once
1414

1515
_logger = logging.getLogger(__name__)
1616

@@ -32,6 +32,9 @@ async def postgres_database_lifespan(_: FastAPI, state: State) -> AsyncIterator[
3232

3333
with log_context(_logger, logging.INFO, f"{__name__}"):
3434

35+
# Mark lifespan as called
36+
called_state = record_lifespan_called_once(state, "postgres_database_lifespan")
37+
3538
settings = state[PostgresLifespanState.POSTGRES_SETTINGS]
3639

3740
if settings is None or not isinstance(settings, PostgresSettings):
@@ -48,6 +51,7 @@ async def postgres_database_lifespan(_: FastAPI, state: State) -> AsyncIterator[
4851

4952
yield {
5053
PostgresLifespanState.POSTGRES_ASYNC_ENGINE: async_engine,
54+
**called_state,
5155
}
5256

5357
finally:

packages/settings-library/src/settings_library/rabbit.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from pydantic.networks import AnyUrl
44
from pydantic.types import SecretStr
5+
from pydantic_settings import SettingsConfigDict
56

67
from .base import BaseCustomSettings
78
from .basic_types import PortInt
@@ -33,3 +34,17 @@ def dsn(self) -> str:
3334
)
3435
)
3536
return rabbit_dsn
37+
38+
model_config = SettingsConfigDict(
39+
json_schema_extra={
40+
"examples": [
41+
# minimal required
42+
{
43+
"RABBIT_SECURE": "1",
44+
"RABBIT_HOST": "localhost",
45+
"RABBIT_USER": "user",
46+
"RABBIT_PASSWORD": "foobar", # NOSONAR
47+
}
48+
],
49+
}
50+
)

packages/settings-library/src/settings_library/redis.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ def build_redis_dsn(self, db_index: RedisDatabase) -> str:
5252
"examples": [
5353
# minimal required
5454
{
55-
"REDIS_SECURE": "0",
56-
"REDIS_HOST": "localhost",
57-
"REDIS_PORT": "6379",
5855
"REDIS_USER": "user",
5956
"REDIS_PASSWORD": "foobar", # NOSONAR
6057
}

0 commit comments

Comments
 (0)