death

package module
v3.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2018 License: MIT Imports: 8 Imported by: 0

README

Death Build Status Coverage Status

Simple library to make it easier to manage the death of your application.

Get The Library

Use gopkg.in to import death based on your logger.

Version Go Get URL source doc Notes
3.x gopkg.in/vrecan/death.v3 source doc This removes the need for an independent logger. By default death will not log but will return an error if all the closers do not properly close. If you want to provide a logger just satisfy the deathlog.Logger interface.
2.x gopkg.in/vrecan/death.v2 source doc This supports loggers who do not return an error from their Error and Warn functions like logrus
1.x gopkg.in/vrecan/death.v1 souce doc This supports loggers who do return an error from their Error and Warn functions like seelog

Example

go get gopkg.in/vrecan/death.v3

Use The Library

package main

import (
	DEATH "github.com/vrecan/death"
	SYS "syscall"
)

func main() {
	death := DEATH.NewDeath(SYS.SIGINT, SYS.SIGTERM) //pass the signals you want to end your application
	//when you want to block for shutdown signals
	death.WaitForDeath() // this will finish when a signal of your type is sent to your application
}
Close Other Objects On Shutdown

One simple feature of death is that it can also close other objects when shutdown starts

package main

import (
	"log"
	DEATH "github.com/vrecan/death"
	SYS "syscall"
	"io"
)

func main() {
	death := DEATH.NewDeath(SYS.SIGINT, SYS.SIGTERM) //pass the signals you want to end your application
	objects := make([]io.Closer, 0)

	objects = append(objects, &NewType{}) // this will work as long as the type implements a Close method

	//when you want to block for shutdown signals
	err := death.WaitForDeath(objects...) // this will finish when a signal of your type is sent to your application
	if err != nil {
		log.Println(err)
		os.Exit(1)
	}
}

type NewType struct {
}

func (c *NewType) Close() error {
	return nil
}
Or close using an anonymous function
package main

import (
	DEATH "github.com/vrecan/death"
	SYS "syscall"
)

func main() {
	death := DEATH.NewDeath(SYS.SIGINT, SYS.SIGTERM) //pass the signals you want to end your application
	//when you want to block for shutdown signals
	death.WaitForDeathWithFunc(func(){ 
		//do whatever you want on death
	}) 
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Death

type Death struct {
	// contains filtered or unexported fields
}

Death manages the death of your application.

func NewDeath

func NewDeath(signals ...os.Signal) (death *Death)

NewDeath Create Death with the signals you want to die from.

func (*Death) FallOnSword

func (d *Death) FallOnSword()

FallOnSword manually initiates the death process.

func (*Death) SetLogger

func (d *Death) SetLogger(l Logger) *Death

SetLogger Overrides the default logger (seelog)

func (*Death) SetTimeout

func (d *Death) SetTimeout(t time.Duration) *Death

SetTimeout Overrides the time death is willing to wait for a objects to be closed.

func (*Death) WaitForDeath

func (d *Death) WaitForDeath(closable ...io.Closer) (err error)

WaitForDeath wait for signal and then kill all items that need to die. If they fail to die when instructed we return an error

func (*Death) WaitForDeathWithFunc

func (d *Death) WaitForDeathWithFunc(f func())

WaitForDeathWithFunc allows you to have a single function get called when it's time to kill your application.

type Logger

type Logger interface {
	Error(v ...interface{})
	Debug(v ...interface{})
	Info(v ...interface{})
}

Logger interface to log.

func DefaultLogger

func DefaultLogger() Logger

DefaultLogger returns a logger that does nothing

Jump to

Keyboard shortcuts

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