Skip to content

Commit 2af93ea

Browse files
author
Andrei Neagu
committed
fixed broken tests
1 parent 5b22401 commit 2af93ea

File tree

7 files changed

+82
-31
lines changed

7 files changed

+82
-31
lines changed

services/notifications/src/simcore_service_notifications/api/rpc/routing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
]
1313

1414

15-
async def lifespan_rpc_api_routes(app: FastAPI) -> AsyncIterator[State]:
15+
async def rpc_api_routes_lifespan(app: FastAPI) -> AsyncIterator[State]:
1616
rpc_server = get_rabbitmq_rpc_server(app)
1717

1818
for router in ROUTERS:

services/notifications/src/simcore_service_notifications/core/application.py

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,19 @@
11
import logging
2-
from collections.abc import AsyncIterator
32

43
from fastapi import FastAPI
5-
from fastapi_lifespan_manager import State
6-
from servicelib.fastapi.lifespan_utils import LifespanGenerator, combine_lifespans
74
from servicelib.fastapi.openapi import (
85
get_common_oas_options,
96
override_fastapi_openapi_method,
107
)
8+
from servicelib.fastapi.prometheus_instrumentation import (
9+
initialize_prometheus_instrumentation,
10+
)
1111
from servicelib.fastapi.tracing import initialize_tracing
1212
from servicelib.logging_utils import config_all_loggers
1313

14-
from .._meta import (
15-
API_VTAG,
16-
APP_FINISHED_BANNER_MSG,
17-
APP_NAME,
18-
APP_STARTED_BANNER_MSG,
19-
SUMMARY,
20-
VERSION,
21-
)
14+
from .._meta import API_VTAG, APP_NAME, SUMMARY, VERSION
2215
from ..api.rest.routing import initialize_rest_api
23-
from ..api.rpc.routing import lifespan_rpc_api_routes
24-
from ..services.postgres.service import lifespan_postgres
25-
from ..services.rabbitmq import lifespan_rabbitmq
16+
from . import events
2617
from .settings import ApplicationSettings
2718

2819
_logger = logging.getLogger(__name__)
@@ -39,40 +30,30 @@ def _initialise_logger(settings: ApplicationSettings):
3930
)
4031

4132

42-
async def _lifespan_banner(app: FastAPI) -> AsyncIterator[State]:
43-
_ = app
44-
print(APP_STARTED_BANNER_MSG, flush=True) # noqa: T201
45-
yield {}
46-
print(APP_FINISHED_BANNER_MSG, flush=True) # noqa: T201
47-
48-
4933
def create_app() -> FastAPI:
5034
settings = ApplicationSettings.create_from_envs()
5135
_logger.debug(settings.model_dump_json(indent=2))
5236

5337
_initialise_logger(settings)
5438

55-
lifespans: list[LifespanGenerator] = [
56-
lifespan_rabbitmq,
57-
lifespan_postgres,
58-
lifespan_rpc_api_routes,
59-
]
60-
6139
assert settings.SC_BOOT_MODE # nosec
6240
app = FastAPI(
6341
debug=settings.SC_BOOT_MODE.is_devel_mode(),
6442
title=APP_NAME,
6543
description=SUMMARY,
6644
version=f"{VERSION}",
6745
openapi_url=f"/api/{API_VTAG}/openapi.json",
68-
lifespan=combine_lifespans(*lifespans, _lifespan_banner),
46+
lifespan=events.create_app_lifespan(),
6947
**get_common_oas_options(is_devel_mode=settings.SC_BOOT_MODE.is_devel_mode()),
7048
)
7149
override_fastapi_openapi_method(app)
7250
app.state.settings = settings
7351

7452
initialize_rest_api(app)
7553

54+
if settings.NOTIFICATIONS_PROMETHEUS_INSTRUMENTATION_ENABLED:
55+
initialize_prometheus_instrumentation(app)
56+
7657
if settings.NOTIFICATIONS_TRACING:
7758
initialize_tracing(app, settings.NOTIFICATIONS_TRACING, APP_NAME)
7859

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from collections.abc import AsyncIterator
2+
3+
from fastapi import FastAPI
4+
from fastapi_lifespan_manager import LifespanManager, State
5+
from servicelib.fastapi.postgres_lifespan import (
6+
PostgresLifespanState,
7+
postgres_database_lifespan,
8+
)
9+
from servicelib.fastapi.prometheus_instrumentation import (
10+
lifespan_prometheus_instrumentation,
11+
)
12+
13+
from .._meta import APP_FINISHED_BANNER_MSG, APP_STARTED_BANNER_MSG
14+
from ..api.rpc.routing import rpc_api_routes_lifespan
15+
from ..services.postgres import postgres_lifespan
16+
from ..services.rabbitmq import rabbitmq_lifespan
17+
from .settings import ApplicationSettings
18+
19+
20+
async def _banner_lifespan(app: FastAPI) -> AsyncIterator[State]:
21+
_ = app
22+
print(APP_STARTED_BANNER_MSG, flush=True) # noqa: T201
23+
yield {}
24+
print(APP_FINISHED_BANNER_MSG, flush=True) # noqa: T201
25+
26+
27+
async def _main_lifespan(app: FastAPI) -> AsyncIterator[State]:
28+
settings: ApplicationSettings = app.state.settings
29+
yield {
30+
PostgresLifespanState.POSTGRES_SETTINGS: settings.NOTIFICATIONS_POSTGRES,
31+
"prometheus_instrumentation_enabled": settings.NOTIFICATIONS_PROMETHEUS_INSTRUMENTATION_ENABLED,
32+
}
33+
34+
35+
async def _prometheus_instrumentation_lifespan( # TODO: put this into one single setup like we did for the DB
36+
app: FastAPI, state: State
37+
) -> AsyncIterator[State]:
38+
if state.get("prometheus_instrumentation_enabled", False):
39+
async for prometheus_state in lifespan_prometheus_instrumentation(app):
40+
yield prometheus_state
41+
42+
43+
def create_app_lifespan():
44+
# WARNING: order matters
45+
app_lifespan = LifespanManager()
46+
app_lifespan.add(_main_lifespan)
47+
48+
# - postgres
49+
app_lifespan.add(postgres_database_lifespan)
50+
app_lifespan.add(postgres_lifespan)
51+
52+
# - rabbitmq
53+
app_lifespan.add(rabbitmq_lifespan)
54+
55+
# - rpc api routes
56+
app_lifespan.add(rpc_api_routes_lifespan)
57+
58+
# - prometheus instrumentation
59+
app_lifespan.add(_prometheus_instrumentation_lifespan)
60+
61+
app_lifespan.add(_banner_lifespan)
62+
63+
return app_lifespan

services/notifications/src/simcore_service_notifications/core/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ class ApplicationSettings(BaseCustomSettings, MixinLoggingSettings):
7373
),
7474
]
7575

76+
NOTIFICATIONS_PROMETHEUS_INSTRUMENTATION_ENABLED: bool = True
77+
7678
@field_validator("LOG_LEVEL")
7779
@classmethod
7880
def valid_log_level(cls, value) -> LogLevel:

services/notifications/src/simcore_service_notifications/services/postgres/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
close_db_connection,
88
connect_to_db,
99
)
10+
from servicelib.fastapi.postgres_lifespan import PostgresLifespanState
1011
from servicelib.logging_utils import log_context
1112

1213
from ...core.settings import ApplicationSettings
@@ -15,7 +16,9 @@
1516
_logger = logging.getLogger(__name__)
1617

1718

18-
async def lifespan_postgres(app: FastAPI) -> AsyncIterator[State]:
19+
async def postgres_lifespan(app: FastAPI, state: State) -> AsyncIterator[State]:
20+
app.state.engine = state[PostgresLifespanState.POSTGRES_ASYNC_ENGINE]
21+
1922
settings: ApplicationSettings = app.state.settings
2023

2124
app.state.postgress_liveness = PostgresHealth(app)

services/notifications/src/simcore_service_notifications/services/rabbitmq.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from ..core.settings import ApplicationSettings
1010

1111

12-
async def lifespan_rabbitmq(app: FastAPI) -> AsyncIterator[State]:
12+
async def rabbitmq_lifespan(app: FastAPI) -> AsyncIterator[State]:
1313
settings: ApplicationSettings = app.state.settings
1414
rabbit_settings: RabbitSettings = settings.NOTIFICATIONS_RABBITMQ
1515
app.state.rabbitmq_rpc_server = None

services/notifications/tests/unit/conftest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from collections.abc import AsyncIterator
55

66
import pytest
7+
import sqlalchemy as sa
78
from asgi_lifespan import LifespanManager
89
from fastapi import FastAPI
910
from fastapi.testclient import TestClient
@@ -17,6 +18,7 @@ def service_env(
1718
monkeypatch: pytest.MonkeyPatch,
1819
mock_environment: EnvVarsDict,
1920
rabbit_service: RabbitSettings,
21+
postgres_db: sa.engine.Engine,
2022
postgres_env_vars_dict: EnvVarsDict,
2123
) -> EnvVarsDict:
2224
return setenvs_from_dict(

0 commit comments

Comments
 (0)