Skip to content

feat(cli): worker start -d|--daemon to detach as a background daemon - #11

Merged
ralyodio merged 2 commits into
masterfrom
feat/worker-daemon-flag
Jul 19, 2026
Merged

feat(cli): worker start -d|--daemon to detach as a background daemon#11
ralyodio merged 2 commits into
masterfrom
feat/worker-daemon-flag

Conversation

@ralyodio

Copy link
Copy Markdown
Contributor

What

Adds c0mpute worker start -d|--daemon to run the worker detached as a background daemon, and wires up the previously-stubbed worker stop / worker status commands.

Why

worker start only ran in the foreground and blocked forever; worker stop was a stub ([stub] no running worker to stop) and status only dumped the config. There was no way to run the worker as a daemon short of nohup/systemd.

How

  • New -d/--daemon flag on worker start. When set, the process forks into the background, redirects stdout/stderr to ~/.local/share/c0mpute/worker.log, and writes an flock'd PID file at ~/.local/share/c0mpute/worker.pid.
  • Fork-before-runtime: main is no longer #[tokio::main]. It parses args and daemonizes while still single-threaded (forking after the multi-threaded Tokio runtime spawns worker threads is unsafe), then builds the runtime manually and drives everything via run_app().
  • worker stop → reads the PID file and sends SIGTERM; clears stale PID files.
  • worker status → reports running (pid N) / not running / stale pid, then the config.
  • Unix-only bits are #[cfg(unix)] gated with clear errors on other platforms (adds daemonize + libc as cfg(unix) deps).

Testing

Verified end-to-end on Linux:

  • worker start -d returns immediately; process reparents to PID 1 (detached), PID + log files written.
  • worker statusrunning (pid N); supervisor tracing confirmed landing in worker.log.
  • worker stop → SIGTERM, process terminates.
  • Stale-PID path: a dead PID is reported as stale by status and cleared by stop.
  • Both cargo build (debug) and cargo build --release pass clean.

Note

After a live stop, status reports stale pid until the next start/stop — the daemonize crate holds the pidfile flock and doesn't unlink on exit. Left intentionally: the flock is what prevents a double-start race, so unlinking on stop would open a brief two-worker window.

🤖 Generated with Claude Code

Add `c0mpute worker start -d|--daemon` which forks into the background,
redirects stdout/stderr to ~/.local/share/c0mpute/worker.log, and writes
an flock'd PID file at ~/.local/share/c0mpute/worker.pid.

The fork happens in a plain sync `main` before the Tokio runtime is built
(forking after the multi-threaded runtime spawns worker threads is unsafe),
then the runtime is built manually and drives the rest via run_app().

Wire up the previously-stubbed lifecycle commands:
- `worker stop`   -> SIGTERM the PID from the pid file; clears stale PIDs
- `worker status` -> reports running/not-running/stale plus the config

Unix-only bits are cfg-gated with clear errors on other platforms.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@socket-security

socket-security Bot commented Jul 18, 2026

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Addedcargo/​daemonize@​0.5.010010093100100

View full report

Add a ready-to-use systemd *user* unit for running the worker as a managed
background service. Runs `worker start` in the foreground (Type=simple, no
--daemon) so systemd owns the lifecycle, restarts on crash, and captures
output to journald. Targets the per-user ~/.c0mpute/bin install layout via
%h, with PATH set so the coinpay/infernet peer CLIs resolve.

Document both paths in the README: the self-managed `worker start -d` daemon
and the systemd unit.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@ralyodio
ralyodio merged commit 1291cf9 into master Jul 19, 2026
8 checks passed
@ralyodio
ralyodio deleted the feat/worker-daemon-flag branch July 19, 2026 03:08
ralyodio added a commit that referenced this pull request Jul 19, 2026
Adds background-worker lifecycle from #11 and #12:
  - worker start -d|--daemon  detach and run as a daemon (PID file + log)
  - worker start -a|--attach  stream the log, Ctrl-D/Ctrl-C to detach
  - worker stop / worker status now functional (were stubs)
Ships the systemd user unit at scripts/systemd/c0mpute-worker.service.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant