The Sep 2026 decomposition (PR #102117) makes internal import paths a non-API: names now live in the focused modules that define them. This commit is the ONLY thing keeping the old paths alive, so external plugins have time to update. It is deliberately a single, unsquashed commit: git revert <this sha> removes every shim, stub and manifest at once on the announced date. Nothing in-tree may depend on these pointers: scripts/check_compat_pointers.py (wired into lint.yml) fails CI if it does. What it adds (see COMPAT_MANIFEST.md, compat_manifest.json): - 332 facade modules get one delimited `PLUGIN-COMPAT` block appended at the end of the file - 1,172 moved names resolved lazily via a module `__getattr__` (PEP 562) — never a top-level import, so no import cycles; facades that already had `__getattr__` get a chained one - 592 third-party/stdlib names the old modules used to expose, with their original import statements - 266 public definitions that had been deleted as unused, restored byte-for-byte from the pre-decomposition tree (+40 private helpers and 16 imports pulled in only because a restored definition needs them) - 3 deleted modules recreated as re-export stubs (gateway/startup_watchdog, hermes_cli/observability/ relay_runtime, tools/environments/modal_utils) - private names (`_x`) get no pointer: they were never API (3,792 skipped) Verified: all 335 touched modules import under a fresh HERMES_HOME and every manifest name resolves; the lint reports zero in-tree uses; ruff clean; targeted suites unchanged.
128 lines
5.2 KiB
Python
128 lines
5.2 KiB
Python
"""Minimal, optional systemd ``sd_notify`` support for the gateway."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import contextlib
|
|
import math
|
|
import os
|
|
import socket
|
|
|
|
|
|
def _notify_socket() -> str:
|
|
return os.environ.get("NOTIFY_SOCKET", "").strip() if hasattr(socket, "AF_UNIX") else ""
|
|
|
|
|
|
def notify(message: str) -> bool:
|
|
"""Send an sd_notify datagram if systemd configured it; failures never block gateway startup."""
|
|
if not (address := _notify_socket()) or not isinstance(message, str) or not message:
|
|
return False
|
|
try:
|
|
with socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) as sender:
|
|
sender.setblocking(False) # a full receiver buffer must not stall the event loop
|
|
# systemd's ``@abstract`` notation -> Python's leading-NUL address form
|
|
sender.connect("\0" + address[1:] if address.startswith("@") else address)
|
|
sender.send(message.encode("utf-8"))
|
|
return True
|
|
except (OSError, UnicodeError, ValueError):
|
|
return False
|
|
|
|
|
|
def watchdog_interval_seconds() -> float | None:
|
|
try:
|
|
interval = float(os.environ.get("WATCHDOG_USEC", "") if _notify_socket() else "") / 1e6
|
|
except (TypeError, ValueError):
|
|
return None
|
|
return interval if math.isfinite(interval) and interval > 0 else None
|
|
|
|
|
|
class SystemdWatchdog:
|
|
"""Feed systemd while the asyncio event loop continues to make progress."""
|
|
|
|
def __init__(self, *, config_enabled: bool = True, lag_tolerance_seconds: float | None = None):
|
|
self._config_enabled = bool(config_enabled)
|
|
self.interval_seconds = watchdog_interval_seconds()
|
|
self._lag_tolerance_seconds = lag_tolerance_seconds
|
|
self._task: asyncio.Task[None] | None = None
|
|
self._unhealthy = self._stopping = self._stopping_notified = False
|
|
|
|
enabled = property(lambda self: self._config_enabled and self.interval_seconds is not None)
|
|
unhealthy = property(lambda self: self._unhealthy)
|
|
task = property(lambda self: self._task)
|
|
|
|
def _lag_tolerance(self) -> float:
|
|
default = max(0.1, (self.interval_seconds or 0.0) * 0.25)
|
|
with contextlib.suppress(TypeError, ValueError):
|
|
value = float(self._lag_tolerance_seconds)
|
|
return max(0.0, value) if math.isfinite(value) else default
|
|
return default
|
|
|
|
def start(self) -> bool:
|
|
if not self.enabled:
|
|
return False
|
|
if self._task is not None and not self._task.done():
|
|
return True
|
|
try:
|
|
asyncio.get_running_loop()
|
|
except RuntimeError:
|
|
return False
|
|
self._stopping = self._unhealthy = self._stopping_notified = False
|
|
self._task = asyncio.create_task(self._run(), name="hermes-systemd-watchdog")
|
|
return True
|
|
|
|
def ready(self, status: str = "Gateway running") -> bool:
|
|
safe_status = str(status or "Gateway running").replace("\n", " ")
|
|
return self.enabled and notify(f"READY=1\nSTATUS={safe_status}")
|
|
|
|
def record_tick(self, *, scheduled_at: float, now: float) -> bool:
|
|
"""Feed systemd only when the event loop woke within its lag budget."""
|
|
if not self.enabled or self._stopping or self._unhealthy:
|
|
return False
|
|
try:
|
|
lag = float(now) - float(scheduled_at)
|
|
except (TypeError, ValueError):
|
|
lag = float("inf")
|
|
if not math.isfinite(lag) or lag > self._lag_tolerance():
|
|
self._unhealthy = True
|
|
notify("STATUS=watchdog unhealthy: event loop progress is late")
|
|
return False
|
|
notify("WATCHDOG=1")
|
|
return True
|
|
|
|
async def _run(self) -> None:
|
|
if self.interval_seconds is None:
|
|
return
|
|
cadence = max(0.01, self.interval_seconds / 2.0)
|
|
loop = asyncio.get_running_loop()
|
|
scheduled_at = loop.time() + cadence
|
|
with contextlib.suppress(asyncio.CancelledError):
|
|
while not self._stopping and not self._unhealthy:
|
|
await asyncio.sleep(max(0.0, scheduled_at - loop.time()))
|
|
now = loop.time()
|
|
if not self.record_tick(scheduled_at=scheduled_at, now=now):
|
|
return
|
|
scheduled_at += cadence
|
|
if scheduled_at < now:
|
|
scheduled_at = now + cadence
|
|
|
|
async def stop(self) -> None:
|
|
"""Stop feeding systemd and emit ``STOPPING=1`` at most once."""
|
|
self._stopping = True
|
|
task = self._task
|
|
if task is not None and task is not asyncio.current_task():
|
|
task.cancel() # no-op on a finished task
|
|
with contextlib.suppress(asyncio.CancelledError, Exception):
|
|
await task
|
|
self._task = None
|
|
if self.enabled and not self._stopping_notified:
|
|
notify("STOPPING=1")
|
|
self._stopping_notified = True
|
|
|
|
|
|
# ---- BEGIN PLUGIN-COMPAT (revert-scheduled; see COMPAT_MANIFEST.md) ----
|
|
# Names external plugins imported from this module before the Sep 2026 decomposition.
|
|
# Internal code MUST NOT use these (scripts/check_compat_pointers.py fails CI if it does).
|
|
# The whole block is removed by reverting the commit that added it.
|
|
from typing import Optional # noqa: F401,E402
|
|
# ---- END PLUGIN-COMPAT ----
|