Documentation
¶
Index ¶
- Constants
- type ILauncher
- type IServicesRunner
- type Option
- func WithAfterStart(fn func() error) Option
- func WithAfterStop(fn func() error) Option
- func WithAppStartStopLog(v bool) Option
- func WithBeforeStart(fn func() error) Option
- func WithBeforeStop(fn func() error) Option
- func WithContext(ctx context.Context) Option
- func WithGlobalShutdownTimeout(d time.Duration) Option
- func WithLogger(l logger.ExtendedLogger) Option
- func WithName(n string) Option
- func WithOpsConfig(c ops.Config) Option
- func WithRunnerServicesSequence(v RunnerServicesSequence) Option
- func WithSignal(b bool) Option
- func WithVersion(v string) Option
- type Options
- type RestartMode
- type RestartPolicy
- type RunnerServicesSequence
- type Service
- type ServiceOption
- func WithEnabled(v bool) ServiceOption
- func WithRestartPolicy(p RestartPolicy) ServiceOption
- func WithService(svc any) ServiceOption
- func WithServiceAfterStart(fn func() error) ServiceOption
- func WithServiceAfterStartFinished(fn func() error) ServiceOption
- func WithServiceAfterStop(fn func() error) ServiceOption
- func WithServiceBeforeStart(fn func() error) ServiceOption
- func WithServiceBeforeStop(fn func() error) ServiceOption
- func WithServiceContext(ctx context.Context) ServiceOption
- func WithServiceLogger(l logger.Logger) ServiceOption
- func WithServiceName(n string) ServiceOption
- func WithShutdownTimeout(v time.Duration) ServiceOption
- func WithStart(fn func(context.Context) error) ServiceOption
- func WithStartupTimeout(v time.Duration) ServiceOption
- func WithStop(fn func(context.Context) error) ServiceOption
- type ServiceOptions
- type ServiceState
Constants ¶
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.
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)
}
type IServicesRunner ¶
type Option ¶
type Option func(*Options)
func WithAfterStart ¶
WithAfterStart run funcs after service starts.
func WithAfterStop ¶
WithAfterStop run funcs after service stops.
func WithAppStartStopLog ¶ added in v0.2.2
func WithBeforeStart ¶
WithBeforeStart run funcs before service starts.
func WithBeforeStop ¶
WithBeforeStop run funcs before service stops.
func WithContext ¶
func WithGlobalShutdownTimeout ¶ added in v0.4.0
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 WithOpsConfig ¶ added in v0.1.0
func WithRunnerServicesSequence ¶ added in v0.1.0
func WithRunnerServicesSequence(v RunnerServicesSequence) Option
func WithSignal ¶
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) Options ¶ added in v0.3.0
func (s *Service) Options() *ServiceOptions
func (*Service) State ¶ added in v0.4.0
func (s *Service) State() ServiceState
State returns the current lifecycle state of the service.
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.
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.