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 ¶
- type HealthStatus
- type Option
- func WithBackoff(initial, max time.Duration, multiplier float64) Option
- func WithDependsOn(names ...string) Option
- func WithMaxRetries(max int) Option
- func WithRestartConfig(config RestartConfig) Option
- func WithRestartPolicy(policy RestartPolicy) Option
- func WithStartDelay(duration time.Duration) Option
- type RestartConfig
- type RestartPolicy
- type Runner
- type ServiceHealth
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 ¶
WithBackoff configures the exponential backoff parameters.
func WithDependsOn ¶
WithDependsOn specifies services that must start before this service. The service will wait for all dependencies to be healthy before starting.
func WithMaxRetries ¶
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 ¶
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 (*Runner) Add ¶
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 ¶
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.
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.