Documentation
¶
Overview ¶
Package mkgd (m-k-g-d, or "make go daemon") provides functionality for implementing an init daemon for systems like rc.d in Go. It aims to solve common toil involved in implementing a daemon such as syslog setup, PID file management, setting the daemon process owner user, and creating command-line options that modify those levers at runtime.
Index ¶
- func ChildWaitTimeout(t time.Duration) func(context.Context) <-chan error
- func DefaultPidFilePath() string
- func RunningAsChild() bool
- func RunningAsParent() bool
- type ChildSetupWaitStrateggy
- type CliConfig
- type Config
- type DaemonizeError
- type DaemonizeState
- type IsDaemon
- type IsParent
- type PostExecFnConfig
- type PreDaemonizeFnConfig
- type PreExecFnConfig
- type RuntimeOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ChildWaitTimeout ¶
ChildWaitTimeout returns a function that, when called, creates a channel that is written to when the specified timeout elapses.
This function is meant for use with Config.ChildWaitGiveUpFn.
func DefaultPidFilePath ¶ added in v0.0.5
func DefaultPidFilePath() string
DefaultPidFilePath returns the default PID file path used by the library, which is "/var/run/ARGV_0_NAME.pid" where ARGV_0_NAME is the base name of os.Args[0].
func RunningAsChild ¶
func RunningAsChild() bool
func RunningAsParent ¶
func RunningAsParent() bool
Types ¶
type ChildSetupWaitStrateggy ¶
type ChildSetupWaitStrateggy string
ChildSetupWaitStrateggy configures whether the parent process should wait for the child process to indicate that it is finished setting up.
const ( // DoNotWaitForChild is the default ChildSetupWaitStrategy. DoNotWaitForChild ChildSetupWaitStrateggy = "" // WaitForChildStdoutWrite waits for the child to write to stdout. WaitForChildStdoutWrite ChildSetupWaitStrateggy = "wait-stdout-write" )
type CliConfig ¶
type CliConfig struct {
// PidFilePathArg is the argument string to use for the PID file
// path argument. For example, a value of "P" would result in
// an argument of "-P" being defined.
//
// Defaults to an uppercase letter if empty.
PidFilePathArg string
// PidFilePathDefValue sets the default value for the PID file
// path argument.
//
// Defaults to the value returned by the DefaultPidFilePath
// function.
PidFilePathDefValue string
// RunAsUserArg is the argument string to use for the "run as
// user" argument. For example, a value of "U" would result in
// an argument of "-U" being defined.
//
// Defaults to an uppercase letter if empty.
RunAsUserArg string
// RunAsUserDefValue is the default value for the "run as user"
// argument.
RunAsUserDefValue string
// DoNotExecArg is the argument string to use for the "do not
// exec" argument. For example, a value of "D" would result in
// an argument of "-D" being defined.
//
// Defaults to an uppercase letter if empty.
DoNotExecArg string
// LogToStderrArg is the argument string to use for the log to
// stderr argument. For example, a value of "E" would result in
// an argument of "-E" being defined.
//
// Defaults to an uppercase letter if empty.
LogToStderrArg string
// FlagSet is the flag.FlagSet to add arguments to.
//
// Defaults to flag.CommandLine (the default FlagSet) if nil.
FlagSet *flag.FlagSet
}
CliConfig configures the RuntimeOptionsFromCli function.
type Config ¶
type Config struct {
// RuntimeOptions are options that can be set at runtime (i.e.,
// when the process runs via command-line arguments).
//
// If nil, an empty RuntimeOptions is used with the default
// Go data type values.
//
// This object can be generated from command-line arguments
// using the RuntimeOptionsFromCli function.
RuntimeOptions *RuntimeOptions
// ChildSetupWait specifies if the parent process should wait for
// the child process to tell the parent process it is ready. This
// only applies when RuntimeOptions.DoNotExec is false.
//
// The IsDaemon.NotifyParentReady method should be called by
// the child process when using this feature.
//
// If unspecified, the default behavior is to start the child
// process and not wait for it to tell the parent it is ready.
ChildSetupWait ChildSetupWaitStrateggy
// ChildWaitGiveUpFn is an optional function that specifies
// when the parent process should give up waiting for the
// child process to become ready.
//
// This function is only called if ChildSetupWait is specified
// *and* RuntimeOptions.DoNotExec is false.
//
// If nil, then ChildWaitTimeout is used with a default
// timeout value.
ChildWaitGiveUpFn func(context.Context) <-chan error
// PreDaemonizeFn specifies an optional function to execute
// when Daemonize is called.
//
// If non-nil, this function is *always* called regardless
// of the value of RuntimeOptions.DoNotExec.
PreDaemonizeFn func(context.Context, *PreDaemonizeFnConfig) error
// PreExecFn is an optional function that is executed prior to
// starting the child process. This function is only called if
// it is non-nil *and* RuntimeOptions.DoNotExec is false.
PreExecFn func(context.Context, *PreExecFnConfig) error
// PostExecFn is an optional function that works like PreExecFn,
// but is called after successfully starting the child process.
// The same rules as PreExecFn apply to this function.
PostExecFn func(context.Context, *PostExecFnConfig) error
// ParentCtxFn is an optional function that returns the
// desired context.Context and its associated cancellation
// function for the parent process.
//
// This function is only called if it is non-nil and
// RuntimeOptions.DoNotExec is false.
//
// If nil, then a default signal-based context is used.
ParentCtxFn func() (context.Context, func())
// ChildCtxFn is an optional function that returns the
// desired context.Context and its associated cancellation
// function for the child process.
//
// This function is always called if it non-nil.
//
// If nil, then a default signal-based context is used.
ChildCtxFn func() (context.Context, func())
}
Config configures the Daemonize function's behavior.
type DaemonizeError ¶
type DaemonizeError struct {
// contains filtered or unexported fields
}
DaemonizeError is one of the possible values returned by the Daemonize function. It represents an error condition.
func (DaemonizeError) Error ¶
func (o DaemonizeError) Error() string
func (DaemonizeError) Failed ¶
func (o DaemonizeError) Failed() error
func (DaemonizeError) Unwrap ¶
func (o DaemonizeError) Unwrap() error
type DaemonizeState ¶
type DaemonizeState interface {
// Failed returns an error explaining why daemonization failed.
//
// This should only be called after checking the underlying
// data type of the DaemonizeState.
Failed() error
}
DaemonizeState represents the result of the Daemonize function. Users should use a type switch to check the underlying data type.
func Daemonize ¶
func Daemonize(config *Config) DaemonizeState
Daemonize daemonizes the current process by executing it a second time. The resulting state of the calling code is conveyed by the underlying data type of the returned DaemonizeState object.
Callers should use a Go type switch to get the underlying data type of the returned DaemonizeState object. DaemonizeState can be a pointer to one of the following values:
- *IsParent - Caller is the parent process
- *IsDaemon - Caller is the daemon (child) process (note: this is always returned if RuntimeOptions.DoNotExec is true)
- *DaemonizeError - The function failed. Note: Users can also use the "default" case of the switch statement to capture this state and retrieve the error using the DaemonizeState.Failed method
Each of the above data types provides additonal levers for communicating with the parent or child processes.
type IsDaemon ¶
type IsDaemon struct {
// Ctx is a context.Context for the current process. It is setup
// based on the Config.ChildCtxFn field. Refer to that field's
// documentation for more details.
Ctx context.Context
// CancelCtxFn is a function that cancels the context.Context
// stored in the Ctx field.
CancelCtxFn func()
// contains filtered or unexported fields
}
IsDaemon is returned by Daemonize when the calling code is the daemon process. Refer to the Daemonize function for more details.
func (IsDaemon) NotifyParentReady ¶
func (o IsDaemon) NotifyParentReady()
NotifyParentReady should be called by the child process when using the Config.ChildSetupWait field. Calling this function will inform the parent process that the child is ready and unblock the parent's call to the Daemonize function.
type IsParent ¶
type IsParent struct {
// Ctx is a context.Context for the current process. It is setup
// based on the Config.ParentCtxFn field. Refer to that field's
// documentation for more details.
Ctx context.Context
// CancelCtxFn is a function that cancels the context.Context
// stored in the Ctx field.
CancelCtxFn func()
// Child is the *exec.Cmd representing the running child process.
Child *exec.Cmd
// ChildExited is a channel that is written to *and* closed when
// the child process exits.
//
// Note: This channel is closed by this library after the child
// process exits. After that point, all future read attempts will
// return immediately.
ChildExited <-chan error
}
IsParent is returned by Daemonize when the calling code is the parent process. Refer to the Daemonize function for more details.
type PostExecFnConfig ¶
type PostExecFnConfig struct{}
type PreDaemonizeFnConfig ¶
type PreDaemonizeFnConfig struct{}
type PreExecFnConfig ¶
type RuntimeOptions ¶
type RuntimeOptions struct {
// DoNotExec tells Daemonize to not execute a child process
// or configure syslog.
DoNotExec bool
// NoSyslog disables automatic configuration of the default
// log.Logger to use syslog.
NoSyslog bool
// PidFilePath is the file path of the PID file.
PidFilePath string
// RunAsUser specifies which user to run the child process as.
// If this field is an empty string, then the child process
// will be run as the same user that started the parent.
//
// This field is not used when DoNotExec is true.
RunAsUser string
}
RuntimeOptions customizes the behavior of the Daemonization function at runtime (i.e., when the process is started).
The optional RuntimeOptionsFromCli helper function automates creating this object from command-line arguments.
func RuntimeOptionsFromCli ¶
func RuntimeOptionsFromCli(cliConfig CliConfig) *RuntimeOptions
RuntimeOptionsFromCli derives a RuntimeOptions from command-line arguments. It must be called before executing flag.Parse.
The resulting RuntimeOptions object will be updated by the flag library after command-line arguments are parsed.