Skip to content

The agent that grows with you

refactor(cli): share the pause-ack check between snapshot restore and data cleanup

kshitijk4poor942f2900cc6b
diff --git a/hermes_cli/cli_snapshot.py b/hermes_cli/cli_snapshot.py
index 72102c7078c..9470a512666 100644
--- a/hermes_cli/cli_snapshot.py
+++ b/hermes_cli/cli_snapshot.py
@@ -58,15 +58,14 @@ def restore_snapshot_command(cli, parts):
def _pause_owner(home, control_home, identity):
from gateway.control_socket import pause_gateway_for_update, request_unserve_profile
+ from hermes_cli.data_cleanup_holders import pause_acked
from hermes_cli.gateway_profile_lifecycle import _confirmed
if control_home != home:
ack = request_unserve_profile(control_home, home.name)
accepted = _confirmed(ack, "unserved", home.name) or (
isinstance(ack, dict) and ack.get("pending") and not ack.get("error"))
else:
- ack = pause_gateway_for_update(control_home, timeout=8)
- accepted = (ack is not None and (ack.get("pausing") or ack.get("already_stopping"))
- and identity is not None and ack.get("pid") == identity.get("pid"))
+ accepted = pause_acked(pause_gateway_for_update(control_home, timeout=8), identity)
if not accepted:
raise GatewayClientError("Gateway refused the snapshot maintenance handoff; nothing was restored. "
"Stop the gateway and retry.")
diff --git a/hermes_cli/data_cleanup_holders.py b/hermes_cli/data_cleanup_holders.py
index 3288a3e63ce..48dbe2590bb 100644
--- a/hermes_cli/data_cleanup_holders.py
+++ b/hermes_cli/data_cleanup_holders.py
@@ -37,6 +37,12 @@ def manual_gateway_identity(home: Path) -> dict | None:
return identity
+def pause_acked(response: dict | None, identity: dict | None) -> bool:
+ """True when the identified gateway itself accepted (or is already running) the drain."""
+ return bool(response and identity is not None and response.get("pid") == identity.get("pid")
+ and (response.get("pausing") or response.get("already_stopping")))
+
+
def _drain_manual_gateway(home: Path) -> None:
from gateway.control_socket import identify_gateway, pause_gateway_for_update
from gateway.status import get_process_start_time, get_running_pid_identity_strict, _pid_exists
@@ -51,9 +57,7 @@ def _drain_manual_gateway(home: Path) -> None:
if type(pid) is not int or pid <= 0 or start is None or get_process_start_time(pid) != start:
raise RuntimeError(f"gateway process identity is unverified for {home}")
response = pause_gateway_for_update(home)
- if not response or response.get("pid") != identity.get("pid") or not (
- response.get("pausing") or response.get("already_stopping")
- ):
+ if not pause_acked(response, identity):
raise RuntimeError(f"gateway refused to drain for {home}")
deadline = time.monotonic() + min(120.0, max(5.0, float(response.get("drain_timeout", 30.0)) + 5.0))
while time.monotonic() < deadline: