8000 GitHub - ifnotnil/daemon: Go tiny daemon/service wrapper
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

ifnotnil/daemon

Repository files navigation

daemon

CI Status codecov Go Report Card PkgGoDev

Install:

go get -u github.com/ifnotnil/daemon

The daemon package encapsulates the core functionality required for running an application as a daemon or service, and it ensures a graceful shutdown when stop conditions are met. Stop conditions:

  1. A signal (one of daemonConfig.signalsNotify) is received from OS.
  2. An error is received in fatal errors channel.
  3. the given parent context (parentCTX) in Start function is done.

The shutdown can be initiated manually at any point by calling ShutDown() daemon's receiver function.

Example:

func main() {
	d := daemon.Start(
		context.Background(),
		daemon.WithSignalsNotify(os.Interrupt, syscall.SIGQUIT, syscall.SIGABRT, syscall.SIGTERM),
		daemon.WithShutdownGraceDuration(5*time.Second),
	)

	ctx := d.CTX() // This ctx should be provided to the rest of the code

	// example modules
	db := InitRepo(ctx)
	serviceA := InitServiceA(ctx, db) // starts its own go routines / jobs
	httpServer := NewHTTPModule(ctx, serviceA) // starts its own go routine
	consumers := InitQueueConsumer(ctx) // starts its own go routine

	d.OnShutDown(
		httpServer.ShutDown,
		consumers.Stop,
		serviceA.Stop,
		db.Stop,
	)

	d.Wait() // this will block ti the graceful shutdown is initiated and done.
}

Context

The context provided by the daemon struct .CTX() should be passed downstream to the rest of the code.

It will get cancelled by default after the shutdown callbacks are done or if it configured as a shutdown callback by passing daemon.CancelCTX in the OnShutDown() function. For example:

76C2
	d.OnShutDown(
		httpServer.ShutDown,
		consumers.Stop,
		serviceA.Stop,
		daemon.CancelCTX  // the ctx will be cancelled before the db disconnects.
		db.Stop,
	)

Shutdown callbacks

Using the daemon function OnShutDown(f ...func(context.Context)) you can register callback functions that will be called once the graceful shutdown is initiated.

The context that is given to each shutdown callback is not the same with .CTX(). It will be the parentCTX with a separate timeout (shutdown grace period) depending on the configuration.

Using the WithShutdownGraceDuration option you can set the grace period of shutdown, after which the ctx given to each shutdown callback will be cancelled. By setting to 0, infinite grace period is set.

Fatal errors channel

Daemon provides an error channel FatalErrorsChannel() chan<- error that can be used downstream to push errors that are considered catastrophic into it. Once an error received in this channel the daemon struct will initiate the graceful shutdown process.

About

Go tiny daemon/service wrapper

Topics

Resources

License

Stars

Watchers

Forks

Contributors 2

  •  
  •  
0