daemon

package
v0.1.11 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2023 License: Apache-2.0, BSD-2-Clause Imports: 8 Imported by: 30

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDaemonAlreadyStopped                 = errors.New("daemon was already stopped")
	ErrDuplicateBackgroundWorker            = errors.New("duplicate background worker")
	ErrExistingBackgroundWorkerStillRunning = errors.New("existing background worker is still running")
)

Errors for the daemon package.

Functions

func BackgroundWorker

func BackgroundWorker(name string, handler WorkerFunc, priority ...int) error

BackgroundWorker adds a new background worker to the default daemon instance. Use shutdownOrderWorker to define in which shutdown order this particular background worker is shut down (higher = earlier).

func ContextStopped

func ContextStopped() context.Context

ContextStopped returns a context that is done when the deamon is stopped.

func DebugLogger

func DebugLogger(logger *logger.Logger)

DebugLogger allows to pass a logger to the daemon to issue log messages for debugging purposes.

func GetRunningBackgroundWorkers

func GetRunningBackgroundWorkers() []string

GetRunningBackgroundWorkers gets the running background workers of the default daemon instance.

func IsRunning

func IsRunning() bool

IsRunning checks whether the default daemon instance is running.

func IsStopped

func IsStopped() bool

IsStopped checks whether the default daemon instance was stopped.

func Run

func Run()

Run runs the default daemon instance and then waits for the daemon to shutdown.

func Shutdown

func Shutdown()

Shutdown signals all background worker of the default daemon instance to shut down. This call doesn't await termination of the background workers.

func ShutdownAndWait

func ShutdownAndWait()

ShutdownAndWait signals all background worker of the default daemon instance to shut down and then waits for their termination.

func Start

func Start()

Start starts the default daemon instance.

Types

type Daemon

type Daemon interface {
	// GetRunningBackgroundWorkers gets the running background workers.
	GetRunningBackgroundWorkers() []string

	// BackgroundWorker adds a new background worker to the daemon.
	// Use order to define in which shutdown order this particular
	// background worker is shut down (higher = earlier).
	BackgroundWorker(name string, handler WorkerFunc, order ...int) error

	// DebugLogger allows to pass a logger to the daemon to issue log messages for debugging purposes.
	DebugLogger(logger *logger.Logger)

	// Start starts the daemon.
	Start()

	// Run runs the daemon and then waits for the daemon to shutdown.
	Run()

	// Shutdown signals all background worker of the daemon shut down.
	// This call doesn't await termination of the background workers.
	Shutdown()

	// Shutdown signals all background worker of the daemon to shut down and
	// then waits for their termination.
	ShutdownAndWait()

	// IsRunning checks whether the daemon is running.
	IsRunning() bool

	// IsStopped checks whether the daemon was stopped.
	IsStopped() bool

	// ContextStopped returns a context that is done when the deamon is stopped.
	ContextStopped() context.Context
}

Daemon specifies an interface to run background go routines.

type OrderedDaemon

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

OrderedDaemon is an orchestrator for background workers. stopOnce ensures that the daemon can only be terminated once.

func New

func New() *OrderedDaemon

New creates a new daemon instance.

func (*OrderedDaemon) BackgroundWorker

func (d *OrderedDaemon) BackgroundWorker(name string, handler WorkerFunc, order ...int) error

BackgroundWorker adds a new background worker to the daemon. Use order to define in which shutdown order this particular background worker is shut down (higher = earlier).

func (*OrderedDaemon) ContextStopped

func (d *OrderedDaemon) ContextStopped() context.Context

ContextStopped returns a context that is done when the deamon is stopped.

func (*OrderedDaemon) DebugLogger

func (d *OrderedDaemon) DebugLogger(logger *logger.Logger)

DebugLogger allows to pass a logger to the daemon to issue log messages for debugging purposes.

func (*OrderedDaemon) GetRunningBackgroundWorkers

func (d *OrderedDaemon) GetRunningBackgroundWorkers() []string

GetRunningBackgroundWorkers gets the running background workers sorted by their priority.

func (*OrderedDaemon) IsRunning

func (d *OrderedDaemon) IsRunning() bool

IsRunning checks whether the daemon is running.

func (*OrderedDaemon) IsStopped

func (d *OrderedDaemon) IsStopped() bool

IsStopped checks whether the daemon was stopped.

func (*OrderedDaemon) Run

func (d *OrderedDaemon) Run()

Run runs the daemon and then waits for the daemon to shutdown.

func (*OrderedDaemon) Shutdown

func (d *OrderedDaemon) Shutdown()

Shutdown signals all background worker of the daemon shut down. This call doesn't await termination of the background workers.

func (*OrderedDaemon) ShutdownAndWait

func (d *OrderedDaemon) ShutdownAndWait()

ShutdownAndWait signals all background worker of the daemon to shut down and then waits for their termination.

func (*OrderedDaemon) Start

func (d *OrderedDaemon) Start()

Start starts the daemon.

type WorkerFunc

type WorkerFunc = func(ctx context.Context)

WorkerFunc is the function to run a worker accepting its context.

Jump to

Keyboard shortcuts

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