New commands: - `gt up` - Idempotent boot command that brings up all services: Daemon, Deacon, Mayor, and Witnesses for all rigs - `gt down` - Graceful shutdown of all services Doctor improvements: - New daemon check verifies daemon is running - Fixable with `gt doctor --fix` to auto-start daemon The system can run degraded (any services down) but `gt up` ensures a fully operational Gas Town with one idempotent command. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
112 lines
2.2 KiB
Go
112 lines
2.2 KiB
Go
package doctor
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
"time"
|
|
|
|
"github.com/steveyegge/gastown/internal/daemon"
|
|
)
|
|
|
|
// DaemonCheck verifies the daemon is running.
|
|
type DaemonCheck struct {
|
|
FixableCheck
|
|
}
|
|
|
|
// NewDaemonCheck creates a new daemon check.
|
|
func NewDaemonCheck() *DaemonCheck {
|
|
return &DaemonCheck{
|
|
FixableCheck: FixableCheck{
|
|
BaseCheck: BaseCheck{
|
|
CheckName: "daemon",
|
|
CheckDescription: "Check if Gas Town daemon is running",
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
// Run checks if the daemon is running.
|
|
func (c *DaemonCheck) Run(ctx *CheckContext) *CheckResult {
|
|
running, pid, err := daemon.IsRunning(ctx.TownRoot)
|
|
if err != nil {
|
|
return &CheckResult{
|
|
Name: c.Name(),
|
|
Status: StatusError,
|
|
Message: "Failed to check daemon status",
|
|
Details: []string{err.Error()},
|
|
}
|
|
}
|
|
|
|
if running {
|
|
// Get more info about daemon state
|
|
state, err := daemon.LoadState(ctx.TownRoot)
|
|
details := []string{}
|
|
if err == nil && !state.StartedAt.IsZero() {
|
|
uptime := time.Since(state.StartedAt).Round(time.Second)
|
|
details = append(details, "Uptime: "+uptime.String())
|
|
if state.HeartbeatCount > 0 {
|
|
details = append(details, "Heartbeats: "+string(rune(state.HeartbeatCount)))
|
|
}
|
|
}
|
|
|
|
return &CheckResult{
|
|
Name: c.Name(),
|
|
Status: StatusOK,
|
|
Message: "Daemon is running (PID " + itoa(pid) + ")",
|
|
Details: details,
|
|
}
|
|
}
|
|
|
|
return &CheckResult{
|
|
Name: c.Name(),
|
|
Status: StatusWarning,
|
|
Message: "Daemon is not running",
|
|
FixHint: "Run 'gt daemon start' or 'gt doctor --fix'",
|
|
}
|
|
}
|
|
|
|
// Fix starts the daemon.
|
|
func (c *DaemonCheck) Fix(ctx *CheckContext) error {
|
|
// Find gt executable
|
|
gtPath, err := os.Executable()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Start daemon in background
|
|
cmd := exec.Command(gtPath, "daemon", "run")
|
|
cmd.Dir = ctx.TownRoot
|
|
cmd.Stdin = nil
|
|
cmd.Stdout = nil
|
|
cmd.Stderr = nil
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Wait a moment for daemon to initialize
|
|
time.Sleep(300 * time.Millisecond)
|
|
|
|
return nil
|
|
}
|
|
|
|
// itoa is a simple int to string helper
|
|
func itoa(i int) string {
|
|
if i == 0 {
|
|
return "0"
|
|
}
|
|
s := ""
|
|
neg := i < 0
|
|
if neg {
|
|
i = -i
|
|
}
|
|
for i > 0 {
|
|
s = string(rune('0'+i%10)) + s
|
|
i /= 10
|
|
}
|
|
if neg {
|
|
s = "-" + s
|
|
}
|
|
return s
|
|
}
|