Skip to content

Commit c5b1ba2

Browse files
committed
🎨 Refactor: Replace string-based app keys with type-safe web.AppKey instances in various modules
1 parent 0b0a12c commit c5b1ba2

File tree

6 files changed

+34
-24
lines changed

6 files changed

+34
-24
lines changed

services/web/server/src/simcore_service_webserver/diagnostics/_healthcheck.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import statistics
33
import time
44
from dataclasses import dataclass, field
5+
from typing import Final
56

67
from aiohttp import web
78
from servicelib.aiohttp.incidents import LimitedOrderedStack, SlowCallback
@@ -11,17 +12,6 @@
1112

1213
_logger = logging.getLogger(__name__)
1314

14-
# APP KEYS ---
15-
HEALTH_INCIDENTS_REGISTRY = f"{__name__}.incidents_registry"
16-
HEALTH_LAST_REQUESTS_AVG_LATENCY = f"{__name__}.last_requests_avg_latency"
17-
HEALTH_MAX_AVG_RESP_LATENCY = f"{__name__}.max_avg_response_latency"
18-
HEALTH_MAX_TASK_DELAY = f"{__name__}.max_task_delay"
19-
20-
HEALTH_LATENCY_PROBE = f"{__name__}.latency_probe"
21-
HEALTH_PLUGIN_START_TIME = f"{__name__}.plugin_start_time"
22-
23-
HEALTH_START_SENSING_DELAY_SECS = f"{__name__}.start_sensing_delay"
24-
2515

2616
class IncidentsRegistry(LimitedOrderedStack[SlowCallback]):
2717
def max_delay(self) -> float:
@@ -57,6 +47,18 @@ def value(self) -> float:
5747
return delay
5848

5949

50+
HEALTH_INCIDENTS_REGISTRY: Final = web.AppKey(
51+
"HEALTH_INCIDENTS_REGISTRY", IncidentsRegistry
52+
)
53+
HEALTH_LATENCY_PROBE: Final = web.AppKey("HEALTH_LATENCY_PROBE", DelayWindowProbe)
54+
55+
HEALTH_LAST_REQUESTS_AVG_LATENCY: Final = f"{__name__}.last_requests_avg_latency"
56+
HEALTH_MAX_AVG_RESP_LATENCY: Final = f"{__name__}.max_avg_response_latency"
57+
HEALTH_MAX_TASK_DELAY: Final = f"{__name__}.max_task_delay"
58+
HEALTH_PLUGIN_START_TIME: Final = f"{__name__}.plugin_start_time"
59+
HEALTH_START_SENSING_DELAY_SECS: Final = f"{__name__}.start_sensing_delay"
60+
61+
6062
_logged_once = False
6163

6264

services/web/server/src/simcore_service_webserver/director_v2/_director_v2_abc_service.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
from aiohttp import web
44
from models_library.projects import CommitID, ProjectID
55

6-
_APP_PROJECT_RUN_POLICY_KEY = f"{__name__}.ProjectRunPolicy"
7-
86

97
class AbstractProjectRunPolicy(ABC):
108
"""
@@ -42,6 +40,11 @@ async def get_or_create_runnable_projects(
4240
) -> tuple[list[ProjectID], list[CommitID]]: ...
4341

4442

43+
_APP_PROJECT_RUN_POLICY_KEY = web.AppKey(
44+
"_APP_PROJECT_RUN_POLICY_KEY", AbstractProjectRunPolicy
45+
)
46+
47+
4548
def get_project_run_policy(app: web.Application) -> AbstractProjectRunPolicy | None:
4649
app_: AbstractProjectRunPolicy | None = app.get(_APP_PROJECT_RUN_POLICY_KEY)
4750
return app_

services/web/server/src/simcore_service_webserver/fogbugz/_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import json
77
import logging
8-
from typing import Any
8+
from typing import Any, Final
99
from urllib.parse import urljoin
1010

1111
import httpx
@@ -156,7 +156,7 @@ async def reopen_case(self, case_id: str, assigned_fogbugz_person_id: str) -> No
156156
raise ValueError(msg)
157157

158158

159-
_APP_KEY = f"{__name__}.{FogbugzRestClient.__name__}"
159+
_APP_KEY: Final = web.AppKey(FogbugzRestClient.__name__, FogbugzRestClient)
160160

161161

162162
async def setup_fogbugz_rest_client(app: web.Application) -> None:

services/web/server/src/simcore_service_webserver/garbage_collector/_tasks_utils.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import asyncio
66
from collections.abc import AsyncIterator, Callable, Coroutine
7+
from typing import Final
78

89
from aiohttp import web
910
from common_library.async_tools import cancel_wait_task
@@ -19,6 +20,9 @@ def create_task_name(coro: Callable) -> str:
1920
return f"{coro.__module__}.{coro.__name__}"
2021

2122

23+
_GC_PERIODIC_TASKS_KEY: Final = web.AppKey("gc-tasks", dict[str, asyncio.Task])
24+
25+
2226
async def periodic_task_lifespan(
2327
app: web.Application,
2428
periodic_async_func: Callable[[], Coroutine[None, None, None]],
@@ -43,15 +47,16 @@ async def periodic_task_lifespan(
4347
)
4448

4549
# Keeping a reference in app's state to prevent premature garbage collection of the task
46-
app_task_key = f"gc-tasks/{task_name}"
47-
if app_task_key in app:
50+
app.setdefault(_GC_PERIODIC_TASKS_KEY, {})
51+
if task_name in app[_GC_PERIODIC_TASKS_KEY]:
4852
msg = f"Task {task_name} is already registered in the app state"
4953
raise ValueError(msg)
5054

51-
app[app_task_key] = task
55+
app[_GC_PERIODIC_TASKS_KEY][task_name] = task
5256

5357
yield
5458

5559
# tear-down
5660
await cancel_wait_task(task)
57-
app.pop(app_task_key, None)
61+
if _GC_PERIODIC_TASKS_KEY in app:
62+
app[_GC_PERIODIC_TASKS_KEY].pop(task_name, None)

services/web/server/src/simcore_service_webserver/products/_web_events.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
_logger = logging.getLogger(__name__)
1414

15-
APP_PRODUCTS_TEMPLATES_DIR_KEY = f"{__name__}.template_dir"
15+
APP_PRODUCTS_TEMPLATES_DIR_KEY = web.AppKey("template_dir", Path)
1616

1717

1818
async def _auto_create_products_groups(app: web.Application) -> None:
@@ -59,9 +59,7 @@ async def _setup_product_templates(app: web.Application):
5959
"""
6060
builds a directory and download product templates
6161
"""
62-
with tempfile.TemporaryDirectory(
63-
suffix=APP_PRODUCTS_TEMPLATES_DIR_KEY
64-
) as templates_dir:
62+
with tempfile.TemporaryDirectory(suffix="product_template_") as templates_dir:
6563
app[APP_PRODUCTS_TEMPLATES_DIR_KEY] = Path(templates_dir)
6664

6765
yield

services/web/server/src/simcore_service_webserver/projects/_projects_repository_legacy.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@
114114

115115
_logger = logging.getLogger(__name__)
116116

117-
APP_PROJECT_DBAPI = __name__ + ".ProjectDBAPI"
118117
ANY_USER = ANY_USER_ID_SENTINEL
119118

120119
DEFAULT_ORDER_BY = OrderBy(
@@ -1392,6 +1391,9 @@ async def check_project_has_only_one_product(self, project_uuid: ProjectID) -> N
13921391
)
13931392

13941393

1394+
APP_PROJECT_DBAPI = web.AppKey("ProjectDBAPI", ProjectDBAPI)
1395+
1396+
13951397
def setup_projects_db(app: web.Application):
13961398
# NOTE: inits once per app
13971399
return ProjectDBAPI.set_once_in_app_context(app)

0 commit comments

Comments
 (0)