summaryrefslogtreecommitdiffstats
path: root/packages/meshbay-node/src/meshbay_node/daemon.py
diff options
context:
space:
mode:
authorChristophe Besson <cbesson@gmail.com>2026-08-22 18:26:22 +0200
committerChristophe Besson <cbesson@gmail.com>2026-08-22 18:26:22 +0200
commit8dc11dc05a35a5d64ba4d2c892ccc01c7bfae3da (patch)
treee172bb4d596bde53c48ce2ca3ef2d59c98968147 /packages/meshbay-node/src/meshbay_node/daemon.py
parentb4baa4770d517ea7d1d25bb0ac50fc7c989531d6 (diff)
downloadmeshbay-8dc11dc05a35a5d64ba4d2c892ccc01c7bfae3da.tar.gz
feat(node): systemd service panel on the Node page, and a clean CLI restart
Add a status panel at the top of the Node page — always visible, even before an MNP connection exists — showing the meshbay-node systemd unit's own state (via `systemctl --user show`, main process only) with Start/Stop/Restart controls. This is the piece the rest of the page cannot provide: it has to work while the daemon is stopped or crash- looping, which the MNP-based sections require the daemon to already answer. While touching node lifecycle: `reload` and `restart-daemon` in the CLI shelled out to pgrep + SIGTERM/SIGHUP and respawned the process by hand, logging to a hardcoded /tmp path. That pattern already SIGHUPed a developer's own running node by accident once (see the old test_cli_dispatch.py comment). Both now delegate to `systemctl --user reload|restart meshbay-node`, which the unit already supports correctly (ExecReload=, Restart=on-failure). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016SF6RKNBKg9qejmoMJ9ybA
Diffstat (limited to 'packages/meshbay-node/src/meshbay_node/daemon.py')
-rw-r--r--packages/meshbay-node/src/meshbay_node/daemon.py113
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":