services

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2021 License: MIT Imports: 6 Imported by: 0

README

Go Report Card codecov

rscsrv

rscsrv is a go library that implement services and let you start and stop them gracefully.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultSignals is the list of signals that the ResourceStarter will listen if no listener is specified.
	DefaultSignals = []os.Signal{os.Interrupt}
)
View Source
var (
	// ErrAlreadyListening is returned by the ServerStarter when it tries to listen a service that is already
	// listeining.
	ErrAlreadyListening = errors.New("service already listening")
)

Functions

This section is empty.

Types

type Configurable

type Configurable interface {
	// Load will load the configuration
	Load(ctx context.Context) error
}

Configurable describes a service that should be loaded before started.

This method will be used direct by `ResourceStarter`. Before starting a service, `ResourceStarter` will call `Load` (if available) before continuing starting the service. If it fails, `ResourceStarter.Start` will fail. Otherwise, the starting process will continue normally.

type ManagerOption added in v0.1.0

type ManagerOption = func(*ResourceStarter)

func WithReporter added in v0.1.0

func WithReporter(reporter Reporter) ManagerOption

WithReporter is a ManagerOption that will set the signal listener instance of a ResourceStarter.

type Reporter

type Reporter interface {
	BeforeStart(context.Context, Service)
	AfterStart(context.Context, Service, error)
	BeforeStop(context.Context, Service)
	AfterStop(context.Context, Service, error)
	BeforeLoad(context.Context, Configurable)
	AfterLoad(context.Context, Configurable, error)

	SignalReceived(os.Signal)
}

Reporter will be called Before and After some actions by a `ResourceStarter`.

type ResourceService added in v0.1.0

type ResourceService interface {
	Service

	// Start will start the service in a blocking way.
	//
	// If the service is successfully started, `nil` should be returned. Otherwise, an error must be returned.
	Start(ctx context.Context) error

	// Stop will stop this service.
	//
	// For most implementations it will be blocking and should return only when the service finishes stopping.
	//
	// If the service is successfully stopped, `nil` should be returned. Otherwise, an error must be returned.
	Stop(ctx context.Context) error
}

ResourceService is the interface that must be implemented for resourceServices that its start is NOT cancellable.

type ResourceServiceRetrier added in v0.1.0

type ResourceServiceRetrier struct {
	// contains filtered or unexported fields
}

ResourceServiceRetrier wraps a `Service` in order to provide functionality for retrying in case of its starting process fails.

func (*ResourceServiceRetrier) Name added in v0.1.0

func (retrier *ResourceServiceRetrier) Name() string

Name will return a human identifiable name for this service. Ex: Postgresql Connection.

func (*ResourceServiceRetrier) Start added in v0.1.0

func (retrier *ResourceServiceRetrier) Start(ctx context.Context) error

Start implements the logic of starting a service. If it fails, it should use the configuration to retry.

func (*ResourceServiceRetrier) Stop added in v0.1.0

func (retrier *ResourceServiceRetrier) Stop(ctx context.Context) error

Stop will stop this service.

For most implementations it will be blocking and should return only when the service finishes stopping.

If the service is successfully stopped, `nil` should be returned. Otherwise, an error must be returned.

type ResourceStarter added in v0.1.0

type ResourceStarter struct {
	// contains filtered or unexported fields
}

func NewManager added in v0.1.0

func NewManager(opts ...ManagerOption) *ResourceStarter

NewManager creates a new instance of ResourceStarter.

If a listener is not defined, it will create one based on DefaultSignals.

func (*ResourceStarter) Start added in v0.1.0

func (starter *ResourceStarter) Start(ctx context.Context, services ...ResourceService) (errResult error)

Start will initialize the starting process. Once it is finished, nil is returned in case of success. Otherwise, an error is returned.

func (*ResourceStarter) Stop added in v0.1.0

func (starter *ResourceStarter) Stop(ctx context.Context) (errResult error)

Stop will go through all started resourceServices, in the opposite order they were started, stopping one by one. If any, failure is detected, the function will stop leaving some started resourceServices.

func (*ResourceStarter) WithReporter added in v0.1.0

func (starter *ResourceStarter) WithReporter(reporter Reporter) *ResourceStarter

WithReporter sets the reporter for this ResourceStarter instance, returning it afterwards.

type RetrierBuilder

type RetrierBuilder struct {
	// contains filtered or unexported fields
}

RetrierBuilder is the helper for building `ResourceServiceRetrier`.

func Retrier

func Retrier() *RetrierBuilder

Retrier returns a new `RetrierBuilder` instance.

func (*RetrierBuilder) Backoff added in v0.1.0

func (builder *RetrierBuilder) Backoff(value backoff.BackOff) *RetrierBuilder

Backoff set the timeout for the `Retrier`.

func (*RetrierBuilder) Build

func (builder *RetrierBuilder) Build(service ResourceService) ResourceService

Build creates a new `ResourceServiceRetrier` with

func (*RetrierBuilder) Reporter

func (builder *RetrierBuilder) Reporter(value RetrierReporter) *RetrierBuilder

Reporter set the reporter for the `Retrier`.

type RetrierReporter

type RetrierReporter interface {
	Reporter
	BeforeRetry(context.Context, Service, int)
}

type ServerService added in v0.1.0

type ServerService interface {
	Service

	// Listen will start the server service and will block until the service is closed.
	//
	// If the services is already listining, this should return an error ErrAlreadyListening.
	Listen(ctx context.Context) error

	// Close will stop this service.
	//
	// If the services has not started, or is already stopped, this should do nothing and just return nil.
	Close(ctx context.Context) error
}

ServerService is the interface that must be implemented for resourceServices that its start is NOT cancellable.

type ServerStarter added in v0.1.0

type ServerStarter struct {
	// contains filtered or unexported fields
}

ServerStarter is capable of starting multiple ServerService in one call

func NewServerStarter added in v0.1.0

func NewServerStarter(listener signals.Listener) ServerStarter

NewServerStarter returns a new instance of ServerStarter with a listener set.

func (*ServerStarter) Close added in v0.1.0

func (starter *ServerStarter) Close(_ context.Context) error

func (*ServerStarter) Listen added in v0.1.0

func (starter *ServerStarter) Listen(ctx context.Context, services ...ServerService) []error

Listen will start all services by calling its Listen method.

type Service

type Service interface {
	// Name will return a human identifiable name for this service. Ex: Postgresql Connection.
	Name() string
}

Service is the abstraction of what minimum signature a service must have.

Jump to

Keyboard shortcuts

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