supervisor

package
v0.0.0-...-6378e9d Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2024 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

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

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

type Delayed

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

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

func NewDelayed

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

NewDelayed returns a new Delayed service.

func (*Delayed) Start

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

Start implements the Service interface.

func (*Delayed) Wait

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

Wait implements the Service interface.

type Reloader

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

Reloader is a service that can reload another wrapped service.

func NewReloader

func NewReloader(cfg ReloaderConfig) *Reloader

NewReloader returns a new Reloader instance.

func (*Reloader) ServiceName

func (r *Reloader) ServiceName() string

ServiceName implements the supervisor.WithName interface.

func (*Reloader) Start

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

Start implements the Service interface.

func (*Reloader) Wait

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

Wait implements the Service interface.

type ReloaderConfig

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

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