1
Daemon Lifecycle
Virgil edited this page 2026-03-11 12:05:31 +00:00
Table of Contents
Daemon Lifecycle
Module: forge.lthn.ai/core/go-process
Overview
The Daemon struct orchestrates three concerns: single-instance enforcement (PID file), health monitoring (HTTP server), and fleet tracking (registry).
Lifecycle
NewDaemon(opts) -> Start() -> SetReady(true) -> Run(ctx) -> [ctx cancelled] -> Stop()
Start()
- Acquire PID file (fails if another instance holds it)
- Start health server (HTTP
/health+/ready) - Auto-register in daemon registry (if configured)
Run(ctx)
Blocks until ctx.Done(), then calls Stop().
Stop()
- Set readiness to false (health server returns 503)
- Graceful shutdown of health server (with timeout)
- Release PID file
- Unregister from daemon registry
Registry
Registry tracks daemons via JSON files in a directory (default ~/.core/daemons/).
File naming: {code}-{daemon}.json (slashes replaced with dashes).
Each file contains:
{
"code": "myapp",
"daemon": "server",
"pid": 12345,
"health": "127.0.0.1:9090",
"started": "2026-03-10T12:00:00Z"
}
List() and Get() automatically prune entries whose PIDs are no longer alive (checked via kill -0).
Health Server
Endpoints:
GET /health— Runs all registeredHealthCheckfunctions. Returns 200 if all pass, 503 if any fail.GET /ready— Returns 200 whenSetReady(true), 503 otherwise.
WaitForHealth(addr, timeoutMs) polls /health every 200ms until 200 or timeout.