-
Notifications
You must be signed in to change notification settings - Fork 33
🎨 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
🎨 catalog: lifespan managers for fastapi apps #7483
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #7483 +/- ##
==========================================
+ Coverage 87.41% 87.47% +0.06%
==========================================
Files 1740 1734 -6
Lines 67273 67132 -141
Branches 1140 1142 +2
==========================================
- Hits 58806 58724 -82
+ Misses 8146 8087 -59
Partials 321 321
Continue to review full report in Codecov by Sentry.
🚀 New features to boost your workflow:
|
36d20c7
to
2a56df1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, this looks very promising!
A few questions here and there.
packages/service-library/src/servicelib/fastapi/postgres_lifespan.py
Outdated
Show resolved
Hide resolved
packages/service-library/src/servicelib/fastapi/postgres_lifespan.py
Outdated
Show resolved
Hide resolved
services/catalog/src/simcore_service_catalog/core/background_tasks.py
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please consider my naming change issue. I'd like to keep things aligned accross. I have the pattern I mentioned in my comment already in place in two services and I would like to avoid having different styles.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed OK with the changes. Just let's alright the naming. I'd like to avoid having it mixed, which is even more confusing
packages/service-library/src/servicelib/fastapi/postgres_lifespan.py
Outdated
Show resolved
Hide resolved
services/catalog/src/simcore_service_catalog/services/director.py
Outdated
Show resolved
Hide resolved
services/catalog/src/simcore_service_catalog/services/function_services.py
Outdated
Show resolved
Hide resolved
services/catalog/src/simcore_service_catalog/services/rabbitmq.py
Outdated
Show resolved
Hide resolved
a9ecdce
to
c81269f
Compare
…icIndentFormatter initialization
…te management in lifespan setup
…recate old function
… update references
…ifespan for database connection management
…stgres integration and cleanup old connection logic
…ith error handling and update test structure
…ures to use MockType
…to events module and updating related handlers
… improved lifecycle management
…lication lifespan management
…oved lifespan management
…improved test handling
… test fixtures for improved clarity
c81269f
to
1fd7453
Compare
|
What do these changes do?
FastAPI has deprecated the
on_startup
andon_shutdown
events in favor of the lifespan events contexts. This PR introduces apostgres_lifespan: LifespanManager
as a demonstration of howapp_lifespan
can coordinate multiple independent lifespan managers within a single FastAPI application.The initial focus is on standardizing the lifespan for a PostgreSQL database. Using
postgres_lifespan
centralizes the setup and teardown logic for PostgreSQL, allowing it to be shared across all microservices. Thecatalog
service already incorporatespostgres_lifespan
into its own application lifespan. This should be used as example to extend to other micro-servicesThe example below corresponds to the output of
test_app_lifespan_composition
It shows how nested lifespans can be composed so that state flows cleanly from parent to child. It also demonstrates the order of execution during startup/shutdown and how errors affect that flow.Future work will extend this approach to additional FastAPI apps and later to aiohttp-based services, enabling consistent and modular resource management across the stack.
FastAPI(..., lifespan=app_lifespan)
is used, theon_startup
andon_shutdown
events are not triggered.app = FastAPI(lifespan=events.lifecycle)
app.add_middleware(...)
app.include_router(...)
app.add_exception_handler(...)
Related issue/s
How to test
Dev-ops
None