watch

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package watch hosts runtime condition observers (spec §4.2).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DockerAvailable

func DockerAvailable() error

DockerAvailable reports whether the docker CLI is usable; watch creation refuses docker watches without it.

func MapDockerEvent

func MapDockerEvent(line []byte) (string, map[string]string)

MapDockerEvent maps one `docker events --format '{{json .}}'` line to a RuntimePulse event type; "" means ignore the line.

func MapDockerInspect

func MapDockerInspect(out string) string

MapDockerInspect maps `docker inspect --format '{{.State.Status}} {{if .State.Health}}{{.State.Health.Status}}{{end}}'` output to an event type for the initial check; "" means unknown.

func RunDockerWatch

func RunDockerWatch(ctx context.Context, cfg Config, container string, emit Emitter) error

RunDockerWatch performs the initial check via `docker inspect`, then streams `docker events` for the container, mapping lines to events. If the subprocess exits (docker daemon restart, …) it re-inspects (catching transitions missed in the gap) and restarts the stream after a backoff. Requires the docker CLI on PATH (validated at watch creation). Blocks until ctx is cancelled.

func RunFileWatch

func RunFileWatch(ctx context.Context, cfg Config, path string, emit Emitter) error

RunFileWatch watches a single file path via its parent directory (so creation is observable) and emits file.created/changed/removed. Initial check: an existing file emits file.created immediately. Debounce is TRAILING-EDGE: fs events reset a timer; when the burst goes quiet for the debounce window (Config.StabilityThreshold, default 500ms), one event is emitted reflecting the file's final state — so rm+recreate bursts report the truth (file.changed), never a stale file.removed. Blocks until ctx is cancelled.

func RunGitWatch

func RunGitWatch(ctx context.Context, cfg Config, repo string, emit Emitter) error

RunGitWatch watches a repository's .git/HEAD (branch switches → git.branch.changed, payload branch) and .git/refs/heads + packed-refs (ref updates → git.ref.changed). There is no initial event: git watching is pure edge (no boolean state). A push is not reliably detectable locally, hence no git.pushed (spec note, §4.1). Blocks until ctx is cancelled.

func RunPoller

func RunPoller(ctx context.Context, cfg Config, posType, negType, source string, probe Prober, emit Emitter)

RunPoller implements the spec's triggering semantics (§4.2) for polling watch types: the current state is emitted immediately on start (initial check), afterwards only state transitions emit, and a transition must hold for StabilityThreshold before it is believed (flap suppression). Blocks until ctx is cancelled.

func ValidateType

func ValidateType(t string) error

Types

type Config

type Config struct {
	Interval           time.Duration `json:"interval,omitempty"`
	StabilityThreshold time.Duration `json:"stabilityThreshold,omitempty"`
}

Config controls polling cadence and flap suppression. Durations marshal as nanoseconds in JSON (stored in the watches table).

func (Config) WithDefaults

func (c Config) WithDefaults() Config

WithDefaults fills zero values; a zero threshold means transitions emit immediately (no debounce).

type Emitter

type Emitter func(evType, source string, payload map[string]string)

Emitter delivers a watcher observation into the engine.

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager owns one goroutine per active watch inside the daemon.

func NewManager

func NewManager(emit Emitter) *Manager

func (*Manager) Run

func (m *Manager) Run(ctx context.Context, persisted []Watch)

Run records the lifetime ctx and arms all persisted watches (daemon boot re-arm). Must be called once before Start.

func (*Manager) RunningIDs

func (m *Manager) RunningIDs() []string

RunningIDs returns the ids of currently running watchers, sorted.

func (*Manager) Start

func (m *Manager) Start(w Watch) error

Start spawns the watcher for w; starting an already-running id is a no-op.

func (*Manager) Stop

func (m *Manager) Stop(id string)

Stop cancels the watcher for id (no-op if not running).

type Prober

type Prober func(ctx context.Context) bool

Prober reports whether the watched condition currently holds.

func HTTPProber

func HTTPProber(url string) Prober

HTTPProber: available = the target ITSELF answers with a 2xx/3xx status. Redirects are not followed — an app that 302s "/" → "/login" is up; the redirect target's health is not this watch's business.

func ProcessProber

func ProcessProber(pattern string) Prober

ProcessProber: running = some process's full command line contains pattern as a case-sensitive substring. Implemented with gopsutil on every platform (owner decision, windows-support spec §2) — identical semantics on macOS/Linux/Windows, no pgrep dependency. The prober's own process (the daemon) is excluded; as with pgrep before it, a pattern matching another runtimepulse invocation's argv will match.

func TCPProber

func TCPProber(addr string) Prober

TCPProber: available = the address accepts a TCP connection. This is the service-agnostic readiness check (postgres, redis, …).

type Watch

type Watch struct {
	ID        string    `json:"id"`
	Type      string    `json:"type"` // http|tcp|file|process|docker|git
	Target    string    `json:"target"`
	Config    Config    `json:"config"`
	CreatedAt time.Time `json:"createdAt"`
}

Watch is a persisted runtime condition observer.

Jump to

Keyboard shortcuts

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