restart_notice.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. """Planned-restart marker: the CLI stamps it so the next boot says the gateway is back online.
  2. The gateway writes one itself only when the restart is in-band (SIGUSR1 / pause-for-update). A CLI
  3. ``stop`` + ``start`` — always on Windows, which has no SIGUSR1 — is otherwise indistinguishable from
  4. a plain stop, and the boot pass skips the "Gateway online" home-channel notice
  5. (``gateway/run_notifications.py::_send_home_channel_startup_notifications``).
  6. The reader is ``gateway/run.py::_planned_restart_notification_path``; the roundtrip test in
  7. ``tests/gateway/test_planned_restart_marker_cli.py`` pins the two together.
  8. """
  9. from __future__ import annotations
  10. import logging
  11. import time
  12. from pathlib import Path
  13. from hermes_constants import get_process_hermes_home
  14. from utils import atomic_json_write
  15. logger = logging.getLogger(__name__)
  16. PLANNED_RESTART_MARKER_FILENAME = ".restart_pending.json"
  17. def planned_restart_marker_path(home: Path | None = None) -> Path:
  18. return (home or get_process_hermes_home()) / PLANNED_RESTART_MARKER_FILENAME
  19. def write_planned_restart_marker(*, via_service: bool = False, detached: bool = False) -> None:
  20. """Record that the running gateway is going down FOR A RESTART; never blocks the restart.
  21. One writer for both restart kinds: the gateway's own in-band shutdown (``via_service`` /
  22. ``detached`` from its shutdown context) and the CLI, which has no in-band path of its own.
  23. Best-effort like ``gateway.status.write_planned_stop_marker``: an unwritable home costs the
  24. notice, not the restart. Wider than ``OSError`` on purpose — a marker write that raises for any
  25. other reason must still not leave the caller's ``stop()`` un-run.
  26. """
  27. try:
  28. atomic_json_write(
  29. planned_restart_marker_path(),
  30. {
  31. "requested_at": time.time(),
  32. "via_service": bool(via_service),
  33. "detached": bool(detached),
  34. },
  35. indent=None,
  36. )
  37. except Exception:
  38. logger.debug(
  39. "Could not record the planned restart; home channels get no online notice", exc_info=True
  40. )