launcher

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2026 License: MIT Imports: 13 Imported by: 6

Documentation

Index

Constants

View Source
const (
	ServiceStateIdle     = types.ServiceStateIdle
	ServiceStateStarting = types.ServiceStateStarting
	ServiceStateRunning  = types.ServiceStateRunning
	ServiceStateStopping = types.ServiceStateStopping
	ServiceStateStopped  = types.ServiceStateStopped
	ServiceStateFailed   = types.ServiceStateFailed
)

Re-export ServiceState constants so callers don't need to import launcher/types.

View Source
const (
	RunnerServicesSequenceNone = iota
	RunnerServicesSequenceFifo
	RunnerServicesSequenceLifo
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ILauncher

type ILauncher interface {
	// Run launcher and all services
	Run() error
	// Stop launcher and all services
	Stop()
	// ServicesRunner return services runner
	ServicesRunner() IServicesRunner
	// Context return global context
	Context() context.Context

	// AddBeforeStartHooks adds before start hooks
	AddBeforeStartHooks(hook ...func() error)
	// AddBeforeStopHooks adds before stop hooks
	AddBeforeStopHooks(hook ...func() error)
	// AddAfterStartHooks adds after start hooks
	AddAfterStartHooks(hook ...func() error)
	// AddAfterStopHooks adds after stop hooks
	AddAfterStopHooks(hook ...func() error)
}

func New

func New(opts ...Option) ILauncher

New creates a new launcher.

type IServicesRunner

type IServicesRunner interface {
	// Register services
	Register(services ...*Service)
	// Services return all registered services
	Services() []*Service
	// Get returns a registered service by name, or false if not found.
	Get(name string) (*Service, bool)
}

type Option

type Option func(*Options)

func WithAfterStart

func WithAfterStart(fn func() error) Option

WithAfterStart run funcs after service starts.

func WithAfterStop

func WithAfterStop(fn func() error) Option

WithAfterStop run funcs after service stops.

func WithAppStartStopLog added in v0.2.2

func WithAppStartStopLog(v bool) Option

func WithBeforeStart

func WithBeforeStart(fn func() error) Option

WithBeforeStart run funcs before service starts.

func WithBeforeStop

func WithBeforeStop(fn func() error) Option

WithBeforeStop run funcs before service stops.

func WithContext

func WithContext(ctx context.Context) Option

func WithGlobalShutdownTimeout added in v0.4.0

func WithGlobalShutdownTimeout(d time.Duration) Option

WithGlobalShutdownTimeout sets an upper bound on the total graceful shutdown duration. If all services do not stop within this duration, the launcher exits immediately.

func WithLogger

func WithLogger(l logger.ExtendedLogger) Option

func WithName

func WithName(n string) Option

Name of the launcher.

func WithOpsConfig added in v0.1.0

func WithOpsConfig(c ops.Config) Option

func WithRunnerServicesSequence added in v0.1.0

func WithRunnerServicesSequence(v RunnerServicesSequence) Option

func WithSignal

func WithSignal(b bool) Option

func WithVersion

func WithVersion(v string) Option

Version of the launcher.

type Options

type Options struct {
	Name    string
	Version string

	// Before and After funcs
	BeforeStart []func() error
	BeforeStop  []func() error
	AfterStart  []func() error
	AfterStop   []func() error

	AppStartStopLog bool

	RunnerServicesSequence RunnerServicesSequence

	Signal bool

	// GlobalShutdownTimeout limits the total time allowed for all services to stop.
	// Zero means no global timeout (each service uses its own ShutdownTimeout).
	GlobalShutdownTimeout time.Duration

	Context context.Context //nolint:containedctx

	OpsConfig ops.Config
	// contains filtered or unexported fields
}

type RestartMode added in v0.4.0

type RestartMode int

RestartMode defines when a service should be restarted after exit.

const (
	// RestartNever disables automatic restarts (default).
	RestartNever RestartMode = iota
	// RestartOnFailure restarts the service only when it exits with an error.
	RestartOnFailure
	// RestartAlways restarts the service on any exit (error or clean).
	RestartAlways
)

type RestartPolicy added in v0.4.0

type RestartPolicy struct {
	// Mode controls when restarts happen.
	Mode RestartMode

	// MaxRetries is the maximum number of restart attempts. 0 means unlimited.
	MaxRetries int

	// Delay is the initial wait before the first restart attempt.
	Delay time.Duration

	// MaxDelay caps the exponential backoff. Zero defaults to Delay (no backoff growth).
	MaxDelay time.Duration
}

RestartPolicy configures automatic restart behaviour for a service.

type RunnerServicesSequence added in v0.1.0

type RunnerServicesSequence int

type Service added in v0.3.0

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

Service wraps a lifecycle-managed unit with Start/Stop functions and hooks.

func NewService added in v0.3.0

func NewService(opts ...ServiceOption) *Service

NewService creates a new Service.

func (Service) Name added in v0.3.0

func (s Service) Name() string

func (*Service) Options added in v0.3.0

func (s *Service) Options() *ServiceOptions

func (*Service) Start added in v0.3.0

func (s *Service) Start() error

func (*Service) State added in v0.4.0

func (s *Service) State() ServiceState

State returns the current lifecycle state of the service.

func (*Service) Stop added in v0.3.0

func (s *Service) Stop() error

func (Service) String added in v0.3.0

func (s Service) String() string

type ServiceOption added in v0.3.0

type ServiceOption func(o *ServiceOptions)

ServiceOption is a function that configures a ServiceOptions.

func WithEnabled added in v0.3.0

func WithEnabled(v bool) ServiceOption

WithEnabled sets the enabled state of the service.

func WithRestartPolicy added in v0.4.0

func WithRestartPolicy(p RestartPolicy) ServiceOption

WithRestartPolicy configures automatic restart behaviour after an unexpected exit.

func WithService added in v0.3.0

func WithService(svc any) ServiceOption

WithService wraps any value that implements Name/Start/Stop/Enabled/HealthChecker.

func WithServiceAfterStart added in v0.3.0

func WithServiceAfterStart(fn func() error) ServiceOption

WithServiceAfterStart runs fn after service starts.

func WithServiceAfterStartFinished added in v0.3.0

func WithServiceAfterStartFinished(fn func() error) ServiceOption

WithServiceAfterStartFinished runs fn after the service Start func finishes.

func WithServiceAfterStop added in v0.3.0

func WithServiceAfterStop(fn func() error) ServiceOption

WithServiceAfterStop runs fn after service stops.

func WithServiceBeforeStart added in v0.3.0

func WithServiceBeforeStart(fn func() error) ServiceOption

WithServiceBeforeStart runs fn before service starts.

func WithServiceBeforeStop added in v0.3.0

func WithServiceBeforeStop(fn func() error) ServiceOption

WithServiceBeforeStop runs fn before service stops.

func WithServiceContext added in v0.3.0

func WithServiceContext(ctx context.Context) ServiceOption

func WithServiceLogger added in v0.3.0

func WithServiceLogger(l logger.Logger) ServiceOption

func WithServiceName added in v0.3.0

func WithServiceName(n string) ServiceOption

WithServiceName sets the name of the service.

func WithShutdownTimeout added in v0.3.0

func WithShutdownTimeout(v time.Duration) ServiceOption

WithShutdownTimeout sets the shutdown timeout of the service.

func WithStart added in v0.3.0

func WithStart(fn func(context.Context) error) ServiceOption

WithStart sets the start function of the service.

func WithStartupTimeout added in v0.4.0

func WithStartupTimeout(v time.Duration) ServiceOption

WithStartupTimeout sets the maximum time the service's StartFn is allowed to run before being considered failed. Zero (default) means no startup timeout.

func WithStop added in v0.3.0

func WithStop(fn func(context.Context) error) ServiceOption

WithStop sets the stop function of the service.

type ServiceOptions added in v0.3.0

type ServiceOptions struct {
	Logger logger.Logger

	Name          string
	Enabled       bool
	HealthChecker types.HealthChecker

	StartFn func(ctx context.Context) error
	StopFn  func(ctx context.Context) error

	// Before and After funcs
	BeforeStart        []func() error
	BeforeStop         []func() error
	AfterStart         []func() error
	AfterStartFinished []func() error
	AfterStop          []func() error

	Context context.Context //nolint:containedctx

	// ShutdownTimeout is the maximum time to wait for Stop to complete. Default 10 seconds.
	ShutdownTimeout time.Duration

	// StartupTimeout is the maximum time to wait for the StartFn goroutine to signal an error
	// after launch. Zero means no per-service startup timeout (the service runs until context cancel).
	StartupTimeout time.Duration

	// RestartPolicy defines how the service behaves after an unexpected exit.
	RestartPolicy RestartPolicy
}

ServiceOptions holds configuration for a Service.

func (*ServiceOptions) Validate added in v0.3.0

func (s *ServiceOptions) Validate() error

type ServiceState added in v0.4.0

type ServiceState = types.ServiceState

ServiceState is an alias for types.ServiceState for convenience.

Directories

Path Synopsis
services

Jump to

Keyboard shortcuts

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