daemon

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2019 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const DaemonizedKey = "demonized"

DaemonizedKey is the keyPath to ensure you are running in demonized mode.

Variables

View Source
var (
	// DaemonServerCommand defines a group of sub-commands for daemon operations.
	DaemonServerCommand = &cmdr.Command{
		BaseOpt: cmdr.BaseOpt{

			Short:       "s",
			Full:        "server",
			Aliases:     []string{"serve", "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}} status
					display the daemonized program running status.
$ {{.AppName}} install [--systemd]
					install program as a systemd service.
$ {{.AppName}} uninstall
					remove the installed systemd service.
`,
				},
				Flags: []*cmdr.Flag{
					{
						BaseOpt: cmdr.BaseOpt{
							Short:       "f",
							Full:        "foreground",
							Aliases:     []string{"fg"},
							Description: "run on foreground, NOT daemonized.",
						},
						DefaultValue: false,
					},
				},
			},
			{
				BaseOpt: cmdr.BaseOpt{
					Short:       "t",
					Full:        "stop",
					Aliases:     []string{"stp", "halt", "pause"},
					Description: "stop this system service/daemon.",
					Action:      daemonStop,
				},
				Flags: []*cmdr.Flag{
					{
						BaseOpt: cmdr.BaseOpt{
							Short:       "1",
							Full:        "hup",
							Description: "send SIGHUP - to reload service",
						},
						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:       "re",
					Full:        "restart",
					Aliases:     []string{"reload"},
					Description: "restart this system service/daemon.",
					Action:      daemonRestart,
				},
			},
			{
				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,
					},
				},
			},
			{
				BaseOpt: cmdr.BaseOpt{
					Short:       "u",
					Full:        "uninstall",
					Aliases:     []string{"remove"},
					Description: "remove from a system service/daemon.",
					Group:       "Config",
					Action:      daemonUninstall,
				},
			},
		},
	}
)
View Source
var ErrNoRoot = errors.New("MUST have administrator privileges")

ErrNoRoot error object: `MUST have administrator privileges`

Functions

func Enable deprecated

func Enable(daemonImplX Daemon,
	modifier func(daemonServerCommand *cmdr.Command) *cmdr.Command,
	preAction func(cmd *cmdr.Command, args []string) (err error),
	postAction func(cmd *cmdr.Command, args []string),
)

Enable enables the daemon plugin: - add daemon commands and sub-commands: start/run, stop, restart/reload, status, install/uninstall - pidfile - go-daemon supports -

Deprecated: from v1.5.0, replaced with WithDaemon()

func IsRunningInDemonizedMode added in v1.1.5

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 QuitSignals added in v1.0.0

func QuitSignals() chan os.Signal

QuitSignals return a channel for quit signal raising up.

func SetReloadSignals added in v1.1.7

func SetReloadSignals(sig func() []os.Signal)

SetReloadSignals allows an functor to provide a list of Signals

func SetSigEmtSignals added in v1.5.0

func SetSigEmtSignals(sig func() []os.Signal)

SetSigEmtSignals allows an functor to provide a list of Signals

func SetTermSignals added in v1.1.7

func SetTermSignals(sig func() []os.Signal)

SetTermSignals allows an functor to provide a list of Signals

func StopSelf added in v1.0.0

func StopSelf()

StopSelf will terminate the app gracefully

func WithDaemon added in v1.5.0

func WithDaemon(daemonImplX Daemon,
	modifier func(daemonServerCommand *cmdr.Command) *cmdr.Command,
	preAction func(cmd *cmdr.Command, args []string) (err error),
	postAction func(cmd *cmdr.Command, args []string),
) cmdr.ExecOption

WithDaemon enables daemon plugin: - add daemon commands and sub-commands: start/run, stop, restart/reload, status, install/uninstall - pidfile - go-daemon supports -

Types

type Context

type Context struct {
	daemon.Context
}

Context of daemon operations

type Daemon

type Daemon interface {
	OnRun(cmd *cmdr.Command, args []string, stopCh, doneCh chan struct{}) (err error)
	OnStop(cmd *cmdr.Command, args []string) (err error)
	OnReload()
	OnStatus(cxt *Context, cmd *cmdr.Command, p *os.Process) (err error)
	OnInstall(cxt *Context, cmd *cmdr.Command, args []string) (err error)
	OnUninstall(cxt *Context, cmd *cmdr.Command, args []string) (err error)
}

Daemon interface should be implemented when you are using `daemon.Enable()`.

Jump to

Keyboard shortcuts

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