runner

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package runner provides orchestration for managing multiple services with dependency handling, restart logic, and aggregate health checks.

Example usage:

runner := runner.New("my-app")

// Add services with dependencies
runner.Add(dbService, runner.WithRestartPolicy(runner.RestartAlways))
runner.Add(apiService,
    runner.WithDependsOn("database"),
    runner.WithStartDelay(2*time.Second),
    runner.WithRestartPolicy(runner.RestartOnFailure),
)

// Start all services in dependency order
if err := runner.Start(ctx); err != nil {
    log.Fatal(err)
}

// Stop all services in reverse order
defer runner.Stop(context.Background())

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HealthStatus

type HealthStatus struct {
	// Healthy indicates if all services are healthy.
	Healthy bool

	// Services maps service names to their individual health status.
	Services map[string]ServiceHealth
}

HealthStatus represents the aggregate health status of the runner.

type Option

type Option func(*serviceConfig)

Option configures a service within the runner.

func WithBackoff

func WithBackoff(initial, max time.Duration, multiplier float64) Option

WithBackoff configures the exponential backoff parameters.

func WithDependsOn

func WithDependsOn(names ...string) Option

WithDependsOn specifies services that must start before this service. The service will wait for all dependencies to be healthy before starting.

func WithMaxRetries

func WithMaxRetries(max int) Option

WithMaxRetries sets the maximum number of restart attempts.

func WithRestartConfig

func WithRestartConfig(config RestartConfig) Option

WithRestartConfig sets the full restart configuration for the service.

func WithRestartPolicy

func WithRestartPolicy(policy RestartPolicy) Option

WithRestartPolicy sets the restart policy for the service.

func WithStartDelay

func WithStartDelay(duration time.Duration) Option

WithStartDelay adds a delay before starting the service. Useful for staggering service startups or waiting for external dependencies.

type RestartConfig

type RestartConfig struct {
	// Policy determines when to restart the service.
	Policy RestartPolicy

	// MaxRetries is the maximum number of restart attempts (0 = unlimited).
	MaxRetries int

	// InitialBackoff is the initial delay before first restart.
	InitialBackoff time.Duration

	// MaxBackoff is the maximum delay between restarts.
	MaxBackoff time.Duration

	// Multiplier is the factor by which the backoff increases on each retry.
	Multiplier float64

	// Jitter adds randomness to backoff (±25%) to prevent thundering herd.
	Jitter bool
}

RestartConfig configures restart behavior for a service.

func DefaultRestartConfig

func DefaultRestartConfig() RestartConfig

DefaultRestartConfig returns a restart config with sensible defaults.

type RestartPolicy

type RestartPolicy int

RestartPolicy determines when and how a service should be restarted on failure.

const (
	// RestartNever means the service will never be automatically restarted.
	RestartNever RestartPolicy = iota

	// RestartAlways means the service will always be restarted regardless of exit status.
	RestartAlways

	// RestartOnFailure means the service will be restarted only if it exits with an error.
	RestartOnFailure
)

func (RestartPolicy) String

func (p RestartPolicy) String() string

String returns the string representation of the restart policy.

type Runner

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

Runner manages multiple services with dependency handling and restart logic.

func New

func New(name string) *Runner

New creates a new Runner with the given name.

func (*Runner) Add

func (r *Runner) Add(svc service.Service, opts ...Option) error

Add adds a service to the runner with optional configuration. The service will be started when Runner.Start() is called. Services are started in dependency order based on WithDependsOn options.

func (*Runner) Health

func (r *Runner) Health() error

Health implements a health check that can be integrated with the health package. It returns nil if all services are healthy, or an error with details about unhealthy services.

func (*Runner) HealthStatus

func (r *Runner) HealthStatus() HealthStatus

HealthStatus returns detailed health status for all managed services.

func (*Runner) Name

func (r *Runner) Name() string

Name returns the name of the runner.

func (*Runner) Start

func (r *Runner) Start(ctx context.Context) error

Start starts all services in dependency order. Services are started concurrently where dependencies allow. Returns an error if any service fails to start.

func (*Runner) Stop

func (r *Runner) Stop(ctx context.Context) error

Stop stops all services in reverse startup order. Services are stopped gracefully with the given context timeout.

type ServiceHealth

type ServiceHealth struct {
	// Healthy indicates if the service is healthy.
	Healthy bool

	// Error is the error message if the service is unhealthy.
	Error string

	// Running indicates if the service is currently running.
	Running bool
}

ServiceHealth represents the health status of a single service.

Jump to

Keyboard shortcuts

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