Skip to content

Commit a674198

Browse files
acn-ericlawclaude
andcommitted
chore: IDE/Sonar round on the actuator - single async dispatcher, narrowing, spelling
One async handle() dispatcher (awaits the /health branch; the constant endpoints render synchronously) replaces four async-without-await handlers - and now mirrors the node twin's dispatcher shape. app_origin uses the local-narrowing pattern (checkers do not narrow module globals). 'health check' spelling; origin test no longer compares an expression to itself. Co-Authored-By: Claude Code <noreply@anthropic.com>
1 parent 56c002c commit a674198

5 files changed

Lines changed: 39 additions & 23 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
- Actuator endpoints `/info`, `/info/routes`, `/env`, `/health` and `/livenessprobe` -
66
the engines' operational surface, for Kubernetes probes and one-dashboard monitoring
7-
of polyglot installations. Health-check functions are normal registered functions
7+
of polyglot installations. Health check functions are normal registered functions
88
speaking the engines' `type=info` / `type=health` interface contract, listed in
99
`mandatory.health.dependencies` / `optional.health.dependencies` and called through
1010
the event bus. `/health` answers `UP` (200) / `DOWN` (400); `/livenessprobe` follows

‎README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ Kubernetes probes and dashboards treat a Python app exactly like a Java or Rust
143143
Configuration keys carry the engines' names: `info.app.version`, `info.app.description`,
144144
`show.env.variables` and `show.application.properties` (opt-in lists — secrets are never
145145
dumped wholesale), and `mandatory.health.dependencies` / `optional.health.dependencies`
146-
(routes of health-check functions; optional ones never change the overall status). A
147-
health-check function is a normal registered function — usually private — speaking the
146+
(routes of health check functions; optional ones never change the overall status). A
147+
health check function is a normal registered function — usually private — speaking the
148148
engines' interface contract, called through the event bus:
149149

150150
```python

‎src/mercury_composable/actuator.py‎

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
- ``GET /env`` - selected environment variables (``show.env.variables``) and
1212
selected configuration parameters (``show.application.properties``) -
1313
opt-in lists, so secrets are never dumped wholesale (engine parity).
14-
- ``GET /health`` - runs the health-check functions listed in
14+
- ``GET /health`` - runs the health check functions listed in
1515
``mandatory.health.dependencies`` / ``optional.health.dependencies``.
1616
All mandatory up -> ``UP`` (HTTP 200); any mandatory down -> ``DOWN``
1717
(HTTP 400, engine parity). The outcome feeds the liveness state.
1818
- ``GET /livenessprobe`` - ``OK`` (text) while the last health outcome is
1919
good, else HTTP 400 ``Unhealthy. Please check '/health' endpoint.``
2020
21-
A health-check function is a normal registered function (usually private)
21+
A health check function is a normal registered function (usually private)
2222
speaking the engines' interface contract - called through the same event bus
2323
that serves PostOffice, first with header ``type=info`` (an advisory identity
2424
map merged into its dependency entry), then with ``type=health`` (a status
@@ -71,9 +71,11 @@ def app_origin() -> str:
7171
"""Unique instance id, minted once per process (the Java reference
7272
engine's format: UTC yyyyMMdd date prefix + 32-hex uuid)."""
7373
global _origin
74-
if _origin is None:
75-
_origin = time.strftime("%Y%m%d", time.gmtime()) + uuid.uuid4().hex
76-
return _origin
74+
origin = _origin # local narrowing - checkers do not narrow module globals
75+
if origin is None:
76+
origin = time.strftime("%Y%m%d", time.gmtime()) + uuid.uuid4().hex
77+
_origin = origin
78+
return origin
7779

7880

7981
def elapsed_time(milliseconds: float) -> str:
@@ -135,9 +137,24 @@ def _app_block(self) -> dict[str, Any]:
135137
return {"name": self.app_name, "version": self.app_version,
136138
"description": self.description}
137139

138-
# aiohttp handlers must be coroutines - async is the framework contract
139-
# even when a handler has nothing to await
140-
async def handle_info(self, _request: web.Request) -> web.Response:
140+
async def handle(self, request: web.Request) -> web.Response:
141+
"""The single GET dispatcher (aiohttp handlers must be coroutines):
142+
/health awaits its dependency probes; the rest render synchronously."""
143+
match request.path:
144+
case "/info":
145+
return self._info()
146+
case "/info/routes":
147+
return self._routes()
148+
case "/env":
149+
return self._env()
150+
case "/health":
151+
return await self._health()
152+
case "/livenessprobe":
153+
return self._liveness_probe()
154+
case _: # the router only maps the five paths above here
155+
return web.Response(status=404, text="Not found")
156+
157+
def _info(self) -> web.Response:
141158
now = datetime.now(timezone.utc)
142159
return web.json_response({
143160
"app": self._app_block(),
@@ -151,7 +168,7 @@ async def handle_info(self, _request: web.Request) -> web.Response:
151168
"up_time": elapsed_time((now - self.start).total_seconds() * 1000),
152169
})
153170

154-
async def handle_routes(self, _request: web.Request) -> web.Response:
171+
def _routes(self) -> web.Response:
155172
public: dict[str, int] = {}
156173
private: dict[str, int] = {}
157174
for route, service in sorted(self.registry.routes().items()):
@@ -162,7 +179,7 @@ async def handle_routes(self, _request: web.Request) -> web.Response:
162179
"routing": {"public": public, "private": private},
163180
})
164181

165-
async def handle_env(self, _request: web.Request) -> web.Response:
182+
def _env(self) -> web.Response:
166183
config = app_config()
167184
environment = {name: os.environ.get(name, "")
168185
for name in _as_list(config.get("show.env.variables"))}
@@ -173,7 +190,7 @@ async def handle_env(self, _request: web.Request) -> web.Response:
173190
"env": {"environment": environment, "properties": properties},
174191
})
175192

176-
async def handle_health(self, _request: web.Request) -> web.Response:
193+
async def _health(self) -> web.Response:
177194
dependency: list[dict[str, Any]] = []
178195
# optional services never affect the overall status (engine semantics)
179196
await self._check_services(self.optional, required=False, dependency=dependency)
@@ -189,7 +206,7 @@ async def handle_health(self, _request: web.Request) -> web.Response:
189206
result["name"] = self.app_name
190207
return web.json_response(result, status=200 if up else 400)
191208

192-
async def handle_livenessprobe(self, _request: web.Request) -> web.Response:
209+
def _liveness_probe(self) -> web.Response:
193210
if self.healthy:
194211
return web.Response(text="OK")
195212
return web.Response(status=400, text=UNHEALTHY)

‎src/mercury_composable/server.py‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,9 @@ async def handle_event(self, request: web.Request) -> web.Response:
103103
def create_app(self) -> web.Application:
104104
app = web.Application(client_max_size=16 * 1024 * 1024)
105105
app.router.add_post("/api/event", self.handle_event)
106-
app.router.add_get("/info", self.actuator.handle_info)
107-
app.router.add_get("/info/routes", self.actuator.handle_routes)
108-
app.router.add_get("/env", self.actuator.handle_env)
109-
app.router.add_get("/health", self.actuator.handle_health)
110-
app.router.add_get("/livenessprobe", self.actuator.handle_livenessprobe)
106+
# the engines' actuator endpoints (see actuator.py)
107+
for path in ("/info", "/info/routes", "/env", "/health", "/livenessprobe"):
108+
app.router.add_get(path, self.actuator.handle)
111109
return app
112110

113111

‎tests/test_actuator.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ async def get_text(url: str) -> tuple[int, str]:
5757

5858

5959
def engine_contract_registry() -> FunctionRegistry:
60-
"""A health-check function speaking the engines' type=info/type=health contract."""
60+
"""A health check function speaking the engines' type=info/type=health contract."""
6161
registry = FunctionRegistry()
6262

6363
async def health(headers: dict[str, str], _body: Body):
@@ -193,5 +193,6 @@ def test_elapsed_time_matches_engine_rendering():
193193

194194

195195
def test_origin_is_stable_and_engine_shaped():
196-
assert app_origin() == app_origin() # minted once per process
197-
assert re.fullmatch(ORIGIN_SHAPE, app_origin())
196+
minted = app_origin()
197+
assert minted == app_origin() # minted once per process
198+
assert re.fullmatch(ORIGIN_SHAPE, minted)

0 commit comments

Comments
 (0)