dex

package
v1.1.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: 21 Imported by: 1

README

dex

dex is a new daemon plugin for cmdr. It's compatible with both windows, linux and macOS.

For more information, see
the example app: service

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 [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"},
					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,
					},
				},
			},
			{
				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 FindDaemonProcess

func FindDaemonProcess() (present bool, process *os.Process)

FindDaemonProcess locates the daemon process if running

func IsErrorAddressAlreadyInUse

func IsErrorAddressAlreadyInUse(err error) bool

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

func SetHotReloadSignals(sigfn func() []os.Signal)

SetHotReloadSignals allows an functor to provide a list of Signals

func SetOnGetListener

func SetOnGetListener(fn func() net.Listener)

SetOnGetListener returns tcp/http listener for daemon hot-restarting

func SetReloadSignals

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

SetReloadSignals allows an functor to provide a list of Signals

func SetSigEmtSignals

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

SetSigEmtSignals allows an functor to provide a list of Signals

func SetTermSignals

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

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(prog *Program, stopCh, doneCh chan struct{}, hotReloadListener net.Listener) (err error)
	OnStop(prog *Program) (err error)
	OnReload(prog *Program)
	OnStatus(prog *Program, p *os.Process) (err error)
	OnInstall(prog *Program) (err error)
	OnUninstall(prog *Program) (err error)

	// OnReadConfigFromCommandLine(root *cmdr.RootCommand)
	BeforeServiceStart(prog *Program, root *cmdr.Command) (err error)
	AfterServiceStop(prog *Program, root *cmdr.Command) (err error)
	OnCmdrPrepare(prog *Program, root *cmdr.RootCommand) (err error)
}

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

type HotReloadable

type HotReloadable interface {
	OnHotReload(prog *Program) (err error)
}

HotReloadable enables hot-restart/hot-reload feature

type Opt

type Opt func()

Opt is functional option type

func WithCommandsModifier

func WithCommandsModifier(modifier func(daemonServerCommand *cmdr.Command) *cmdr.Command) Opt

func WithLoggerForward

func WithLoggerForward(force bool) Opt

WithLoggerForward forwards all log to the out and err files. Typically the files could be found at `/var/log/<appname>/`.

func WithPostAction

func WithPostAction(action func(cmd *cmdr.Command, args []string)) Opt

func WithPreAction

func WithPreAction(action func(cmd *cmdr.Command, args []string) (err error)) Opt

func WithServiceConfig

func WithServiceConfig(config *service.Config) Opt

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
}

func (*Program) EnvFileName

func (p *Program) EnvFileName() string

func (*Program) GetLogFileHandlers

func (p *Program) GetLogFileHandlers() (fOut, fErr *os.File)

func (*Program) LogStderrFileName

func (p *Program) LogStderrFileName() string

func (*Program) LogStdoutFileName

func (p *Program) LogStdoutFileName() string

func (*Program) PidFileName

func (p *Program) PidFileName() string

func (*Program) SocketFileName

func (p *Program) SocketFileName() string

func (*Program) Start

func (p *Program) Start(s service.Service) error

func (*Program) Stop

func (p *Program) Stop(s service.Service) (err error)

func (*Program) WorkDirName

func (p *Program) WorkDirName() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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