graceful

package
v1.20210819.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 19, 2021 License: MIT Imports: 6 Imported by: 8

Documentation

Overview

Package graceful provides a mechanism to gracefully stop processes on os signals.

Index

Constants

This section is empty.

Variables

View Source
var DefaultShutdownSignals = []os.Signal{
	os.Interrupt, syscall.SIGTERM,
}

DefaultShutdownSignals are the default os signals to capture to shut down.

Functions

func Background added in v1.20210306.2

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`).

func MaybeDebugf added in v1.20210316.2

func MaybeDebugf(log Logger, format string, args ...interface{})

MaybeDebugf calls the logger debugf method if the logger is set.

func MaybeErrorf added in v1.20210316.2

func MaybeErrorf(log Logger, format string, args ...interface{})

MaybeErrorf calls the logger errorf method if the logger is set.

func MaybeInfof added in v1.20210316.2

func MaybeInfof(log Logger, format string, args ...interface{})

MaybeInfof calls the logger infof method if the logger is set.

func Notify added in v1.20201204.1

func Notify(signals ...os.Signal) chan os.Signal

Notify returns a channel that listens for a given set of os signals.

func NotifyWithCapacity added in v1.20210316.2

func NotifyWithCapacity(capacity int, signals ...os.Signal) chan os.Signal

NotifyWithCapacity returns a channel with a given capacity that listens for a given set of os signals.

func Shutdown

func Shutdown(hosted ...Graceful) error

Shutdown racefully stops a set hosted processes based on SIGINT or SIGTERM received from the os. It will return any errors returned by Start() that are not caused by shutting down the server. A "Graceful" processes *must* block on start.

func ShutdownByContext added in v1.20210701.2

func ShutdownByContext(ctx context.Context, hosted ...Graceful) error

ShutdownByContext gracefully stops a set hosted processes based on context cancellation.

func ShutdownBySignal

func ShutdownBySignal(hosted []Graceful, opts ...ShutdownOption) error

ShutdownBySignal gracefully stops a set hosted processes based on a set of variadic options. A "Graceful" processes *must* block on start. Fatal errors will be returned, that is, errors that are returned by either .Start() or .Stop(). Panics are not caught by graceful, and it is assumed that your .Start() or .Stop methods will catch relevant panics.

Types

type BackgroundOption added in v1.20210316.2

type BackgroundOption func(*BackgroundOptions)

BackgroundOption mutates background options

func OptBackgroundLog added in v1.20210316.2

func OptBackgroundLog(log Logger) BackgroundOption

OptBackgroundLog sets the logger.

func OptBackgroundSignals added in v1.20210316.2

func OptBackgroundSignals(signals ...os.Signal) BackgroundOption

OptBackgroundSignals sets the signals.

func OptBackgroundSkipStopOnSignal added in v1.20210316.2

func OptBackgroundSkipStopOnSignal(skipStopOnSignal bool) BackgroundOption

OptBackgroundSkipStopOnSignal sets if we should stop the signal channel on stop.

type BackgroundOptions added in v1.20210316.2

type BackgroundOptions struct {
	Signals          []os.Signal
	Log              Logger
	SkipStopOnSignal bool
}

BackgroundOptions are options for the background context.

type Graceful

type Graceful interface {
	Start() error // this call must block
	Stop() error
}

Graceful is a server that can start and stop.

type Logger added in v1.20210316.2

type Logger interface {
	Infof(format string, args ...interface{})
	Debugf(format string, args ...interface{})
	Errorf(format string, args ...interface{})
}

Logger is a type that can be used as a graceful process logger.

type ShutdownOption added in v1.20201204.1

type ShutdownOption func(*ShutdownOptions)

ShutdownOption is a mutator for shutdown options.

func OptDefaultShutdownSignal added in v1.20201204.1

func OptDefaultShutdownSignal() ShutdownOption

OptDefaultShutdownSignal returns an option that sets the shutdown signal to the defaults.

func OptShutdownSignal added in v1.20201204.1

func OptShutdownSignal(signal chan os.Signal) ShutdownOption

OptShutdownSignal sets the shutdown signal.

type ShutdownOptions added in v1.20201204.1

type ShutdownOptions struct {
	ShutdownSignal chan os.Signal
}

ShutdownOptions are the options for graceful shutdown.

Jump to

Keyboard shortcuts

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