supervisor

package
v0.37.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 16, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package supervisor manages the lifecycle of a single supervised service process: spawning, log capture, restart policy, and graceful cancellation.

Index

Constants

This section is empty.

Variables

View Source
var DefaultPolicy = RestartPolicy{
	Mode:               RestartOnFailure,
	MaxRestarts:        10,
	StartupMaxRestarts: 3,
	StartupGrace:       2 * time.Second,
	InitialDelay:       500 * time.Millisecond,
	MaxDelay:           30 * time.Second,
	ResetAfter:         60 * time.Second,
}

DefaultPolicy matches the defaults in the Rust implementation.

Functions

func KillTree added in v0.34.3

func KillTree(pid int, grace time.Duration)

KillTree terminates pid and all of its descendants. Descendants are collected before any signal is sent (killing the parent reparents survivors, losing the tree). Each process gets a graceful Terminate, then a forceful Kill after grace if still alive. Cross-platform via gopsutil — used to reap orphaned service processes left by a crashed devrig, where no process-group handle from this run is available.

func PIDFilePath

func PIDFilePath(stateDir string) string

PIDFilePath returns the path to the devrig master PID file.

func ProcStartTimeMs added in v0.34.3

func ProcStartTimeMs(pid int) (int64, bool)

ProcStartTimeMs returns a process's creation time (milliseconds since the Unix epoch), or ok=false if it can't be inspected (gone, or no permission). Combined with the PID it forms a reuse-proof identity: a recycled PID belongs to a process with a different start time.

func RemovePIDFile

func RemovePIDFile(stateDir string)

RemovePIDFile removes the PID file.

func SameProcess added in v0.34.3

func SameProcess(pid int, wantStartMs int64) bool

SameProcess reports whether pid currently refers to the same process that had the given start time, guarding against PID reuse. wantStartMs == 0 means the start time is unknown (e.g. state written by an older devrig): fall back to a liveness-only check so behaviour degrades gracefully rather than refusing.

func WritePIDFile

func WritePIDFile(stateDir string) error

WritePIDFile writes the current process PID to the state directory.

Types

type RestartMode

type RestartMode int

RestartMode determines when a process should be restarted.

const (
	RestartOnFailure RestartMode = iota // restart only on non-zero exit
	RestartAlways                       // restart regardless of exit code
	RestartNever                        // never restart
)

type RestartPolicy

type RestartPolicy struct {
	Mode               RestartMode
	MaxRestarts        uint32
	StartupMaxRestarts uint32
	StartupGrace       time.Duration
	InitialDelay       time.Duration
	MaxDelay           time.Duration
	ResetAfter         time.Duration
}

RestartPolicy holds all tuning knobs for the restart loop.

func PolicyFromConfig

func PolicyFromConfig(cfg *config.RestartConfig) RestartPolicy

PolicyFromConfig converts a config RestartConfig to a RestartPolicy.

type Supervisor

type Supervisor struct {
	Name       string
	Command    string
	WorkingDir string
	Env        map[string]string
	Policy     RestartPolicy
	// contains filtered or unexported fields
}

Supervisor manages a single service process.

func New

func New(
	name, command, workingDir string,
	env map[string]string,
	policy RestartPolicy,
	logs *events.Broadcaster,
	evts *events.Broadcaster,
	stateDir string,
) *Supervisor

New creates a Supervisor.

func (*Supervisor) Run

func (s *Supervisor) Run(ctx context.Context) error

Run supervises the process until ctx is cancelled or the restart budget is exhausted. The returned error is non-nil only if the process could not be spawned at all.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL