supervisor

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package supervisor provides retry, restart, and escalation policies for GRIP execution pipelines.

The Supervisor orchestrates execution using configurable policies. It never executes user code directly — it delegates to an Executor and applies retry/restart/escalation logic based on the result.

Policies

  • RetryPolicy: max attempts, backoff duration, jitter
  • DeadlinePolicy: timeout per execution
  • AdmissionPolicy: max concurrency
  • EscalationPolicy: escalate-on-panic, escalate-on-retry-exhausted

Hooks

The hook system provides callbacks at each lifecycle stage:

  • OnStart: called before execution begins
  • OnSuccess: called after successful execution
  • OnFailure: called after failed execution
  • OnRetry: called before each retry attempt
  • OnGiveUp: called when all retries are exhausted

Each hook is wrapped in recover() to prevent callback panics from crashing the pipeline.

Index

Constants

View Source
const (
	// Successful Execution
	FailureSuccess = core.FailureNone

	// Returned error (non-panic)
	FailureErr = core.FailureHandlerError

	// Panic occurred
	FailurePanic = core.FailurePanic

	// Context cancelled by caller
	FailureCancelled = core.FailureContextCancelled

	// Deadline exceeded (treated as cancellation in core)
	FailureDeadline = core.FailureContextCancelled

	// System shutdown
	FailureShutdown = core.FailureShutdown
)

Backward compatibility constants - mapped to core.FailureKind

Variables

This section is empty.

Functions

This section is empty.

Types

type BackoffPolicy

type BackoffPolicy interface {
	// NextDelay returns delay before next retry attempt.
	NextDelay(attempt int) time.Duration
}

type EscalationPolicy

type EscalationPolicy struct {
	// Escalates immediately on panic
	EscalateOnPanic bool

	// Escalates when retries are exhausted
	EscalateOnRetryExhausted bool

	// Escalates on non-panic errors
	EscalateOnError bool
}

type ExecutorRunner

type ExecutorRunner interface {
	Execute(exec core.Execution, fn func(context.Context) error) execution.Result
}

ExecutorRunner is the minimal interface Supervisor depends on. This avoids tight coupling and allows mocking in tests.

type ExponentialBackoff

type ExponentialBackoff struct {
	Base time.Duration
	Max  time.Duration
}

Exponential backoff

func (ExponentialBackoff) NextDelay

func (b ExponentialBackoff) NextDelay(attempt int) time.Duration

type FailureKind

type FailureKind = core.FailureKind

FailureKind is now an alias to core.FailureKind for backward compatibility. See core/failure.go for the authoritative definition.

type Hooks

type Hooks struct {
	// Called exactly once when execution starts
	OnStart func(ctx context.Context)

	// Called exactly once when execution completes successfully
	OnSuccess func(ctx context.Context)

	// Called exactly once when execution fails
	OnFailure func(ctx context.Context, failure FailureKind)

	// Called when supervisor decides to retry execution
	OnRetry func(ctx context.Context, attempt int)

	// Called when supervisor permanently gives up
	OnGiveUp func(ctx context.Context, failure FailureKind)
}

type RestartPolicy

type RestartPolicy int
const (
	// Never restart a child
	RestartNever RestartPolicy = iota

	// Restart only on failure (error or panic)
	RestartOnFailure

	// Restart even after success
	RestartAlways
)

type RetryPolicy

type RetryPolicy struct {
	// Maximum retry attempts
	// 0 means retries are disabled
	MaxRetries int

	// Whether panic is retryable
	RetryOnPanic bool

	// Whether normal errors are retryable
	RetryOnError bool
}

type Supervisor

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

Supervisor orchestrates execution using policies. It NEVER executes user code directly.

func NewSupervisor

func NewSupervisor(
	executor ExecutorRunner,
	policy SupervisorPolicy,
	hooks Hooks,
) *Supervisor

NewSupervisor constructs a Supervisor.

func (*Supervisor) Run

func (s *Supervisor) Run(
	ctx context.Context,
	exec core.Execution,
	fn func(context.Context) error,
) execution.Result

Run executes an execution under supervision.

Contract: - fn is passed THROUGH to executor, never called directly - retries, restarts, escalation handled here

type SupervisorPolicy

type SupervisorPolicy struct {
	Retry      RetryPolicy
	Restart    RestartPolicy
	Escalation EscalationPolicy
	Backoff    BackoffPolicy
}

Supervisor Policy

func DefaultSupervisorPolicy

func DefaultSupervisorPolicy() SupervisorPolicy

Defaults

func (SupervisorPolicy) ShouldEscalate

func (p SupervisorPolicy) ShouldEscalate(
	kind FailureKind,
	retriesExhausted bool,
) bool

ShouldEscalate decides whether failure should propagate upward.

func (SupervisorPolicy) ShouldRestart

func (p SupervisorPolicy) ShouldRestart(kind FailureKind) bool

ShouldRestart decides whether child should be restarted.

func (SupervisorPolicy) ShouldRetry

func (p SupervisorPolicy) ShouldRetry(
	kind FailureKind,
	attempt int,
) bool

ShouldRetry decides whether another retry attempt is allowed.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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