worker

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2026 License: AGPL-3.0 Imports: 13 Imported by: 0

Documentation

Overview

Package worker defines interfaces and implementations for managed units of work.

It provides a uniform `Worker` interface to manage the lifecycle (Start, Stop, Wait) of various execution units such as OS processes, Goroutines, or Containers.

key features:

  • **Worker Interface**: A standard contract for starting, stopping, and waiting for work.
  • **Functional Worker**: Simple adapter `FromFunc` to turn any function into a Worker.
  • **Process Worker**: A wrapper around `os/exec` that ensures process hygiene (Fail-Closed) using `pkg/proc` logic (Job Objects on Windows, PDeathSig on Linux).
  • **Container Worker**: A bridge to manage containerized workloads via `pkg/container`.
  • **Handover Protocol**: Standard environment variables (`LIFECYCLE_RESUME_ID`, `LIFECYCLE_PREV_EXIT`) to pass context across worker restarts.

This package is foundational for the Supervisor pattern (v1.3+).

Index

Constants

View Source
const (
	// EnvResumeID is the unique session identifier for a worker.
	// It remains constant across restarts within the same supervisor lifecycle.
	EnvResumeID = "LIFECYCLE_RESUME_ID"

	// EnvPrevExit is the exit code of the previous execution of this worker.
	// It is injected by the supervisor upon restart.
	EnvPrevExit = "LIFECYCLE_PREV_EXIT"
)

Standard environment variables for the Handover Protocol.

Variables

This section is empty.

Functions

func MermaidState

func MermaidState(s State) string

MermaidState returns a simple Mermaid state diagram (FSM) for a single worker. It visualizes the lifecycle transitions: Pending -> Running -> Stopped/Failed. This is useful for understanding the internal behavior of a worker type.

func MermaidTree

func MermaidTree(s State) string

MermaidTree returns a Mermaid diagram string representing the worker structure (Tree). It renders a hierarchical tree (graph TD) showing parent-child relationships.

func RenderTreeFragment

func RenderTreeFragment(sb *strings.Builder, s State, rootID string)

RenderTreeFragment appends the Mermaid tree nodes and links to the provided builder. This is useful for building composite diagrams.

Types

type ContainerWorker

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

ContainerWorker is a Worker that manages a container via the container.Container interface.

func NewContainerWorker

func NewContainerWorker(name string, c container.Container) *ContainerWorker

NewContainerWorker creates a new Worker for a given container.

func (*ContainerWorker) Start

func (cw *ContainerWorker) Start(ctx context.Context) error

func (*ContainerWorker) State

func (cw *ContainerWorker) State() State

func (*ContainerWorker) Stop

func (cw *ContainerWorker) Stop(ctx context.Context) error

func (*ContainerWorker) String

func (cw *ContainerWorker) String() string

func (*ContainerWorker) Wait

func (cw *ContainerWorker) Wait() <-chan error

type EnvInjector

type EnvInjector interface {
	SetEnv(key, value string)
}

EnvInjector is an optional interface for workers that support environment variable injection.

type ProcessWorker

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

ProcessWorker is a Worker that manages an OS process.

func NewProcessWorker

func NewProcessWorker(name string, nameCmd string, args ...string) *ProcessWorker

NewProcessWorker creates a new ProcessWorker for the given command.

func (*ProcessWorker) SetEnv

func (p *ProcessWorker) SetEnv(key, value string)

SetEnv adds an environment variable to the process.

func (*ProcessWorker) Start

func (p *ProcessWorker) Start(ctx context.Context) error

Start starts the OS process.

func (*ProcessWorker) State

func (p *ProcessWorker) State() State

State returns a snapshot of the worker's status.

func (*ProcessWorker) Stop

func (p *ProcessWorker) Stop(ctx context.Context) error

Stop sends a signal to the process to terminate.

func (*ProcessWorker) String

func (p *ProcessWorker) String() string

String returns the worker name.

func (*ProcessWorker) Wait

func (p *ProcessWorker) Wait() <-chan error

Wait returns the channel to wait for process exit.

type State

type State struct {
	Name     string
	Status   Status
	PID      int
	ExitCode int
	Error    error
	Metadata map[string]string
	Children []State
}

State represents a snapshot of the worker's status.

type Status

type Status string

Status represents the lifecycle state of a worker.

const (
	StatusPending Status = "Pending"
	StatusRunning Status = "Running"
	StatusStopped Status = "Stopped"
	StatusFailed  Status = "Failed"
)

type Worker

type Worker interface {
	// Start initiates the worker. It must be non-blocking.
	// The context can be used to control the startup phase.
	Start(context.Context) error

	// Stop requests the worker to stop.
	// It should respect the provided context for timeout/cancellation of the stop request.
	Stop(context.Context) error

	// Wait returns a channel that is closed when the worker has exited.
	// The error associated with the exit (if any) is sent on the channel.
	Wait() <-chan error

	// String returns a human-readable description/ID of the worker.
	String() string

	// State returns the current state of the worker for introspection.
	State() State
}

Worker defines the interface for a managed unit of work (process, goroutine, container).

The lifecycle is:

  1. Start(ctx) -> Non-blocking.
  2. Wait() -> Returns channel that closes when work finishes.
  3. Stop(ctx) -> Graceful termination request.

func FromFunc added in v1.3.1

func FromFunc(name string, fn func(context.Context) error) Worker

FromFunc creates a Worker from a simple function. The function is executed in a goroutine when Start is called. The context passed to the function is cancelled when Stop is called.

Jump to

Keyboard shortcuts

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