Documentation
¶
Overview ¶
Package service provides ready-to-use primitives for creating services in Acronis Cyber Cloud.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrPeriodicWorkerStop = errors.New("stop periodic worker error")
ErrPeriodicWorkerStop is an error that may be used for interrupting PeriodicWorker's loop.
var ErrWorkerUnitStopTimeoutExceeded = errors.New("worker unit stop timeout exceeded")
ErrWorkerUnitStopTimeoutExceeded is an error that occurs when WorkerUnit's gracefully stop timeout is exceeded.
Functions ¶
This section is empty.
Types ¶
type CompositeUnit ¶
type CompositeUnit struct {
Units []Unit
}
CompositeUnit represents a composition of service units and implements Composite design pattern.
func NewCompositeUnit ¶
func NewCompositeUnit(units ...Unit) *CompositeUnit
NewCompositeUnit creates a new composite unit.
func (*CompositeUnit) MustRegisterMetrics ¶
func (cu *CompositeUnit) MustRegisterMetrics()
MustRegisterMetrics registers metrics in Prometheus client and panics if any error occurs.
func (*CompositeUnit) Start ¶
func (cu *CompositeUnit) Start(fatalError chan<- error)
Start launches all units in the composition concurrently, each in its own goroutine, by calling their Start methods. It blocks until all Start method invocations return.
If any unit writes to the provided error channel upon returning, the method attempts to stop all other units (non-gracefully) by calling their Stop methods. A CompositeUnitError - potentially including errors from the stop operations — is then sent to the provided channel.
func (*CompositeUnit) Stop ¶
func (cu *CompositeUnit) Stop(gracefully bool) error
Stop stops all units in the composition (each in its own separate goroutine). Errors that occurred while stopping the units are collected and single CompositeUnitError is returned.
func (*CompositeUnit) UnregisterMetrics ¶
func (cu *CompositeUnit) UnregisterMetrics()
UnregisterMetrics unregisters metrics in Prometheus client.
type CompositeUnitError ¶
type CompositeUnitError struct {
UnitErrors []error
}
CompositeUnitError is an error which may occurs in CompositeUnit's methods.
func (*CompositeUnitError) Error ¶
func (cue *CompositeUnitError) Error() string
Error returns a string representation of a units composition error.
type MetricsRegisterer ¶
type MetricsRegisterer interface {
MustRegisterMetrics()
UnregisterMetrics()
}
MetricsRegisterer is an interface for objects that can register its own metrics.
type PeriodicWorker ¶
type PeriodicWorker struct {
// contains filtered or unexported fields
}
PeriodicWorker represents a worker that runs underlying worker periodically.
func NewPeriodicWorker ¶
func NewPeriodicWorker(worker Worker, intervalDelay time.Duration, logger log.FieldLogger) *PeriodicWorker
NewPeriodicWorker creates a new instance of PeriodicWorker with constant delays.
func NewPeriodicWorkerWithOpts ¶
func NewPeriodicWorkerWithOpts( worker Worker, intervalDelay time.Duration, logger log.FieldLogger, opts PeriodicWorkerOpts, ) *PeriodicWorker
NewPeriodicWorkerWithOpts creates a new instance of PeriodicWorker with an ability to specify different optional parameters.
type PeriodicWorkerOpts ¶
type PeriodicWorkerOpts struct {
InitialDelay time.Duration
IntervalDelayFunc func(worker Worker, err error) time.Duration
}
PeriodicWorkerOpts contains optional parameters for constructing PeriodicWorker.
type Service ¶
Service represents a service which can register metrics in Prometheus client, start unit and stop it in a graceful way by OS signal.
func New ¶
func New(logger log.FieldLogger, unit Unit) *Service
New creates new Service which will start and stop passing unit.
func NewWithOpts ¶
func NewWithOpts(logger log.FieldLogger, unit Unit, opts Opts) *Service
NewWithOpts is a more configurable version of New.
type Unit ¶
type Unit interface {
// Start begins the unit's operation.
//
// On the happy path, an implementation may perform necessary initialization and return immediately,
// or block the calling goroutine for the duration of the unit's lifetime.
// The Stop method may be called regardless of whether the unit has started successfully, failed, or
// is still running.
//
// If Start succeeds, it must not write anything to the provided error channel.
// Additionally, the channel must not be used after the Start method has returned.
Start(fatalErr chan<- error)
// Stop halts the unit.
//
// If 'gracefully' is true, the unit should attempt a clean shutdown.
// Note that this method may be called even if Start has failed or was never called.
Stop(gracefully bool) error
}
Unit represents a service unit that can be started and stopped. Each Unit is a distinct component within a service, with its own lifecycle.
type WorkerFunc ¶
WorkerFunc is an adapter to allow the use of ordinary functions as Worker.
type WorkerUnit ¶
type WorkerUnit struct {
// contains filtered or unexported fields
}
WorkerUnit allows presenting Worker as Unit.
func NewWorkerUnit ¶
func NewWorkerUnit(worker Worker) *WorkerUnit
NewWorkerUnit creates a new instance of WorkerUnit.
func NewWorkerUnitWithOpts ¶
func NewWorkerUnitWithOpts(worker Worker, opts WorkerUnitOpts) *WorkerUnit
NewWorkerUnitWithOpts creates a new instance of WorkerUnit with an ability to specify different optional parameters.
func (*WorkerUnit) MustRegisterMetrics ¶
func (u *WorkerUnit) MustRegisterMetrics()
MustRegisterMetrics registers underlying Worker's metrics.
func (*WorkerUnit) Start ¶
func (u *WorkerUnit) Start(fatalError chan<- error)
Start starts (call Run() method) underlying Worker.
func (*WorkerUnit) Stop ¶
func (u *WorkerUnit) Stop(gracefully bool) error
Stop stops underlying Worker.
func (*WorkerUnit) UnregisterMetrics ¶
func (u *WorkerUnit) UnregisterMetrics()
UnregisterMetrics unregisters underlying Worker's metrics.
type WorkerUnitOpts ¶
type WorkerUnitOpts struct {
MetricsRegisterer MetricsRegisterer
GracefulStopTimeout time.Duration
}
WorkerUnitOpts contains optional parameters for constructing PeriodicWorker.