Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Daemonize ¶
func Daemonize(ctx context.Context, envkey string, check HealthChecker) error
Daemonize runs the current program in the background as a daemon. This function should be called during the program startup, before performing any other significant work, like opening databases, opening network connections, etc.
Daemonize execs the current program with all the same command-line arguments, but with limited environment and an additional `envkey` variable that indicates background mode to the new child process.
Users are required to pass an unique, application-specific, non-empty environment variable name to indicate to the background process that it is a daemon and to put itself in the background.
Standard input and outputs of the background process are replaced with `/dev/null`. Standard library's log output is redirected to the `io.Discard` backend. Current working directory of the background process is changed to the root directory. Background process's environment is also restricted to just PATH, USER, HOME and the user supplied "envkey" variables.
Parent process will use the check function to wait for the background process to initialize successfully or die unsuccessfully. Health checker function is expected to verify that a new instance of child process is initialized successfully.
When successfull, Daemonize returns nil to the background process and exits in the parent process (i.e., never returns). When unsuccessful, Daemonize returns non-nil error to the parent process and kills the background process (i.e., never returns).
Types ¶
type HealthChecker ¶
HealthChecker is a function that checks for the initialization of the background process. Health checker function is run in the parent process after spawning the background child process.
Health check function should return retry=false and err=nil after successful initialization or retry=false and err=non-nil for initialization failure. Health check is performed repeatedly as long as retry=true and the Daemonize context is not expired.
As an example, child process could be implemented expose a REST endpoint with it's process id after it initializes successfully and health checker function could probe for matching process id at the well-known REST endpoint to confirm child process's successful initialization in the background.