Documentation ¶
Index ¶
- Variables
- func FindDaemonProcess() (present bool, process *os.Process)
- func IsErrorAddressAlreadyInUse(err error) bool
- func IsPidFileExists() bool
- func IsRunningInDemonizedMode() bool
- func SetHotReloadSignals(sigfn func() []os.Signal)
- func SetOnGetListener(fn func() net.Listener)
- func SetReloadSignals(sigfn func() []os.Signal)
- func SetSigEmtSignals(sigfn func() []os.Signal)
- func SetTermSignals(sigfn func() []os.Signal)
- func WithDaemon(daemonImplObject Daemon, opts ...Opt) cmdr.ExecOption
- type Daemon
- type HotReloadable
- type Opt
- func WithCommandsModifier(modifier func(daemonServerCommand *cmdr.Command) *cmdr.Command) Opt
- func WithLogger(logger log.Logger) Opt
- func WithLoggerForward(force bool) Opt
- func WithPostAction(action func(cmd *cmdr.Command, args []string)) Opt
- func WithPreAction(action func(cmd *cmdr.Command, args []string) (err error)) Opt
- func WithServiceConfig(config *service.Config) Opt
- type Program
- func (p *Program) EnvFileName() string
- func (p *Program) GetLogFileHandlers() (fOut, fErr *os.File)
- func (p *Program) LogStderrFileName() string
- func (p *Program) LogStdoutFileName() string
- func (p *Program) PidFileName() string
- func (p *Program) SocketFileName() string
- func (p *Program) Start(s service.Service) error
- func (p *Program) Stop(s service.Service) (err error)
- func (p *Program) WorkDirName() string
Constants ¶
This section is empty.
Variables ¶
var ( // DaemonServerCommand defines a group of sub-commands for daemon operations. DaemonServerCommand = &cmdr.Command{ BaseOpt: cmdr.BaseOpt{ Short: "s", Full: "server", Aliases: []string{"svr", "daemon"}, Description: "server ops: for linux daemon.", Group: "Daemonization", }, Flags: []*cmdr.Flag{ { BaseOpt: cmdr.BaseOpt{ Short: "p", Full: "port", Description: "main port (RESTful).", }, DefaultValue: 3000, }, }, SubCommands: []*cmdr.Command{ { BaseOpt: cmdr.BaseOpt{ Short: "s", Full: "start", Aliases: []string{"run", "startup"}, Description: "startup this system service/daemon.", Action: daemonStart, LongDescription: `**start** command make Program running as a daemon background. **run** command make Program running in current tty foreground. `, Examples: ` $ {{.AppName}} start make Program running as a daemon background. $ {{.AppName}} start --foreground make Program running in current tty foreground. $ {{.AppName}} run make Program running in current tty foreground. $ {{.AppName}} stop stop daemonized Program. $ {{.AppName}} reload send signal to trigger Program reload its configurations. $ {{.AppName}} hot-reload [TODO] send signal to make Program restart itself without broken any connections. $ {{.AppName}} status display the demonized Program running status. $ {{.AppName}} install install Program as a daemon service (win, macOS, linux systemd/upstart/init). $ {{.AppName}} uninstall remove the installed daemon service. `, }, Flags: []*cmdr.Flag{ { BaseOpt: cmdr.BaseOpt{ Short: "f", Full: "foreground", Aliases: []string{"fg"}, Description: "run on foreground, instead of demonized.", }, DefaultValue: false, }, { BaseOpt: cmdr.BaseOpt{ Short: "hr", Full: "in-hot-reload", Aliases: []string{"hot-restart"}, Description: "app is been running in hot reload mode.", Hidden: true, }, DefaultValue: false, }, { BaseOpt: cmdr.BaseOpt{ Short: "", Full: "in-daemon", Description: "app is been running in daemon mode (special for windows service).", Hidden: true, }, DefaultValue: false, }, }, }, { BaseOpt: cmdr.BaseOpt{ Short: "t", Full: "stop", Aliases: []string{"halt", "pause", "shutdown"}, Description: "stop this system service/daemon.", Action: daemonStop, }, Flags: []*cmdr.Flag{ { BaseOpt: cmdr.BaseOpt{ Short: "1", Full: "hup", Description: "send SIGHUP - to reload service configurations", }, DefaultValue: false, }, { BaseOpt: cmdr.BaseOpt{ Short: "3", Full: "quit", Description: "send SIGQUIT - to quit service gracefully", }, DefaultValue: false, }, { BaseOpt: cmdr.BaseOpt{ Short: "9", Full: "kill", Description: "send SIGKILL - to quit service unconditionally", }, DefaultValue: false, }, { BaseOpt: cmdr.BaseOpt{ Short: "15", Full: "term", Description: "send SIGTERM - to quit service gracefully", }, DefaultValue: false, }, { BaseOpt: cmdr.BaseOpt{ Short: "31", Full: "usr2", Description: "send SIGUSR2 - to hot-restart service gracefully", }, DefaultValue: false, }, }, }, { BaseOpt: cmdr.BaseOpt{ Short: "re", Full: "restart", Aliases: []string{"reload"}, Description: "reload configurations for this system service/daemon.", Action: daemonRestart, }, }, { BaseOpt: cmdr.BaseOpt{ Short: "hr", Full: "hot-reload", Aliases: []string{"hot-restart", "live-reload"}, Description: "hot-reload this system service/daemon.", LongDescription: `hot-restart/hot-reload/live-reload: This action will start a new child process and transfer all living connections to the child, and shutdown itself gracefully. With this action, the service will keep serving without broken. `, Action: daemonHotReload, }, }, { BaseOpt: cmdr.BaseOpt{ Short: "ss", Full: "status", Aliases: []string{"st"}, Description: "display its running status as a system service/daemon.", Action: daemonStatus, }, }, { BaseOpt: cmdr.BaseOpt{ Short: "i", Full: "install", Aliases: []string{"setup"}, Description: "install as a system service/daemon.", Group: "Config", Action: daemonInstall, }, Flags: []*cmdr.Flag{ { BaseOpt: cmdr.BaseOpt{ Short: "s", Full: "systemd", Aliases: []string{"sys"}, Description: "install as a systemd service.", }, DefaultValue: true, ToggleGroup: "service-type", }, { BaseOpt: cmdr.BaseOpt{ Short: "u", Full: "user", Aliases: []string{"username"}, Description: "service will run under this user.", }, DefaultValue: "", }, { BaseOpt: cmdr.BaseOpt{ Short: "g", Full: "group", Aliases: []string{"groupname"}, Description: "service will run under this group.", }, DefaultValue: "", }, }, }, { BaseOpt: cmdr.BaseOpt{ Short: "u", Full: "uninstall", Aliases: []string{"remove"}, Description: "remove from a system service/daemon.", Group: "Config", Action: daemonUninstall, }, Flags: []*cmdr.Flag{ { BaseOpt: cmdr.BaseOpt{ Short: "s", Full: "systemd", Aliases: []string{"sys"}, Description: "uninstall the systemd service.", }, DefaultValue: true, ToggleGroup: "service-type", }, }, }, }, } )
var ErrNoRoot = errors.New("MUST have administrator privileges")
ErrNoRoot error object: `MUST have administrator privileges`
Functions ¶
func FindDaemonProcess ¶
FindDaemonProcess locates the daemon process if running
func IsErrorAddressAlreadyInUse ¶
IsErrorAddressAlreadyInUse tests if "bind: address already in use" found
func IsPidFileExists ¶
func IsPidFileExists() bool
IsPidFileExists checks if the pid file exists or not
func IsRunningInDemonizedMode ¶
func IsRunningInDemonizedMode() bool
IsRunningInDemonizedMode returns true if you are running under demonized mode. false means that you're running in normal console/tty mode.
func SetHotReloadSignals ¶
SetHotReloadSignals allows an functor to provide a list of Signals
func SetOnGetListener ¶
SetOnGetListener returns tcp/http listener for daemon hot-restarting
func SetReloadSignals ¶
SetReloadSignals allows an functor to provide a list of Signals
func SetSigEmtSignals ¶
SetSigEmtSignals allows an functor to provide a list of Signals
func SetTermSignals ¶
SetTermSignals allows an functor to provide a list of Signals
func WithDaemon ¶
func WithDaemon(daemonImplObject Daemon, opts ...Opt, ) cmdr.ExecOption
WithDaemon enables daemon plugin: - add daemon commands and sub-commands: start/run, stop, restart/reload, status, install/uninstall - pidfile -
Types ¶
type Daemon ¶
type Daemon interface { // OnRun will be invoked when daemon being started, run/fork at foreground, hot reload ... OnRun(program *Program, stopCh, doneCh chan struct{}, hotReloadListener net.Listener) (err error) OnStop(program *Program) (err error) OnReload(program *Program) OnStatus(program *Program, p *os.Process) (err error) OnInstall(program *Program) (err error) OnUninstall(program *Program) (err error) // OnReadConfigFromCommandLine(root *cmdr.RootCommand) BeforeServiceStart(program *Program, root *cmdr.Command) (err error) AfterServiceStop(program *Program, root *cmdr.Command) (err error) OnCmdrPrepare(program *Program, root *cmdr.RootCommand) (err error) }
Daemon interface should be implemented when you are using `daemon.Enable()`.
type HotReloadable ¶
HotReloadable enables hot-restart/hot-reload feature
type Opt ¶
type Opt func()
Opt is functional option type
func WithCommandsModifier ¶
WithCommandsModifier could specify a modifier, which allows the tune can be applied onto daemon-server-command.
func WithLogger ¶ added in v1.7.6
WithLogger sets the user-customizable logger
func WithLoggerForward ¶
WithLoggerForward forwards all logging outputs to the out and err files. Typically the files could be found at `/var/log/<appname>/`.
func WithPostAction ¶
WithPostAction appends a post-action handler to the post-actions chain.
func WithPreAction ¶
WithPreAction appends a pre-action handler to the pre-actions chain.
func WithServiceConfig ¶
WithServiceConfig appends a customized *service.Config object
type Program ¶
type Program struct { Config *service.Config Service service.Service Logger service.Logger // the arguments of cmdr entry. Command *cmdr.Command Args []string Env []string // InvokedInDaemon will be set to true if this daemon service is running under service/daemon manager. // For Windows, it's available if the service app has been starting from serivces.msc or by system automatically. // For macOS, the Launchctl starts it. // For Linux, systemd/upstart/sysv does it. // // The underlying detector InvokedInDaemon bool // InvokedDirectly means that administrator is running it from a tty/console/terminal. InvokedDirectly bool // ForwardLogToFile enables logging forward to /var/log if systemd mode enabled. ForwardLogToFile bool // contains filtered or unexported fields }