Skip to content

Commit 25bd310

Browse files
committed
dynamic-sidecar
1 parent 9a6ebf1 commit 25bd310

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

services/dynamic-sidecar/src/simcore_service_dynamic_sidecar/core/application.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
from asyncio import Lock
33
from typing import Any, ClassVar
44

5+
from common_library.json_serialization import json_dumps
56
from fastapi import FastAPI
67
from servicelib.async_utils import cancel_sequential_workers
78
from servicelib.fastapi import long_running_tasks
9+
from servicelib.fastapi.logging_lifespan import setup_logging_shutdown_event
810
from servicelib.fastapi.openapi import (
911
get_common_oas_options,
1012
override_fastapi_openapi_method,
@@ -13,7 +15,6 @@
1315
initialize_fastapi_app_tracing,
1416
setup_tracing,
1517
)
16-
from servicelib.logging_utils import setup_loggers
1718
from simcore_sdk.node_ports_common.exceptions import NodeNotFound
1819

1920
from .._meta import API_VERSION, API_VTAG, PROJECT_NAME, SUMMARY, __version__
@@ -114,41 +115,44 @@ def compose_spec(self) -> str | None:
114115
return self._shared_store.compose_spec
115116

116117

117-
def setup_logger(settings: ApplicationSettings):
118-
setup_loggers(
119-
log_format_local_dev_enabled=settings.DY_SIDECAR_LOG_FORMAT_LOCAL_DEV_ENABLED,
120-
logger_filter_mapping=settings.DY_SIDECAR_LOG_FILTER_MAPPING,
121-
tracing_settings=settings.DYNAMIC_SIDECAR_TRACING,
122-
log_base_level=settings.log_level,
118+
def create_base_app() -> FastAPI:
119+
# settings
120+
app_settings = ApplicationSettings.create_from_envs()
121+
logging_shutdown_event = setup_logging_shutdown_event(
122+
log_format_local_dev_enabled=app_settings.DY_SIDECAR_LOG_FORMAT_LOCAL_DEV_ENABLED,
123+
logger_filter_mapping=app_settings.DY_SIDECAR_LOG_FILTER_MAPPING,
124+
tracing_settings=app_settings.DYNAMIC_SIDECAR_TRACING,
125+
log_base_level=app_settings.log_level,
123126
noisy_loggers=_NOISY_LOGGERS,
124127
)
125128

126-
127-
def create_base_app() -> FastAPI:
128-
# settings
129-
settings = ApplicationSettings.create_from_envs()
130-
setup_logger(settings)
131-
logger.debug(settings.model_dump_json(indent=2))
129+
logger.info(
130+
"Application settings: %s",
131+
json_dumps(app_settings, indent=2, sort_keys=True),
132+
)
132133

133134
# minimal
134-
assert settings.SC_BOOT_MODE # nosec
135+
assert app_settings.SC_BOOT_MODE # nosec
135136
app = FastAPI(
136-
debug=settings.SC_BOOT_MODE.is_devel_mode(),
137+
debug=app_settings.SC_BOOT_MODE.is_devel_mode(),
137138
title=PROJECT_NAME,
138139
description=SUMMARY,
139140
version=API_VERSION,
140141
openapi_url=f"/api/{API_VTAG}/openapi.json",
141-
**get_common_oas_options(is_devel_mode=settings.SC_BOOT_MODE.is_devel_mode()),
142+
**get_common_oas_options(
143+
is_devel_mode=app_settings.SC_BOOT_MODE.is_devel_mode()
144+
),
142145
)
143146
override_fastapi_openapi_method(app)
144-
app.state.settings = settings
147+
app.state.settings = app_settings
145148

146149
long_running_tasks.server.setup(app)
147150

148151
app.include_router(get_main_router(app))
149152

150153
setup_reserved_space(app)
151154

155+
app.add_event_handler("shutdown", logging_shutdown_event)
152156
return app
153157

154158

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
"""Main application to be deployed in for example uvicorn.
2-
"""
1+
"""Main application to be deployed in for example uvicorn."""
32

43
from fastapi import FastAPI
54
from simcore_service_dynamic_sidecar.core.application import create_app
65

7-
# SINGLETON FastAPI app
8-
the_app: FastAPI = create_app()
6+
7+
def app_factory() -> FastAPI:
8+
"""Factory function to create the FastAPI app instance.
9+
10+
This is used by uvicorn or other ASGI servers to run the application.
11+
"""
12+
return create_app()

0 commit comments

Comments
 (0)