update_cmd_common.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. """Shared leaf helpers for the ``hermes update`` modules (no Hermes imports; no cycle)."""
  2. import logging
  3. import sys
  4. from contextlib import contextmanager
  5. # Log-record parity with the origin module.
  6. logger = logging.getLogger("hermes_cli.update_cmd")
  7. def _record_stop(reason: str, *, without_receipt: str | None = None) -> None:
  8. """Name the exit about to stop this update as one closed token (see update_receipt.record_stop_reason).
  9. ``without_receipt`` (``refused``/``failed``) is for an exit that fires before the receipt
  10. opens: a metrics row only. The receipt module is read from ``sys.modules``, never imported:
  11. an older updater can load this tree after the swap while its own, older ``update_receipt``
  12. (without these calls) stays loaded.
  13. """
  14. receipt = sys.modules.get("hermes_cli.update_receipt")
  15. if without_receipt is None:
  16. record = getattr(receipt, "record_stop_reason", None)
  17. if record is not None:
  18. record(reason)
  19. elif (record := getattr(receipt, "record_stop_without_receipt", None)) is not None:
  20. record(reason, without_receipt)
  21. @contextmanager
  22. def _best_effort(message: str):
  23. """Run a non-critical update step; swallow ``Exception`` and log it at debug.
  24. The updater must never die on bookkeeping (receipt, notices, cache seeds):
  25. ``message`` is the ``%s``-style debug line the inline ``try/except`` used.
  26. """
  27. try:
  28. yield
  29. except Exception as exc:
  30. logger.debug(message, exc)