Tiny supervisor for long running processes with graceful shutdown.
import "github.com/byteartis/neosupervisor"
var (
// ErrApplicationIsRunning for when application is already running
ErrApplicationIsRunning = errors.New("application is already running")
// ErrApplicationAlreadyStopped for when the application is already stopped
ErrApplicationAlreadyStopped = errors.New("application is stopped and cannot be started again")
)
type Option
Option is a type alias for a functional option that can be passed to the supervisor during its creation.
type Option func(o *Options)
func WithTimeout
func WithTimeout(timeout int) Option
WithTimeout allows to set the timeout for the supervisor. The provided value must be greater than 0 and it will override the default timeout.
type Options
Options the application's options
type Options struct {
// contains filtered or unexported fields
}
type Runner
Runner is the interface that must be implemented by all background running processes. The method must run while the received context is not cancelled. Returning an error will stop the entire supervisor and cause de application to exit with code 1.
type Runner interface {
Run(context.Context) error
}
type RunnerOption
RunnerOption defines the options that can be passed to the supervisor when adding a new runner
type RunnerOption func(*runnerOptions)
func WithRunnerName
func WithRunnerName(n string) RunnerOption
WithRunnerName defines the name of the runner in the options.
type Supervisor
Supervisor defines the entity responsible for running the different types of components.
type Supervisor struct {
// contains filtered or unexported fields
}
func New
func New(opts ...Option) *Supervisor
New creates a new supervisor with the provided options.
func (*Supervisor) AddRunner
func (a *Supervisor) AddRunner(ctx context.Context, r Runner, opts ...RunnerOption) error
AddRunner registers a new runner in the supervisor. Optionally we can attach several options to it.
func (*Supervisor) Launch
func (a *Supervisor) Launch(launchCtx context.Context) error
Launch will start the supervisor and all its components. It receives a context that when cancelled will also stop all components. Runners will be stopped immediately.
If any failure occurs on Launch it will return an error.
When called multiple times, if the supervisor is stopped will return an error, ottherwise it will immediately return nil. The possible states are: NEW, RUNNING, STOPPING, STOPPED.
Generated by gomarkdoc