-
Notifications
You must be signed in to change notification settings - Fork 27
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
♻️ Adding lifespan support for FastAPI & migrated dynamic-scheduler
to use it
#7149
♻️ Adding lifespan support for FastAPI & migrated dynamic-scheduler
to use it
#7149
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #7149 +/- ##
==========================================
- Coverage 87.72% 87.58% -0.14%
==========================================
Files 1635 1277 -358
Lines 63943 54006 -9937
Branches 1178 651 -527
==========================================
- Hits 56093 47302 -8791
+ Misses 7538 6524 -1014
+ Partials 312 180 -132
Continue to review full report in Codecov by Sentry.
|
packages/service-library/src/servicelib/fastapi/lifespan_utils.py
Outdated
Show resolved
Hide resolved
packages/service-library/src/servicelib/fastapi/lifespan_utils.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.
Hmm I like the approach to change this to use the lifespan but I am not convinced how this goes now.
I would prefer that you inspire yourself from how aiohttp did it:
- no need to create an asynccontextmanager everytime, it is just a standard python Generator such as
# db.py
async def _setup_db(app: FastAPI) -> AsyncGenerator[None]:
# do the init stuff
yield
# do the cleanup stuff
get_lifespan_manager(app).add(_setup_db)
packages/service-library/src/servicelib/fastapi/lifespan_utils.py
Outdated
Show resolved
Hide resolved
I am not in favour of this. Since a the concept of a context manger is exactly what you like. It runs some code "initially", "yields" execution and "finally" runs code to cleanup. Anybody has more opinions on this? @pcrespov @matusdrobuliak66 @giancarloromeo |
@GitHK I think @sanderegg proposal does not contradict yours. What he is basically suggesting is to expose that functionality as a decorator, which is perfectly possible in this case. I like the ideas you are proposing here. IMO it just need a bit of tuning (I will prepare some examples in my review) |
OK tried with the suggested library. |
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.
thx. left some suggestions
from prometheus_client import CollectorRegistry | ||
from prometheus_fastapi_instrumentator import Instrumentator | ||
|
||
|
||
def setup_prometheus_instrumentation(app: FastAPI) -> Instrumentator: | ||
def initialize_prometheus_instrumentation(app: FastAPI) -> None: | ||
# NOTE: this cannot be ran once the application is started |
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.
THOUGHT: ideally i would enforce this comment with code (e.g. with a decorator that if the app is started, it raises)
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.
I would not know how to do that.
I think this is also fine since an error will be raised pointing you to this function.
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.
What about checking that the state that you initialize in this initializer
is not initialized? :-)
packages/service-library/src/servicelib/fastapi/prometheus_instrumentation.py
Show resolved
Hide resolved
packages/service-library/src/servicelib/fastapi/prometheus_instrumentation.py
Outdated
Show resolved
Hide resolved
packages/service-library/src/servicelib/fastapi/lifespan_utils.py
Outdated
Show resolved
Hide resolved
services/dynamic-scheduler/src/simcore_service_dynamic_scheduler/api/rpc/routes.py
Show resolved
Hide resolved
Quality Gate passedIssues Measures |
What do these changes do?
While working on #7070 it became necessary to have a "setup" function that can use a context manger. Unfortunately FastAPI does not allow us to mix the old
setup
functions and the new lifespan.What does this bring:
dynamic-scheduler
is the first service which was totally portedNew complications
Previously each module had its own
setup
function to clearly state that something required initialisation. Since events were being used forsetup
andteardown
, there was no clear need to distinguish the 3 phases of theFastAPI
setup
:As viewed below.
With the
lifespan
thebefore lifecycle
part cannot be included in the lifespan.Some modules like the
prometheus_instrumentation
had to be split for this reason.Coding style extension
Prefixes for functions used to configure the module during the setting up phase:
setup
can be used as before includingbefore
,startup
andshutdown
lifespan
can only includestartup
andshutdown
intialize
only includesbefore
Maybe
initialize
(as a name) can be improved but I would still keep this structure to avoid confusion when poring over the other services.Related issue/s
docker-api-proxy
service ⚠️ #7070How to test
Dev-ops checklist