service

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package service provides ready-to-use primitives for creating services in Acronis Cyber Cloud.

Index

Constants

This section is empty.

Variables

View Source
var ErrPeriodicWorkerStop = errors.New("stop periodic worker error")

ErrPeriodicWorkerStop is an error that may be used for interrupting PeriodicWorker's loop.

View Source
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 starts all units in the composition (each in its own separate goroutine) and blocks. If fatal error occurs in any unit, it tries to stop (not gracefully) other ones and sends CompositeUnitError (may contain errors caused by stopping too) to passed 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 Opts

type Opts struct {
	ShutdownSignals []os.Signal
}

Opts represents an options for Service.

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.

func (*PeriodicWorker) Run

func (pw *PeriodicWorker) Run(ctx context.Context) (resErr error)

Run runs PeriodicWorker loop.

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

type Service struct {
	Unit    Unit
	Signals chan os.Signal
	Logger  log.FieldLogger
	Opts    Opts
}

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.

func (*Service) Start

func (s *Service) Start() error

Start wraps StartContext using the background context.

func (*Service) StartContext

func (s *Service) StartContext(ctx context.Context) error

StartContext starts service unit in the separate goroutine and blocks until fatal error occurs or any of the OS shutting down signals are received.

type Unit

type Unit interface {
	Start(fatalError chan<- error)
	Stop(gracefully bool) error
}

Unit is a common interface for all service units. Unit is a single component in service with its own life-cycle.

type Worker

type Worker interface {
	Run(ctx context.Context) error
}

Worker performs some (usually long-running) work.

type WorkerFunc

type WorkerFunc func(ctx context.Context) error

WorkerFunc is an adapter to allow the use of ordinary functions as Worker.

func (WorkerFunc) Run

func (f WorkerFunc) Run(ctx context.Context) error

Run is a part of Worker interface.

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.

Jump to

Keyboard shortcuts

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