engine

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2026 License: GPL-3.0 Imports: 19 Imported by: 0

Documentation

Overview

Package engine is the orchestration core of env-starter: a foreground supervisor that starts and stops environments made of globally-shared commands, respecting the dependency graph declared in each environment's workflow.

A command is identified by its name and runs once globally. Starting an environment records that environment as a holder of the command (and waits for it to be healthy if already running). Stopping an environment removes its hold; a command is actually torn down only when no environment holds it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CmdState

type CmdState string

CmdState is the lifecycle state of a command.

const (
	CmdPending    CmdState = "pending"
	CmdStarting   CmdState = "starting"
	CmdHealthy    CmdState = "healthy"
	CmdDone       CmdState = "done"
	CmdStopping   CmdState = "stopping"
	CmdError      CmdState = "error"
	CmdTimeout    CmdState = "timeout"
	CmdStopped    CmdState = "stopped"
	CmdRestarting CmdState = "restarting"
)

type Engine

type Engine struct {

	// GracePeriod is how long a service is given to exit after SIGINT before
	// SIGKILL. Exposed as a field so tests can shrink it.
	GracePeriod time.Duration
	// ProbeTimeout / ProbeInterval are the defaults used when a readiness probe
	// does not specify its own. Exposed so tests can shrink them.
	ProbeTimeout  time.Duration
	ProbeInterval time.Duration
	// contains filtered or unexported fields
}

Engine supervises environments and their commands.

cfg, cmdOf, envsOf, and envEnvOf are guarded by mu but are never mutated in place: a config reload (see ApplyConfig) replaces each with a newly built value under the lock, so a reader that copies a reference while holding mu keeps a valid, immutable snapshot after releasing it.

func New

func New(cfg *config.Config) (*Engine, error)

New builds an Engine from cfg, validating that every environment workflow references commands that resolve to defined commands.

func (*Engine) ApplyConfig added in v1.7.0

func (e *Engine) ApplyConfig(cfg *config.Config) error

ApplyConfig atomically swaps in a new configuration and reconciles running state selectively (see diffConfig/reloadPlan for what "selectively" means). It returns an error only when the new config is structurally invalid, in which case nothing is mutated. Once it returns nil the new config is in force for every reader; the restarts it schedules run asynchronously and report progress via Events(), so a restart failure surfaces as CmdError / EnvDegraded rather than as an error from this call.

func (*Engine) CmdRetries added in v0.2.0

func (e *Engine) CmdRetries(command string) (attempts, max int)

CmdRetries returns the number of restart attempts consumed in the current cycle and the configured maximum. Both values are 0 for a command that has never been (or never can be) auto-restarted.

func (*Engine) CmdState

func (e *Engine) CmdState(command string) CmdState

CmdState returns the current state of a command. A command that has never been started (or that has been fully torn down) reports CmdPending.

func (*Engine) EnvState

func (e *Engine) EnvState(env string) EnvState

EnvState returns the current state of an environment (EnvStopped if unknown).

func (*Engine) Environments

func (e *Engine) Environments() []EnvInfo

Environments returns the environments in config order.

func (*Engine) Events

func (e *Engine) Events() <-chan Event

Events returns the buffered event channel. Sends are non-blocking: if no one is reading and the buffer is full, events are dropped.

func (*Engine) IsUnmanaged added in v1.6.0

func (e *Engine) IsUnmanaged(command string) bool

IsUnmanaged reports whether command is currently healthy only because an external process already satisfied its readiness probe before env-starter spawned anything. False for a command that has never been started, that env-starter actually launched itself, or that is unknown.

func (*Engine) LogPath

func (e *Engine) LogPath(command string) string

LogPath returns the on-disk path where the given command's logs are written.

func (*Engine) Logs

func (e *Engine) Logs(command string) []string

Logs returns a copy of the command's ring-buffer lines, or an empty slice if the command has no logs.

func (*Engine) PurgeOldLogs added in v0.4.1

func (e *Engine) PurgeOldLogs() ([]string, error)

PurgeOldLogs removes log files older than logbuf.LogRetention from the logs directory. It is safe to call before any command starts. A missing directory is not an error. Returns the base names of removed files.

func (*Engine) ResolveEnv added in v1.6.0

func (e *Engine) ResolveEnv(envName, command string) []ResolvedEnvVar

ResolveEnv resolves the env variables visible to the given environment or command, with full layer provenance. Pass a non-empty command to resolve exactly what that command's process receives (OS env, overlaid by the union of its current holders' environment-level env, overlaid by the command's own env — the same layering effectiveEnv applies at launch). Otherwise pass a non-empty envName to resolve what every command in that environment's workflow inherits before its own overrides (OS env, overlaid by the environment's own env). Returns nil for an unknown name.

func (*Engine) RestartCommand added in v1.3.0

func (e *Engine) RestartCommand(name string) error

RestartCommand restarts a single running command in place, preserving its environment holders. Unlike automatic restarts it is user-initiated and ignores the restart policy — it recycles the process even when auto-restart is disabled for this command. Returns an error if the command is unknown or currently has no holders (i.e. it is not running).

func (*Engine) Shutdown

func (e *Engine) Shutdown(ctx context.Context)

Shutdown stops every currently-running command gracefully, respecting ctx as an overall deadline. All holders are cleared.

func (*Engine) StartEnvironment

func (e *Engine) StartEnvironment(env string) error

StartEnvironment starts the named environment asynchronously. It returns an error only if the environment is unknown; startup progress is reported via Events() and the state accessors.

If the environment is currently in a failed state (EnvError or EnvDegraded), it is first reset: its workflow commands are released so that errored commands — whose refcount was never decremented on failure — are recreated fresh. Commands still shared with other running environments are not torn down.

func (*Engine) StopEnvironment

func (e *Engine) StopEnvironment(env string) error

StopEnvironment decrements the reference count of every command in the named environment's workflow. A command is actually torn down only when its reference count reaches zero. Returns an error if the environment is unknown.

func (*Engine) StoppingCommands

func (e *Engine) StoppingCommands() []StoppingCommand

StoppingCommands returns the commands currently being torn down, in config order. Each entry carries the time elapsed since stopping began and the per-process SIGINT→SIGKILL grace budget. Returns an empty slice when nothing is stopping.

func (*Engine) WorkflowCommands

func (e *Engine) WorkflowCommands(env string) []string

WorkflowCommands returns the command names in the given environment's workflow order. Returns nil for an unknown environment.

type EnvInfo

type EnvInfo struct {
	Name        string
	Description string
	AutoStart   bool
}

EnvInfo is a lightweight description of an environment.

type EnvLayer added in v1.6.0

type EnvLayer struct {
	Value  string
	Source EnvSource
}

EnvLayer is one layer's contribution to a key: the value it set, and which source set it.

type EnvSource added in v1.6.0

type EnvSource string

EnvSource identifies which layer produced a resolved env value.

const (
	EnvSourceOS          EnvSource = "os"
	EnvSourceEnvironment EnvSource = "environment"
	EnvSourceCommand     EnvSource = "command"
)

type EnvState

type EnvState string

EnvState is the lifecycle state of an environment.

const (
	EnvStopped  EnvState = "stopped"
	EnvStarting EnvState = "starting"
	EnvRunning  EnvState = "running"
	EnvDegraded EnvState = "degraded"
	EnvStopping EnvState = "stopping"
	EnvError    EnvState = "error"
)

type Event

type Event struct {
	Kind        string // "environment" | "command"
	Environment string
	Command     string
	EnvState    EnvState
	CmdState    CmdState
	Err         error
}

Event is emitted on every state change. Command events carry Command and CmdState; environment events carry Environment and EnvState.

type ResolvedEnvVar added in v1.6.0

type ResolvedEnvVar struct {
	Key      string
	Winning  EnvLayer
	Shadowed []EnvLayer
}

ResolvedEnvVar is one key's full provenance: the value actually applied (Winning) plus every lower-priority layer it shadows, nearest first, so a caller can show what a variable overrides without a second lookup.

type StoppingCommand

type StoppingCommand struct {
	Command string
	Elapsed time.Duration
	Grace   time.Duration
}

StoppingCommand describes a command currently being torn down, for the shutdown screen. Elapsed is the time since it began stopping; Grace is the per-process SIGINT→SIGKILL budget.

Jump to

Keyboard shortcuts

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