Documentation
¶
Overview ¶
Package daemonize wraps a cobra command with Unix daemon lifecycle controls — start, stop, status, and reload — by re-execing the binary as a detached background process. The wrapped command runs in the foreground; the daemon manages backgrounding, a pid file, log streaming during startup and shutdown, and signal-based readiness, all without mutating the command.
Index ¶
- type Daemon
- type DaemonImpl
- func (d *DaemonImpl[T]) DetachOn(detachSig <-chan struct{}) T
- func (d *DaemonImpl[T]) FromCobra(command *cobra.Command) Daemon[*cobra.Command]
- func (d *DaemonImpl[T]) IsAlive() bool
- func (d *DaemonImpl[T]) LogFile() (string, error)
- func (d *DaemonImpl[T]) Name() (string, error)
- func (d *DaemonImpl[T]) PID() (int, error)
- func (d *DaemonImpl[T]) PIDFile() (string, error)
- func (d *DaemonImpl[T]) Reload() error
- func (d *DaemonImpl[T]) Status() error
- func (d *DaemonImpl[T]) Stop() error
- func (d *DaemonImpl[T]) WithGroup(name *string) Daemon[T]
- func (d *DaemonImpl[T]) WithName(name string) Daemon[T]
- func (d *DaemonImpl[T]) WithReload(sig syscall.Signal) Daemon[T]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Daemon ¶
type Daemon[T any] interface { // FromCobra wraps a cobra command, retyping the builder to *cobra.Command so // Into returns the assembled root command. FromCobra(inner *cobra.Command) Daemon[*cobra.Command] // DetachOn assembles the lifecycle wrapper and returns it (as T). detachSig is // closed by the wrapped command once it is ready; the daemon relays that so // "start" stops streaming and detaches. Pass nil for no readiness relay. // After FromCobra, T is *cobra.Command (the root command to Execute). DetachOn(detachSig <-chan struct{}) T // WithReload enables the "reload" subcommand and sets the signal it sends to // the running process. It must match the signal the wrapped command listens // on. Without it, no reload subcommand is registered. WithReload(sig syscall.Signal) Daemon[T] // WithName overrides the state-file base name. By default it is derived from // the wrapped command's path (e.g. "server-serve"); WithName("foo") yields // ".foo.pid"/".foo.log" instead. WithName(name string) Daemon[T] // WithGroup sets the lifecycle help group's title (a trailing ":" is added); // pass nil to ungroup (list them under Additional Commands). Unset, they are // grouped under "Daemon Commands:". WithGroup(name *string) Daemon[T] // Stop signals the running process (SIGTERM, escalating to SIGKILL on // timeout) and waits for it to exit. Usable without building the cobra tree. Stop() error // Status reports whether the process is running and clears a stale pid file. Status() error // Reload sends the configured reload signal (WithReload, default SIGHUP) to // the running process. Reload() error // PID reads the process ID from the pid file. PID() (int, error) // IsAlive reports whether the process named by the pid file is running. IsAlive() bool // PIDFile returns the path to the pid file, or an error if it is not yet // resolved (DetachOn has not run). PIDFile() (string, error) // LogFile returns the path to the log file, or an error if it is not yet // resolved (DetachOn has not run). LogFile() (string, error) // Name returns the effective state-file base name (WithName override or the // derived command path), or an error if it is not yet resolved. Name() (string, error) }
Daemon is the builder for a background-lifecycle wrapper around a foreground command. Configure it with the With* methods, then call IntoCobra. Obtain one from FromCobra.
type DaemonImpl ¶
type DaemonImpl[T any] struct { // contains filtered or unexported fields }
DaemonImpl is the default Daemon implementation. T is the wrapped value's type (and what Into returns).
func (*DaemonImpl[T]) DetachOn ¶
func (d *DaemonImpl[T]) DetachOn(detachSig <-chan struct{}) T
DetachOn records the readiness channel, then assembles the lifecycle wrapper for the wrapped value and returns it (as T), dispatching on inner's type.
func (*DaemonImpl[T]) FromCobra ¶
FromCobra wraps command, retyping the builder to *cobra.Command so Into returns the assembled root command. Configure with the With* methods after this call; reload is disabled until WithReload sets a signal.
func (*DaemonImpl[T]) IsAlive ¶
func (d *DaemonImpl[T]) IsAlive() bool
func (*DaemonImpl[T]) LogFile ¶
func (d *DaemonImpl[T]) LogFile() (string, error)
func (*DaemonImpl[T]) Name ¶
func (d *DaemonImpl[T]) Name() (string, error)
func (*DaemonImpl[T]) PID ¶
func (d *DaemonImpl[T]) PID() (int, error)
func (*DaemonImpl[T]) PIDFile ¶
func (d *DaemonImpl[T]) PIDFile() (string, error)
func (*DaemonImpl[T]) Reload ¶
func (d *DaemonImpl[T]) Reload() error
func (*DaemonImpl[T]) Status ¶
func (d *DaemonImpl[T]) Status() error
func (*DaemonImpl[T]) Stop ¶
func (d *DaemonImpl[T]) Stop() error
func (*DaemonImpl[T]) WithGroup ¶
func (d *DaemonImpl[T]) WithGroup(name *string) Daemon[T]
func (*DaemonImpl[T]) WithName ¶
func (d *DaemonImpl[T]) WithName(name string) Daemon[T]
func (*DaemonImpl[T]) WithReload ¶
func (d *DaemonImpl[T]) WithReload(sig syscall.Signal) Daemon[T]
Directories
¶
| Path | Synopsis |
|---|---|
|
Package e2e builds each example binary and drives its lifecycle end-to-end, asserting the behavior that example demonstrates.
|
Package e2e builds each example binary and drives its lifecycle end-to-end, asserting the behavior that example demonstrates. |
|
examples
|
|
|
grouped
command
Command grouped sets a custom help group title for the lifecycle subcommands via WithGroup.
|
Command grouped sets a custom help group title for the lifecycle subcommands via WithGroup. |
|
minimal
command
Command minimal is the smallest daemonize example: wrap a worker, stream its startup via a readiness channel, and get start/stop/status.
|
Command minimal is the smallest daemonize example: wrap a worker, stream its startup via a readiness channel, and get start/stop/status. |
|
named
command
Command named overrides the state-file base name with WithName, so the pid and log files are ".../<cache>/.serve/widget.{pid,log}" instead of being derived from the command path.
|
Command named overrides the state-file base name with WithName, so the pid and log files are ".../<cache>/.serve/widget.{pid,log}" instead of being derived from the command path. |
|
reload
command
Command reload adds the "reload" subcommand, wired to SIGHUP.
|
Command reload adds the "reload" subcommand, wired to SIGHUP. |
|
shutdown-error
command
Command shutdown-error demonstrates a wrapped command that starts fine but errors during shutdown.
|
Command shutdown-error demonstrates a wrapped command that starts fine but errors during shutdown. |
|
slow-shutdown
command
Command slow-shutdown demonstrates a wrapped command with a slow, graceful shutdown.
|
Command slow-shutdown demonstrates a wrapped command with a slow, graceful shutdown. |
|
slow-start
command
Command slow-start demonstrates a wrapped command with a slow startup.
|
Command slow-start demonstrates a wrapped command with a slow startup. |
|
start-error
command
Command start-error demonstrates a wrapped command that fails during startup (before signaling readiness).
|
Command start-error demonstrates a wrapped command that fails during startup (before signaling readiness). |
|
subcommand
command
Command subcommand shows daemonize mounted under a larger cobra tree: the app has its own init/create/delete subcommands, and "subcommand run" is the daemonized entry point (so "subcommand run start", "subcommand run stop", "subcommand run serve", ...
|
Command subcommand shows daemonize mounted under a larger cobra tree: the app has its own init/create/delete subcommands, and "subcommand run" is the daemonized entry point (so "subcommand run start", "subcommand run stop", "subcommand run serve", ... |
|
ungrouped
command
Command ungrouped passes WithGroup(nil) so the lifecycle subcommands are not grouped — they appear under cobra's default "Additional Commands:".
|
Command ungrouped passes WithGroup(nil) so the lifecycle subcommands are not grouped — they appear under cobra's default "Additional Commands:". |
|
with-args
command
Command with-args shows that flags and positional args are forwarded verbatim to the wrapped command.
|
Command with-args shows that flags and positional args are forwarded verbatim to the wrapped command. |