daemon

package
v0.0.0-...-93b1e75 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package daemon provides service that runs blocking loop. It will be stopped after receiving external system signal TERM or INT. Also you can prematurely stop daemon if you pass cancellable context into Run method.

Optionally you can set some custom processor that will be called continuously with provided delay.

Typical usage:

// ... Setup any services you need and run them as goroutines
daemon.New(
    daemon.WithName("my-daemon"),
    daemon.WithDelay(1000),
    daemon.WithProcessor(myProcessor),
).Run(ctx)
// ... Shutdown your services and free resources

Index

Constants

View Source
const (
	// DefDelay is the default process delay interval in milliseconds.
	DefDelay = 1
	// DefName is the default daemon's name.
	DefName = "daemon"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

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

Config structure with daemon configuration. Shouldn't be created manually.

type ConfigService

type ConfigService interface {
	GetByKey(string, interface{}) error
	IsNoSuchKeyError(error) bool
}

ConfigService interface used to obtain configuration from somewhere into some specific structure.

type Daemon

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

Daemon structure that provides daemon service. Don't create manually, use the functions down below instead.

func MustNew

func MustNew(opts ...Option) *Daemon

MustNew function creates new daemon with provided options and panics on any error.

func New

func New(opts ...Option) (*Daemon, error)

New function creates new daemon with provided options.

func (*Daemon) Delay

func (daemon *Daemon) Delay() time.Duration

Delay method gets daemon's delay with which processor is called (if it is set).

func (*Daemon) Name

func (daemon *Daemon) Name() string

Name method gets daemon's name.

func (*Daemon) Run

func (daemon *Daemon) Run(ctx context.Context)

Run method runs daemon blocking loop. It will be stopped after receiving TERM or INT system signal. You can also pass cancellable context to stop daemon prematurely. If processor was set, it will be called at regular intervals according to delay setting.

func (*Daemon) SigChan

func (daemon *Daemon) SigChan() chan<- os.Signal

SigChan method gets daemon's signal channel. You can send any value there to simulate incoming system signal.

type Option

type Option func(*Config) error

Option function that is fed to New and MustNew. Obtain them using 'With' functions down below.

func WithConfig

func WithConfig(service ConfigService, key string) Option

WithConfig option retrieves configuration from provided configuration service.

Example JSON configuration with all possible fields (if some are not present, defaults will be used):

{
    "name": "daemon-name",
    "delay": 2000
}

func WithDelay

func WithDelay(delay int) Option

WithDelay option applies provided process delay in milliseconds. Default: 1.

func WithLogger

func WithLogger(logger log.Logger) Option

WithLogger option applies provided logger. Default: standard logger with name equal to daemon's one.

func WithName

func WithName(name string) Option

WithName option applies provided daemon name. Default: "daemon".

func WithProcessFunc

func WithProcessFunc(processFunc func()) Option

WithProcessFunc option applies provided function as processor. Default: none.

func WithProcessor

func WithProcessor(processor Processor) Option

WithProcessor option applies provided implementation of Processor interface. Default: none.

type ProcessFunc

type ProcessFunc func()

ProcessFunc is a function implementing Processor interface.

func (ProcessFunc) Process

func (processFunc ProcessFunc) Process()

Process method here just calls the underlying function itself.

type Processor

type Processor interface {
	Process()
}

Processor interface. You can assign its implementation to daemon, and it's Process method will be called at regular intervals according to delay setting.

Jump to

Keyboard shortcuts

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