supervisor

package
v1.3.0 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: 8 Imported by: 0

Documentation

Overview

Package supervisor implements the Supervisor Pattern for managing process and worker lifecycles.

A Supervisor is a special type of Worker that manages a collection of child Workers. It is responsible for starting (spawning), monitoring (watching), and restarting (healing) its children based on defined strategies.

Strategies

The supervisor supports the following restart strategies:

  • StrategyOneForOne: If a child process terminates, only that process is restarted.
  • StrategyOneForAll: If a child process terminates, all other child processes are terminated, and then all child processes are restarted.

Handover Protocol

The supervisor implements a Handover Protocol to provide continuity across restarts. Each worker spec is assigned a persistent `ResumeID`. When a worker is restarted, the supervisor injects this ID and the previous exit status (via `EnvInjector`) to allow the new instance to resume work or report status accurately.

Usage

sup := supervisor.New("my-sup", supervisor.StrategyOneForOne,
    supervisor.Spec{
        Name: "worker-1",
        Factory: func() (worker.Worker, error) {
            return worker.NewProcess("worker-1", "sleep", "10"), nil
        },
    },
)

sup.Start(ctx)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Factory

type Factory func() (worker.Worker, error)

Factory is a function that creates a new worker instance.

type Spec

type Spec struct {
	Name    string
	Factory Factory
}

Spec defines the configuration for a supervised child worker.

type Strategy

type Strategy string

Strategy defines how the supervisor handles child failures.

const (
	// StrategyOneForOne: If a child process terminates, only that process is restarted.
	StrategyOneForOne Strategy = "OneForOne"
	// StrategyOneForAll: If a child process terminates, all other child processes are terminated,
	// and then all child processes are restarted.
	StrategyOneForAll Strategy = "OneForAll"
)

type Supervisor

type Supervisor interface {
	worker.Worker
}

Supervisor defines the interface for a managed cluster of workers. It implements the worker.Worker interface, allowing it to be nested.

func New

func New(name string, strategy Strategy, specs ...Spec) Supervisor

New creates a new Supervisor.

Jump to

Keyboard shortcuts

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