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 ¶
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 ¶
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 (*Daemon) Delay ¶
Delay method gets daemon's delay with which processor is called (if it is set).
type Option ¶
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 WithLogger ¶
WithLogger option applies provided logger. Default: standard logger with name equal to daemon's one.
func WithProcessFunc ¶
func WithProcessFunc(processFunc func()) Option
WithProcessFunc option applies provided function as processor. Default: none.
func WithProcessor ¶
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.