diff options
Diffstat (limited to 'packages/meshbay-node/src/meshbay_node/daemon.py')
| -rw-r--r-- | packages/meshbay-node/src/meshbay_node/daemon.py | 113 |
1 files changed, 52 insertions, 61 deletions
diff --git a/packages/meshbay-node/src/meshbay_node/daemon.py b/packages/meshbay-node/src/meshbay_node/daemon.py index 5fc70fe..b34e710 100644 --- a/packages/meshbay-node/src/meshbay_node/daemon.py +++ b/packages/meshbay-node/src/meshbay_node/daemon.py @@ -888,6 +888,32 @@ def _resolve_group(cfg: Config, group: str | None) -> str: sys.exit(1) +def _systemctl_user(verb: str, unit: str, *, not_running_hint: str, + success: str, watch: str | None) -> None: + """ + Run `systemctl --user <verb> <unit>` and report the result. + + The lifecycle authority is the unit, not this process: systemd already + knows which PID it started, restarts it on failure (`Restart=on-failure` + in the unit) and reloads it correctly (`ExecReload=`). Anything this CLI + did instead — finding a process by pattern-matching its command line, + signalling it, respawning it — is a second, worse implementation of what + systemd is already doing, and pattern-matching a process list has already + hit a real developer's real running node by accident. + """ + import subprocess + + result = subprocess.run(["systemctl", "--user", verb, unit], + capture_output=True, text=True) + if result.returncode != 0: + detail = (result.stderr or result.stdout).strip() + print(detail or not_running_hint) + sys.exit(1) + print(success) + if watch: + print(watch) + + # ── Entry point ─────────────────────────────────────────────────────────────── def main() -> None: @@ -904,7 +930,8 @@ def main() -> None: "browser with this node | member list|invite|revoke|unpin " "| group list|add|remove | gek init|rotate | file list|rm " "| denylist show|clear | reload: re-read node.toml " - "| restart-daemon: full stop + start " + "(systemctl --user reload) | restart-daemon: restart " + "the systemd unit (systemctl --user restart) " "| calibrate-argon2: benchmark") parser.add_argument("subcommand", nargs="?", help="'pair' for operator; list|invite|revoke|unpin for " @@ -1159,69 +1186,33 @@ def main() -> None: if args.command == "reload": # Milestone 14.8. The daemon re-reads node.toml; groups that appeared or # whose roots changed are picked up without dropping live connections. - cfg = load_config(args.config or DEFAULT_CONFIG_PATH) - import os as _os - import signal as _signal - import subprocess as _subprocess - # `--` is pgrep's own end-of-options marker and must be its own argument; - # folded into the pattern it searches for a process literally called - # "-- -m …". Anchored to the end of the command line so it matches the - # daemon and never a shell that merely mentions it — the same trap - # deploy-node.sh documents, where an unanchored pattern kills the script. - pid_out = _subprocess.run( - ["pgrep", "-f", "--", r"-m meshbay_node\.daemon$"], - capture_output=True, text=True) - pids = [int(x) for x in pid_out.stdout.split()] - if not pids: - print("Node is not running — start it with: meshbay-node") - sys.exit(1) - for pid in pids: - _os.kill(pid, _signal.SIGHUP) - print(f"sent SIGHUP to {len(pids)} daemon process(es)") - print("watch the result: tail -f /tmp/meshbay-node.log") + # + # Delegated to systemd rather than hunting a PID with pgrep and signalling + # it directly: an unanchored (or merely unlucky) pattern match there has + # already SIGHUPed a developer's own running node by accident — see the + # comment this replaced, and test_cli_dispatch.py's stub_daemon fixture, + # which had to stub os.kill for exactly that reason. The unit already + # declares `ExecReload=/bin/kill -HUP $MAINPID`, so systemd sends the + # signal to the one process it actually started. + _systemctl_user( + "reload", "meshbay-node", + not_running_hint="Node is not running as a systemd unit — start it " + "with: systemctl --user start meshbay-node", + success="sent reload to meshbay-node", + watch="watch the result: journalctl --user -u meshbay-node -f") return if args.command == "restart-daemon": - import os as _os - import signal as _signal - import subprocess as _subprocess - cfg = load_config(args.config or DEFAULT_CONFIG_PATH) - pid_out = _subprocess.run( - ["pgrep", "-f", "--", r"-m meshbay_node\.daemon$"], - capture_output=True, text=True) - pids = [int(x) for x in pid_out.stdout.split()] - if pids: - for pid in pids: - _os.kill(pid, _signal.SIGTERM) - print(f"stopped {len(pids)} daemon process(es)") - for pid in pids: - try: - _os.waitpid(pid, 0) - except ChildProcessError: - import time as _time - _time.sleep(2) - else: - print("no running daemon found — starting fresh") - log_path = "/tmp/meshbay-node.log" - config_flag = ["--config", str(args.config)] if args.config else [] - _subprocess.Popen( - [sys.executable, "-m", "meshbay_node.daemon"] + config_flag, - stdout=open(log_path, "a"), - stderr=_subprocess.STDOUT, - start_new_session=True, - ) - import time as _time - _time.sleep(3) - pid_out2 = _subprocess.run( - ["pgrep", "-f", "--", r"-m meshbay_node\.daemon$"], - capture_output=True, text=True) - new_pids = [int(x) for x in pid_out2.stdout.split()] - if new_pids: - print(f"daemon started (PID {new_pids[0]})") - print(f"log: tail -f {log_path}") - else: - print(f"daemon may have failed to start — check {log_path}") - sys.exit(1) + # Same reasoning as reload: no PID hunting, no manual respawn — systemd + # already knows how to stop and start this unit, and does not need this + # process to guess where its log file is. + _systemctl_user( + "restart", "meshbay-node", + not_running_hint="meshbay-node is not installed as a systemd unit — " + "see packaging/systemd/", + success="meshbay-node restarted via systemd", + watch="check status: systemctl --user status meshbay-node\n" + "watch logs: journalctl --user -u meshbay-node -f") return if args.command == "denylist": |