Documentation ¶
Index ¶
- type Finalizer
- type Health
- type HealthConfigFunc
- type Initializer
- type InitializerConfigFunc
- type InitializerFunc
- type InitializerMeta
- type ParallelInitializer
- type ParallelInitializerConfigFunc
- type Process
- type ProcessConfigFunc
- func WithPriority(priority int) ProcessConfigFunc
- func WithProcessInitTimeout(timeout time.Duration) ProcessConfigFunc
- func WithProcessLogFields(fields log.LogFields) ProcessConfigFunc
- func WithProcessName(name string) ProcessConfigFunc
- func WithProcessShutdownTimeout(timeout time.Duration) ProcessConfigFunc
- func WithProcessStartTimeout(timeout time.Duration) ProcessConfigFunc
- func WithSilentExit() ProcessConfigFunc
- type ProcessContainer
- type ProcessMeta
- type Reason
- type Runner
- type RunnerConfigFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Finalizer ¶
type Finalizer interface { // Finalize is called after the application has stopped // all running processes. Finalize() error }
Finalizer is an optional extension of an Initializer that supports finalization. This is useful for initializers that need to tear down a background process before the process exits, but needs to be started early in the boot process (such as flushing logs or metrics).
type Health ¶
type Health interface { Reasons() []Reason LastChange() time.Duration AddReason(key interface{}) error RemoveReason(key interface{}) error HasReason(key interface{}) bool }
func NewHealth ¶
func NewHealth(configs ...HealthConfigFunc) Health
type HealthConfigFunc ¶
type HealthConfigFunc func(*health)
func WithHealthClock ¶
func WithHealthClock(clock glock.Clock) HealthConfigFunc
type Initializer ¶
type Initializer interface { // Init reads the given configuration and prepares // something for use by a process. This can be loading // files from disk, connecting to a remote service, // initializing shared data structures, and inserting // a service into a shared service container. Init(config config.Config) error }
Initializer is an interface that is called once on app startup.
type InitializerConfigFunc ¶
type InitializerConfigFunc func(*InitializerMeta)
InitializerConfigFunc is a function used to append additional metadata to an initializer during registration.
func WithFinalizerTimeout ¶
func WithFinalizerTimeout(timeout time.Duration) InitializerConfigFunc
WithFinalizerTimeout sets the time limit for the finalizer.
func WithInitializerLogFields ¶ added in v1.1.0
func WithInitializerLogFields(fields log.LogFields) InitializerConfigFunc
WithInitializerLogFields sets additional fields sent with every log message from this initializer.
func WithInitializerName ¶
func WithInitializerName(name string) InitializerConfigFunc
WithInitializerName assigns a name to an initializer, visible in logs.
func WithInitializerTimeout ¶
func WithInitializerTimeout(timeout time.Duration) InitializerConfigFunc
WithInitializerTimeout sets the time limit for the initializer.
type InitializerFunc ¶
InitializerFunc is a non-struct version of an initializer.
type InitializerMeta ¶
type InitializerMeta struct { Initializer // contains filtered or unexported fields }
InitializerMeta wraps an initializer with some package private fields.
func (*InitializerMeta) FinalizeTimeout ¶
func (m *InitializerMeta) FinalizeTimeout() time.Duration
FinalizeTimeout returns the maximum timeout allowed for a call to the Finalize function. A zero value indicates no timeout.
func (*InitializerMeta) InitTimeout ¶
func (m *InitializerMeta) InitTimeout() time.Duration
InitTimeout returns the maximum timeout allowed for a call to the Init function. A zero value indicates no timeout.
func (*InitializerMeta) LogFields ¶ added in v1.1.0
func (m *InitializerMeta) LogFields() log.LogFields
LogFields returns logging fields registered to this initializer.
func (*InitializerMeta) Name ¶
func (m *InitializerMeta) Name() string
Name returns the name of the initializer.
func (*InitializerMeta) Wrapped ¶
func (m *InitializerMeta) Wrapped() interface{}
Wrapped returns the underlying initializer.
type ParallelInitializer ¶
type ParallelInitializer struct { Logger log.Logger `service:"logger"` Services service.ServiceContainer `service:"services"` // contains filtered or unexported fields }
ParallelInitializer is a container for initializers that are initialized in parallel. This is useful when groups of initializers are independent and may contain some longer-running process (such as dialing a remote service).
func NewParallelInitializer ¶
func NewParallelInitializer(initializerConfigs ...ParallelInitializerConfigFunc) *ParallelInitializer
NewParallelInitializer creates a new parallel initializer.
func (*ParallelInitializer) Finalize ¶
func (pi *ParallelInitializer) Finalize() error
Finalize runs Finalize on all registered initializers concurrently.
func (*ParallelInitializer) Init ¶
func (pi *ParallelInitializer) Init(config config.Config) error
Init runs Init on all registered initializers concurrently.
func (*ParallelInitializer) RegisterInitializer ¶
func (i *ParallelInitializer) RegisterInitializer( initializer Initializer, initializerConfigs ...InitializerConfigFunc, )
RegisterInitializer adds an initializer to the initializer set with the given configuration.
type ParallelInitializerConfigFunc ¶
type ParallelInitializerConfigFunc func(*ParallelInitializer)
ParallelInitializerConfigFunc is a function used to configure an instance of a ParallelInitializer.
func WithParallelInitializerClock ¶
func WithParallelInitializerClock(clock glock.Clock) ParallelInitializerConfigFunc
WithParallelInitializerClock sets the clock used by the runner.
func WithParallelInitializerContainer ¶
func WithParallelInitializerContainer(container service.ServiceContainer) ParallelInitializerConfigFunc
WithParallelInitializerContainer sets the service container used by the runner.
func WithParallelInitializerLogger ¶
func WithParallelInitializerLogger(logger log.Logger) ParallelInitializerConfigFunc
WithParallelInitializerLogger sets the logger used by the runner.
type Process ¶
type Process interface { Initializer // Start begins performing the core action of the process. // For servers, this will begin accepting clients ona port. // For workers, this may begin reading from a remote work // queue and processing messages. This method should block // until a fatal error occurs, or until the Stop method is // called (at which point a nil error should be returned). // If this method is non-blocking, then the process should // be registered with the WithSilentExit option. Start() error // Stop informs the work being performed by the Start // method to begin a graceful shutdown. This method is // not expected to block until shutdown completes. Stop() error }
Process is an interface that continually performs a behavior during the life of a program. Generally, one process should do a single thing. Multiple processes can be registered to a process container and those processes can coordinate and communicate through shared services.
type ProcessConfigFunc ¶
type ProcessConfigFunc func(*ProcessMeta)
ProcessConfigFunc is a function used to append additional metadata to an process during registration.
func WithPriority ¶
func WithPriority(priority int) ProcessConfigFunc
WithPriority assigns a priority to a process. A process with a lower-valued priority is initialized and started before a process with a higher-valued priority. Two processes with the same priority are started concurrently.
func WithProcessInitTimeout ¶
func WithProcessInitTimeout(timeout time.Duration) ProcessConfigFunc
WithProcessInitTimeout sets the time limit for the process's Init method.
func WithProcessLogFields ¶ added in v1.1.0
func WithProcessLogFields(fields log.LogFields) ProcessConfigFunc
WithProcessLogFields sets additional fields sent with every log message from this process.
func WithProcessName ¶
func WithProcessName(name string) ProcessConfigFunc
WithProcessName assigns a name to an process, visible in logs.
func WithProcessShutdownTimeout ¶
func WithProcessShutdownTimeout(timeout time.Duration) ProcessConfigFunc
WithProcessShutdownTimeout sets the time limit for the process's Start method to yield after the Stop method has been called.
func WithProcessStartTimeout ¶
func WithProcessStartTimeout(timeout time.Duration) ProcessConfigFunc
WithProcessStartTimeout sets the time limit for the process to become healthy.
func WithSilentExit ¶
func WithSilentExit() ProcessConfigFunc
WithSilentExit allows a process to exit without causing the program to halt. The default is the opposite, where the completion of any registered process (even successful) causes a graceful shutdown of the other processes.
type ProcessContainer ¶
type ProcessContainer interface { // RegisterInitializer adds an initializer to the container // with the given configuration. RegisterInitializer(Initializer, ...InitializerConfigFunc) // RegisterProcess adds a process to the container with the // given configuration. RegisterProcess(Process, ...ProcessConfigFunc) // NumInitializers returns the number of registered initializers. NumInitializers() int // NumProcesses returns the number of registered processes. NumProcesses() int // NumPriorities returns the number of distinct registered // process priorities. NumPriorities() int // GetInitializers returns a slice of meta objects wrapping // all registered initializers. GetInitializers() []*InitializerMeta // GetProcessesAtPriorityIndex returns aslice of meta objects // wrapping all processes registered to this priority index, // where zero denotes the lowest priority, one the second // lowest, and so on. The index parameter is not checked for // validity before indexing an internal slice - caller beware. GetProcessesAtPriorityIndex(index int) []*ProcessMeta }
ProcessContainer is a collection of initializers and processes.
func NewProcessContainer ¶
func NewProcessContainer() ProcessContainer
NewProcessContainer creates an empty process container.
type ProcessMeta ¶
type ProcessMeta struct { Process // contains filtered or unexported fields }
ProcessMeta wraps a process with some package private fields.
func (*ProcessMeta) InitTimeout ¶
func (m *ProcessMeta) InitTimeout() time.Duration
InitTimeout returns the maximum timeout allowed for a call to the Init function. A zero value indicates no timeout.
func (*ProcessMeta) LogFields ¶ added in v1.1.0
func (m *ProcessMeta) LogFields() log.LogFields
Logields returns logging fields registered to this process.
func (*ProcessMeta) Name ¶
func (m *ProcessMeta) Name() string
Name returns the name of the process.
func (*ProcessMeta) Stop ¶
func (m *ProcessMeta) Stop() (err error)
Stop wraps the underlying process's Stop method with a Once value in order to guarantee that the Stop method will not take effect multiple times.
func (*ProcessMeta) Wrapped ¶
func (m *ProcessMeta) Wrapped() interface{}
Wrapped returns the underlying process.
type Runner ¶
type Runner interface { // Run takes a loaded configuration object, then starts and monitors // the registered items in the process container. This method returns // a channel of errors. Each error from an initializer or a process will // be sent on this channel (nil errors are ignored). This channel will // close once all processes have exited (or, alternatively, when the // shutdown timeout has elapsed). Run(config.Config) <-chan error // Shutdown will begin a graceful exit of all processes. This method // will block until the runner has exited (the channel from the Run // method has closed) or the given duration has elapsed. In the later // case a non-nil error is returned. Shutdown(time.Duration) error }
Runner wraps a process container. Given a loaded configuration object, it can run the registered initializers and processes and wait for them to exit (cleanly or via shutdown request).
func NewRunner ¶
func NewRunner( processes ProcessContainer, services service.ServiceContainer, health Health, runnerConfigs ...RunnerConfigFunc, ) Runner
NewRunner creates a process runner from the given process and service containers.
type RunnerConfigFunc ¶
type RunnerConfigFunc func(*runner)
RunnerConfigFunc is a function used to configure an instance of a ProcessRunner.
func WithClock ¶
func WithClock(clock glock.Clock) RunnerConfigFunc
WithClock sets the clock used by the runner.
func WithHealthCheckInterval ¶ added in v1.1.0
func WithHealthCheckInterval(interval time.Duration) RunnerConfigFunc
WithHealthCheckInterval sets the frequency between checks of process health during startup.
func WithLogger ¶
func WithLogger(logger log.Logger) RunnerConfigFunc
WithLogger sets the logger used by the runner.
func WithShutdownTimeout ¶
func WithShutdownTimeout(timeout time.Duration) RunnerConfigFunc
WithShutdownTimeout sets the maximum time it will wait for a process to exit during a graceful shutdown.
func WithStartTimeout ¶
func WithStartTimeout(timeout time.Duration) RunnerConfigFunc
WithStartTimeout sets the time it will wait for a process to become healthy after startup.