supervisor

package
v0.23.1 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2023 License: AGPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const LoggerTag = "SUPERVISOR"
View Source
const ReloaderLoggerTag = "RELOADER"

Variables

This section is empty.

Functions

func ServiceName added in v0.20.0

func ServiceName(s any) string

ServiceName returns the name of the service. If the service implements the WithName interface, the ServiceName method is used. Otherwise, the name of the type is returned.

Types

type Config added in v0.11.0

type Config interface {
	Services(logger log.Logger, appName string, appVersion string) (Service, error)
}

type Delayed added in v0.10.0

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

Delayed is a service that delays the start of another service.

func NewDelayed added in v0.10.0

func NewDelayed(service Service, delay time.Duration) *Delayed

NewDelayed returns a new Delayed service.

func (*Delayed) Start added in v0.10.0

func (d *Delayed) Start(ctx context.Context) error

Start implements the Service interface.

func (*Delayed) Wait added in v0.10.0

func (d *Delayed) Wait() <-chan error

Wait implements the Service interface.

type Reloader added in v0.23.1

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

Reloader is a service that can reload another wrapped service.

func NewReloader added in v0.23.1

func NewReloader(cfg ReloaderConfig) *Reloader

NewReloader returns a new Reloader instance.

func (*Reloader) ServiceName added in v0.23.1

func (r *Reloader) ServiceName() string

ServiceName implements the supervisor.WithName interface.

func (*Reloader) Start added in v0.23.1

func (r *Reloader) Start(ctx context.Context) (err error)

Start implements the Service interface.

func (*Reloader) Wait added in v0.23.1

func (r *Reloader) Wait() <-chan error

Wait implements the Service interface.

type ReloaderConfig added in v0.23.1

type ReloaderConfig struct {
	// Factory is a function that creates a new service instance. Every time
	// the new service is sent to the channel, the service is reloaded with
	// the new instance.
	//
	// The service is stopped when the factory channel is closed or when the
	// factory function returns an error.
	Factory func(ctx context.Context, service chan Service) error

	// Logger is a logger instance.
	Logger log.Logger
}

ReloaderConfig is a configuration for the Reloader service.

type Service

type Service interface {
	// Start starts the service.
	Start(ctx context.Context) error

	// Wait returns a channel that is blocked while service is running.
	// When the service is stopped, the channel will be closed. If an error
	// occurs, an error will be sent to the channel before closing it.
	Wait() <-chan error
}

Service that could be managed by Supervisor.

type Supervisor

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

Supervisor manages long-running services that implement the Service interface. If any of the managed services fail, all other services are stopped. This ensures that all services are running or none.

func New

func New(logger log.Logger) *Supervisor

New returns a new instance of *Supervisor.

func (*Supervisor) Start

func (s *Supervisor) Start(ctx context.Context) error

Start starts all watched services. It can be invoked only once, otherwise it panics.

func (*Supervisor) Wait

func (s *Supervisor) Wait() <-chan error

Wait returns a channel that is blocked until at least one service is running. When all services are stopped, the channel will be closed. If an error occurs in any of the services, it will be sent to the channel before closing it. If multiple service crash, only the first error is returned.

func (*Supervisor) Watch

func (s *Supervisor) Watch(services ...Service)

Watch add one or more services to a supervisor. Services must be added before invoking the Start method, otherwise it panics.

type WithName added in v0.20.0

type WithName interface {
	Service

	ServiceName() string
}

WithName is an optional interface that can be implemented by a service to provide a name. The name is used in logs and metrics.

Jump to

Keyboard shortcuts

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