daemon

package module
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2023 License: MIT Imports: 11 Imported by: 0

README

go-daemon

License pkg.go.dev reference Build Status Go Report Card Coverage Release

Logo

A daemon package for use with Go services without any dependencies (except for golang.org/x/sys/windows)

Features

  • Use install and uninstall service file on Linux (SystemD, SystemV, UpStart), MacOS and FreeBSD
  • More control: start, stop, restart, reload for all supported OS and pause and continue for Windows
  • Unified interface for all supported OS

Install

go get -u github.com/nsemikov/go-daemon@latest

Usage

Create config for daemon:

cfg := daemon.NewConfig(
  daemon.WithName("cmd_example"),
  daemon.WithDescription("Command Line daemon example"),
  daemon.WithStartHdlr(start),
  daemon.WithStopHdlr(stop),
  // add more options if you need
)

Create daemon:

d, err := daemon.New(cfg)

And then run it:

err = d.Run()

See the examples directory for more complete examples.

Documentation

Overview

Package daemon is endpoint to create cross platform service

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrConfigNotSpecified appears if service configuration is not specified
	ErrConfigNotSpecified = errors.New("service configuration is not specified")

	// ErrMethodErrorNotSpecified appears if error method missed in service config
	ErrMethodErrorNotSpecified = errors.New("error method are not specified in service configuration")

	// ErrMethodInfoNotSpecified appears if info method missed in service config
	ErrMethodInfoNotSpecified = errors.New("info method are not specified in service configuration")

	// ErrSomeMethodsNotSpecified appears if any methods missed in service config
	ErrSomeMethodsNotSpecified = errors.New("some methods are not specified in service configuration")

	// ErrUnsupportedSystem appears if try to use service on system which is not supported by this release
	ErrUnsupportedSystem = errors.New("unsupported system")

	// ErrRootPrivileges appears if run installation or deleting the service without root privileges
	ErrRootPrivileges = errors.New("you must have root user privileges. Possibly using 'sudo' command should help")

	// ErrAlreadyInstalled appears if service already installed on the system
	ErrAlreadyInstalled = errors.New("service has already been installed")

	// ErrNotInstalled appears if try to delete service which was not been installed
	ErrNotInstalled = errors.New("service is not installed")

	// ErrAlreadyRunning appears if try to start already running service
	ErrAlreadyRunning = errors.New("service is already running")

	// ErrAlreadyStopped appears if try to stop already stopped service
	ErrAlreadyStopped = errors.New("service has already been stopped")

	// ErrNotStarted appears if try to reload stopped service
	ErrNotStarted = errors.New("service is not started")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	// Name of daemon
	Name string
	// Description of daemon
	Description string
	// Dependencies of daemon
	Dependencies []string

	// PIDDir is path to the directory containing the pid file.
	// Default value is "/run".
	// Suitable for for SystemV only
	PIDDir string
	// PIDName is empty by default.
	// In this case, Name will be used instead of PIDName
	// Suitable for for SystemV only
	PIDName string

	// StartRunLevels is start run levels, [2, 3, 4, 5] by default.
	// Suitable for for Linux SystemV and UpStart only
	StartRunLevels []int
	// StopRunLevels is stop run levels, [0, 1, 6] by default.
	// Suitable for for Linux SystemV and UpStart only
	StopRunLevels []int

	// TemplateLinuxUpstart contains template for Linux UpStart service file
	TemplateLinuxUpstart string
	// TemplateLinuxSystemV contains template for Linux SystemV service file
	TemplateLinuxSystemV string
	// TemplateLinuxSystemD contains template for Linux SystemD service unit
	TemplateLinuxSystemD string
	// TemplateMacOSPorpertyList contains template for MacOS property list
	TemplateMacOSPorpertyList string
	// TemplateFreeBSDSystemV contains template for FreeBSD SystemV system
	TemplateFreeBSDSystemV string

	// HideMethodsWarning will hide ErrSomeMethodsNotSpecified warning for Pause,
	// Continue and Reload
	HideMethodsWarning bool

	// WindowsStartMode is a mgr.Config.StartType:
	// https://godoc.org/golang.org/x/sys/windows/svc/mgr#Config
	WindowsStartMode uint32
	// WindowsStartAccountName is a mgr.Config.ServiceStartName:
	// https://godoc.org/golang.org/x/sys/windows/svc/mgr#Config
	WindowsStartAccountName string
	// WindowsStartAccountPassword is a mgr.Config.Password:
	// https://godoc.org/golang.org/x/sys/windows/svc/mgr#Config
	WindowsStartAccountPassword string

	// StartHdlr is non-blocking start service handler
	StartHdlr func() error
	// StopHdlr is non-blocking stop service handler
	StopHdlr func() error
	// Pause is non-blocking pause service handler
	PauseHdlr func() error
	// ContinueHdlr is non-blocking continue service handler
	ContinueHdlr func() error
	// ReloadHdlr is non-blocking reload service handler
	ReloadHdlr func() error
	// RunHdlr is blocking service entry-point.
	// Daemon will ignore other handlers if this setted.
	// Not suitable for Windows services.
	RunHdlr func() error

	// ErrorHdlr is non-blocking error message handler
	ErrorHdlr func(format string, args ...interface{})
	// InfoHdlr is non-blocking information message handler
	InfoHdlr func(format string, args ...interface{})
}

Config type

func NewConfig

func NewConfig(opts ...ConfigOption) *Config

NewConfig is a Config constructor. Get the Config properly. Fills the generated Config with default templates.

type ConfigOption added in v0.5.0

type ConfigOption func(*Config)

func WithContinueHdlr added in v0.5.0

func WithContinueHdlr(hdlr func() error) ConfigOption

func WithDependencies added in v0.5.0

func WithDependencies(dependencies ...string) ConfigOption

func WithDescription added in v0.5.0

func WithDescription(description string) ConfigOption

func WithErrorHdlr added in v0.5.0

func WithErrorHdlr(hdlr func(format string, args ...interface{})) ConfigOption

func WithHideMethodsWarning added in v0.5.0

func WithHideMethodsWarning(hideMethodsWarning bool) ConfigOption

func WithInfoHdlr added in v0.5.0

func WithInfoHdlr(hdlr func(format string, args ...interface{})) ConfigOption

func WithName added in v0.5.0

func WithName(name string) ConfigOption

func WithPIDDir added in v0.5.0

func WithPIDDir(dir string) ConfigOption

func WithPIDName added in v0.5.0

func WithPIDName(name string) ConfigOption

func WithPauseHdlr added in v0.5.0

func WithPauseHdlr(hdlr func() error) ConfigOption

func WithReloadHdlr added in v0.5.0

func WithReloadHdlr(hdlr func() error) ConfigOption

func WithRunHdlr added in v0.5.0

func WithRunHdlr(hdlr func() error) ConfigOption

func WithStartHdlr added in v0.5.0

func WithStartHdlr(hdlr func() error) ConfigOption

func WithStartRunLevels added in v0.5.0

func WithStartRunLevels(lvls ...int) ConfigOption

func WithStopHdlr added in v0.5.0

func WithStopHdlr(hdlr func() error) ConfigOption

func WithStopRunLevels added in v0.5.0

func WithStopRunLevels(lvls ...int) ConfigOption

func WithTemplateFreeBSDSystemV added in v0.5.0

func WithTemplateFreeBSDSystemV(template string) ConfigOption

func WithTemplateLinuxSystemD added in v0.5.0

func WithTemplateLinuxSystemD(template string) ConfigOption

func WithTemplateLinuxSystemV added in v0.5.0

func WithTemplateLinuxSystemV(template string) ConfigOption

func WithTemplateLinuxUpstart added in v0.5.0

func WithTemplateLinuxUpstart(template string) ConfigOption

func WithTemplateMacOSPorpertyList added in v0.5.0

func WithTemplateMacOSPorpertyList(template string) ConfigOption

func WithWindowsStartAccountName added in v0.5.0

func WithWindowsStartAccountName(name string) ConfigOption

func WithWindowsStartAccountPassword added in v0.5.0

func WithWindowsStartAccountPassword(password string) ConfigOption

func WithWindowsStartMode added in v0.5.0

func WithWindowsStartMode(mode uint32) ConfigOption

type Daemon

type Daemon interface {
	Install(args ...string) (string, error)
	Pause() (string, error)
	Reload() (string, error)
	Restart() (string, error)
	Continue() (string, error)
	Status() (string, error)
	Start() (string, error)
	Stop() (string, error)
	Uninstall() (string, error)
	Run() error
}

Daemon interface.

func Must added in v0.4.4

func Must(c *Config) Daemon

Must create Daemon or panic.

func New

func New(c *Config) (Daemon, error)

New is Daemon constructor. Get the Daemon properly.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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