Add create_app()
factory for pipeline deployment server
#4037
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.
This PR adds a
create_app()
factory function to the pipeline deployment server, enabling isolated FastAPI app instances for serverless environments like Modal while maintaining full backward compatibility with existing deployments.Changes
Core Implementation (
src/zenml/deployers/server/app.py
)create_app()
factory function: Creates fresh FastAPI instances with per-instance state isolationDeploymentAppConfig
Pydantic model: Type-safe configuration for app instances (deployment_id, auth_key, test_mode, CORS, etc.)_build_auth_dependency()
helper: Constructs per-instance auth closures with scoped auth keys_build_invoke_router()
: Now accepts optionalauth_dep
parameter for per-app auth injectionget_pipeline_service()
: Changed signature to requireRequest
parameter (no default) to comply with FastAPI dependency requirementsapp
,lifespan
,verify_token
, and_service
remain unchanged for backward compatibilityTest Coverage (
tests/unit/deployers/server/test_app.py
)Added comprehensive
TestCreateAppFactory
test class with 5 tests verifying:create_app()
withtest_mode=True
doesn't initialize service or touch global_service
app.state.service
create_app()
never modifies module-level_service
variableget_pipeline_service()
correctly prefersapp.state.service
over legacy globalMotivation
The existing module-level
app
uses global state (_service
) which causes issues in serverless environments:_service
Design Highlights
Per-Instance State
Per-Instance Auth
Backward Compatibility
Testing
All tests pass:
Usage Example (Modal)
Breaking Changes
None. All existing imports, CLI usage, and tests continue working without modification.
Rollout Plan
create_app()
for new deployments