sessions_owner.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. """Bulk prune while the gateway is up: the owner applies it, this process never writes.
  2. The one always-on gateway owns ``state.db``; a CLI rewriting it underneath is the second writer
  3. the held-store refusal exists to stop. So when a ready owner serves this home, ``hermes sessions
  4. prune`` reads its preview from a read-only view and sends the selected filters to the owner
  5. (``session.prune``), which deletes through the same ``retire_prunable`` path housekeeping uses.
  6. With no gateway, prune stays the local command it always was (holder scan, ``--force``).
  7. """
  8. from __future__ import annotations
  9. #: A cron-heavy store prunes tens of thousands of rows in one transaction; the owner shields the
  10. #: write, so a short client budget would only misreport a prune that still commits.
  11. PRUNE_RPC_TIMEOUT = 600
  12. def live_owner():
  13. """The ready gateway endpoint serving this home, else None. Discovery only: never starts one."""
  14. from hermes_cli.gateway_runtime import discover_gateway_endpoint
  15. from hermes_constants import get_hermes_home
  16. found = discover_gateway_endpoint(get_hermes_home().resolve())
  17. return found.endpoint if found.state == "ready" else None
  18. def owner_prune(endpoint, **params) -> dict:
  19. """Run one ``session.prune`` on *endpoint*; raises ``GatewayClientError`` on refusal/loss."""
  20. import asyncio
  21. from hermes_cli.gateway_client import connect_gateway
  22. async def _run():
  23. async with connect_gateway(endpoint) as client:
  24. return await client.rpc("session.prune", _timeout=PRUNE_RPC_TIMEOUT, **params)
  25. return asyncio.run(_run())
  26. def report_owner_failure(exc) -> int:
  27. print(f"Error: the gateway did not complete the prune ({exc}). Run `hermes sessions prune --dry-run` "
  28. "with the same filters to see what is left.")
  29. return 1