Documentation
¶
Overview ¶
Package graceful provides mechanisms to gracefully stop processes on os signals.
Index ¶
- Variables
- func Background(opts ...BackgroundOption) context.Context
- func MaybeDebugf(log Logger, format string, args ...interface{})
- func MaybeErrorf(log Logger, format string, args ...interface{})
- func MaybeInfof(log Logger, format string, args ...interface{})
- func SignalNotify(signals ...os.Signal) chan os.Signal
- func SignalNotifyWithCapacity(capacity int, signals ...os.Signal) chan os.Signal
- func StartForShutdown(ctx context.Context, hosted ...Service) error
- type BackgroundOption
- type BackgroundOptions
- type Graceful
- type Logger
- type Service
Constants ¶
This section is empty.
Variables ¶
var DefaultRestartSignals = []os.Signal{ syscall.SIGHUP, }
DefaultRestartSignals are the default os signals to capture to restart.
DefaultShutdownSignals are the default os signals to capture to shut down.
Functions ¶
func Background ¶
func Background(opts ...BackgroundOption) context.Context
Background yields a context that will signal `<-ctx.Done()` when a signal is sent to the process (as specified in `DefaultShutdownSignals`).
This context will cancel only (1) time.
func MaybeDebugf ¶
MaybeDebugf calls the logger debugf method if the logger is set.
func MaybeErrorf ¶
MaybeErrorf calls the logger errorf method if the logger is set.
func MaybeInfof ¶
MaybeInfof calls the logger infof method if the logger is set.
func SignalNotify ¶
SignalNotify returns a channel that listens for a given set of os signals.
func SignalNotifyWithCapacity ¶
SignalNotifyWithCapacity returns a channel with a given capacity that listens for a given set of os signals.
func StartForShutdown ¶
StartForShutdown is a helper that sets up a graceful hosted process with default signals. You can pass a context to this function to explicitly control shutdown, in addition to setting up notification on process signals.
Types ¶
type BackgroundOption ¶
type BackgroundOption func(*BackgroundOptions)
BackgroundOption mutates background options
func OptBackgroundContext ¶
func OptBackgroundContext(ctx context.Context) BackgroundOption
OptBackgroundSkipStopOnSignal sets if we should stop the signal channel on stop.
func OptBackgroundLog ¶
func OptBackgroundLog(log Logger) BackgroundOption
OptBackgroundLog sets the logger.
func OptBackgroundSignals ¶
func OptBackgroundSignals(signals ...os.Signal) BackgroundOption
OptBackgroundSignals sets the signals.
type BackgroundOptions ¶
type BackgroundOptions struct { // Context is a root context, if unset `context.Background()` is used. Context context.Context // Signals are the specific os signals to intercept. Signals []os.Signal // Log holds an reference to a graceful logger. Log Logger }
BackgroundOptions are options for the background context.
type Graceful ¶
type Graceful struct { Hosted []Service ShutdownSignals []os.Signal RestartSignals []os.Signal Log Logger }
Graceful is the main entrypoint for hosting graceful processes.
func (Graceful) StartForShutdown ¶
StartForShutdown starts, and prepares to gracefully stop, a set hosted processes based on a provided context's cancellation.
The context is used to stop the goroutines this function spawns, as well as call `Stop(...)` on the hosted processes when it cancels.