-
Notifications
You must be signed in to change notification settings - Fork 36
🎨 catalog: lifespan managers for fastapi apps #7483
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
pcrespov
merged 29 commits into
ITISFoundation:master
from
pcrespov:mai/common-lifespans
Apr 8, 2025
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
2a76de5
tests concept further
pcrespov 7e287e2
🐛 Fix: Adjust sign handling in timedelta formatting and improve Dynam…
pcrespov 3ce6b36
helpers
pcrespov 8d4b3bf
✨ Refactor: Rename async engine creation function and update references
pcrespov c491f77
✨ Refactor: Introduce PostgresLifespanStateKeys enum for improved sta…
pcrespov 7532170
✨ Refactor: Rename async engine creation function for clarity and dep…
pcrespov 23af712
✨ Refactor: Rename async engine creation function for consistency and…
pcrespov 8305b9b
✨ Refactor: Deprecate connect_to_db function and recommend postgres_l…
pcrespov c4fe816
✨ Refactor catalog: Implement application lifespan management with Po…
pcrespov 5c0aa25
✨ Refactor: Ensure async engine disposal in setup_postgres_database w…
pcrespov 4dc9f46
✨ Refactor: Clean up imports in postgres_lifespan.py for consistency
pcrespov f26338a
✨ Refactor: Enhance Postgres settings validation and update test fixt…
pcrespov 119540c
cleanup
pcrespov 79887d2
✨ Refactor: Simplify application lifespan management by moving logic …
pcrespov 47979b4
✨ Refactor: Update service setup functions to use async iterators for…
pcrespov 05c1a90
✨ Refactor: Integrate Prometheus instrumentation and tracing into app…
pcrespov 4c26ada
✨ Refactor: Update RPC API route setup to use async iterator for impr…
pcrespov da317b7
✨ Refactor: Update background tasks setup to use context manager for …
pcrespov adcb226
✨ Refactor: Enhance Prometheus instrumentation integration and update…
pcrespov 2bad4cd
✨ Refactor: Rename postgres lifespan manager for consistency across t…
pcrespov f8bd194
minor
pcrespov 85703c0
@sanderegg review: config error
pcrespov 2a33aa9
@GitHK review: rename
pcrespov f7c7f5d
@sanderegg review: renames
pcrespov b088016
✨ Refactor: Rename lifespan mocks for clarity and consistency in tests
pcrespov 530f4df
minor
pcrespov 1fd7453
fixes fixture
pcrespov 2145c09
✨ Refactor: Update service specifications and remove redundant null s…
pcrespov 0ccb681
fixes test
pcrespov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 13 additions & 2 deletions
15
packages/service-library/src/servicelib/fastapi/lifespan_utils.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
packages/service-library/src/servicelib/fastapi/postgres_lifespan.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import asyncio | ||
import logging | ||
from collections.abc import AsyncIterator | ||
from enum import Enum | ||
|
||
from fastapi_lifespan_manager import State | ||
from servicelib.logging_utils import log_catch, log_context | ||
from settings_library.postgres import PostgresSettings | ||
from sqlalchemy.ext.asyncio import AsyncEngine | ||
|
||
from ..db_asyncpg_utils import create_async_engine_and_database_ready | ||
from .lifespan_utils import LifespanOnStartupError | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
|
||
class PostgresLifespanState(str, Enum): | ||
POSTGRES_SETTINGS = "postgres_settings" | ||
POSTGRES_ASYNC_ENGINE = "postgres.async_engine" | ||
|
||
|
||
class PostgresConfigurationError(LifespanOnStartupError): | ||
msg_template = "Invalid postgres settings [={settings}] on startup. Note that postgres cannot be disabled using settings" | ||
|
||
|
||
def create_input_state(settings: PostgresSettings) -> State: | ||
return {PostgresLifespanState.POSTGRES_SETTINGS: settings} | ||
|
||
|
||
async def postgres_database_lifespan(_, state: State) -> AsyncIterator[State]: | ||
|
||
with log_context(_logger, logging.INFO, f"{__name__}"): | ||
|
||
settings = state[PostgresLifespanState.POSTGRES_SETTINGS] | ||
|
||
if settings is None or not isinstance(settings, PostgresSettings): | ||
raise PostgresConfigurationError(settings=settings) | ||
|
||
assert isinstance(settings, PostgresSettings) # nosec | ||
|
||
# connect to database | ||
async_engine: AsyncEngine = await create_async_engine_and_database_ready( | ||
settings | ||
) | ||
|
||
try: | ||
|
||
yield { | ||
PostgresLifespanState.POSTGRES_ASYNC_ENGINE: async_engine, | ||
} | ||
|
||
finally: | ||
with log_catch(_logger, reraise=False): | ||
await asyncio.wait_for(async_engine.dispose(), timeout=10) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.