Skip to main content

Module daemon

Module daemon 

Source
Expand description

Daemonization for waratah start (spawn-self detached).

Default waratah start re-executes this same binary as a detached child carrying the original arguments plus the hidden --daemon-ready <path> marker, so the parent can return to the shell immediately and the cluster keeps running under waratah stop. --foreground (or --query / --no-http, which imply it) skips this module entirely.

§Parent side (run_detached)

  1. Resolve the run layout (log dir) and mint a per-attempt status file name — start-status-{pid}-{millis}.txt — so two concurrent waratah start attempts never share a handshake file: a “still starting” attempt’s late ready can never be attributed to another attempt, and no stale verdict can survive into a new attempt. The spawner removes the file on every exit.
  2. Spawn current_exe() with the parent’s own arguments plus --daemon-ready, fully detached: new process group (Unix) or DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP (Windows), stdin null, stdout → output.txt, stderr → error.txt (append). Storage children spawned later by the daemon inherit those fds, so their output lands in the same files with no per-child plumbing.
  3. Poll the status file for one line — ready or error: <message> — up to READY_TIMEOUT. The child writes ready only once startup is complete and the HTTP socket is bound; a first boot with geo/temporal auto-rebuilds can sit in between for far longer than the timeout, so a timeout with a live child is reported as “still starting” (exit 0), not as a failure.
  4. Ctrl+C / SIGTERM during the wait reaches only the foreground spawner (the daemon is in its own process group), so the spawner answers for the cluster: kill the starting group — SIGTERM, short grace, SIGKILL — clear the status file and the daemon’s pidfile, and fail the invocation.

§Child side (run_child)

Runs the normal dev::start path with three additions: the readiness status write (via DevOptions::ready_file, reported by run_session right before the serve loop), the file-backed debug sink (DevOptions::debug_file → debug.txt, truncated each start), and a pidfile even for the memory backend (the working directory) so waratah stop without --data-dir can find the daemon.

The daemon never changes its working directory: relative --data-dir paths and the memory backend’s cwd pidfile/logs depend on it.

Structs§

DetachPlan
What the spawner needs to detach a cluster and report the result.

Constants§

DEBUG_FILE
ERROR_FILE
KILL_GRACE 🔒
How long a cancelled start’s SIGTERM gets before the whole process group is SIGKILLed. Deliberately shorter than the daemon’s own 5s HTTP drain: a cancelled start prefers returning the prompt over a fully drained teardown.
OUTPUT_FILE
Log file names inside the log dir.
READY_TIMEOUT 🔒
How long the spawner waits for the daemon child’s ready line. Startup that outlives this (index auto-rebuilds on a first boot) is reported as “still starting” — the child keeps booting in the background.
STATUS_PREFIX 🔒
Per-attempt status file prefix in the log dir. The full name is start-status-{spawner pid}-{unix millis}.txt: two concurrent waratah start attempts always differ in pid, so they never share a handshake file.

Functions§

detach 🔒
Make command survive the parent and the terminal.
kill_starting_cluster 🔒
Kill a cluster cancelled mid-startup. The signal reached only the foreground spawner: the daemon sits in its own process group (see detach), together with the storage children it already spawned, so the whole group must be killed from here — SIGTERM first (the daemon’s own handler tears the children down and removes its pidfile), SIGKILL to the group past KILL_GRACE.
log_dir
Where the daemon writes its log files: {data_dir}/logs when a data dir is set, otherwise the working directory (memory backend).
print_daemonized 🔒
The parent’s success report: pid, URLs, log paths, stop hint.
read_status 🔒
First line of the status file, if it exists and is non-empty.
report_ready
Write the readiness line (called by run_session once startup is complete and the serve loop is about to begin).
run_child
The daemon-child half: run the normal start path and funnel the outcome into the status file the spawner is polling.
run_detached
Spawn this binary as a detached daemon and report the outcome.
status_file 🔒
Mint this attempt’s status path (see STATUS_PREFIX). No I/O.
wait_for_interrupt 🔒
Resolve when the operator interrupts the start wait: SIGINT or SIGTERM on Unix, Ctrl+C on Windows.
write_status 🔒
One-line status write. Newlines are flattened so the file always holds exactly one parseable line.