update_serve_resume.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. """Fresh-process adapter for manual backends paused by historical updaters."""
  2. from __future__ import annotations
  3. import json
  4. from pathlib import Path
  5. import sys
  6. def main(context: Path, result: Path) -> int:
  7. handled = False
  8. try:
  9. request = json.loads(context.read_text(encoding="utf-8-sig"))
  10. root = Path(request["root"])
  11. token = request["stopped_serves"]
  12. if not token.get("pending"):
  13. handled = True
  14. return 0
  15. sys.path.insert(0, str(root))
  16. from pm.environments import activate_dependencies
  17. activate_dependencies(root)
  18. from hermes_cli.dashboard_procs import _filter_dashboard_respawn_candidates
  19. from hermes_cli.main_dashboard import _respawn_dashboard_processes
  20. candidates = []
  21. skipped = 0
  22. for entry in token.get("entries") or []:
  23. port = entry.get("port")
  24. if (entry.get("purpose") not in ("serve", "dashboard")
  25. or type(port) is not int or not 0 < port <= 65535):
  26. skipped += 1
  27. continue
  28. command = [sys.executable, str(root / "hermes")]
  29. profile = entry.get("profile")
  30. if profile and profile != "default":
  31. command += ["--profile", str(profile)]
  32. command.append(entry["purpose"])
  33. if entry.get("host"):
  34. command += ["--host", str(entry["host"])]
  35. command += ["--port", str(port)]
  36. candidates.append((entry.get("pid", 0), command, entry.get("hermes_home") or None))
  37. commands = _filter_dashboard_respawn_candidates(candidates, own_home=request.get("home"))
  38. skipped += len(candidates) - len(commands)
  39. # An acknowledged attempt is terminal even if spawning failed: replaying
  40. # the whole token at atexit would duplicate the successful backends.
  41. failed = _respawn_dashboard_processes(commands) if commands else []
  42. handled = True
  43. if skipped or failed:
  44. print("Some stopped backends could not be relaunched; restart them manually.", file=sys.stderr)
  45. return 1
  46. return 0
  47. except Exception as exc:
  48. print(f"Stopped serve recovery failed: {exc}", file=sys.stderr, flush=True)
  49. return 1
  50. finally:
  51. result.write_text(json.dumps({"serves_handled": handled}), encoding="utf-8")
  52. if __name__ == "__main__":
  53. raise SystemExit(main(Path(sys.argv[1]), Path(sys.argv[2])))