daemon

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2020 License: MIT Imports: 13 Imported by: 0

README

cmdr Daemon plugin

Documentation

Index

Constants

This section is empty.

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{"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
					send signal to make program restart itself without broken any connections.
$ {{.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:       "hr",
							Full:        "hot-restart",
							Aliases:     []string{"hot-reload"},
							Description: "hot reload mode.",
						},
						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 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-restart",
					Aliases:     []string{"hot-reload", "live-reload"},
					Description: "hot-restart 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: daemonHotRestart,
				},
			},
			{
				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 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 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),
	opts ...Opt,
) cmdr.ExecOption

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

Deprecated since v1.0.51, will be removed at v1.7.x. please using `cmdr-addons: dex` as the replacement.

Types

type Daemon

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

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

type HotReloadable added in v1.6.19

type HotReloadable interface {
	OnHotReload(ctx *impl.Context) (err error)
}

HotReloadable enables hot-restart/hot-reload feature

type Opt added in v1.6.19

type Opt func()

Opt is functional option type

func WithOnGetListener added in v1.6.19

func WithOnGetListener(fn func() net.Listener) Opt

WithOnGetListener returns tcp/http listener for daemon hot-restarting

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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