cleanup

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2022 License: MIT Imports: 5 Imported by: 46

Documentation

Overview

Package cleanup provides a unified way to defer running functions until a later date.

Cleanup provides a way for tooling to defer running functions until a later date. A typical example would be wanting to defer running a shutdown function until main() exits:

func shutdown() {
	// shutdown function for some service that should really be run before the
	// program exits
	...
}

cleanup.Add(shutdown)

func main() {
	defer cleanup.Run() // Ensure that any cleanup functions are run on exit
	...
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add(f func() error)

Add adds the given function. This will panic if f is nil.

func AddRunable

func AddRunable(r Runable)

AddRunable adds the given Runable.

func CancelOnSignal

func CancelOnSignal(cancel context.CancelFunc, lg Printer)

CancelOnSignal installs a signal handler, calling the given cancel function on the first os.Interrupt. Subsequent os.Interrupt signals will cause immediate termination.

func Run

func Run() error

Run executes the Runables, in LIFO order. Runables will be removed as they are executed. It is safe for a Runable to add itself back to be executed at a later date. The first error encountered (if any) will be returned, however all Runables will be run.

Types

type Group

type Group struct {
	Name string // The group's name
	// contains filtered or unexported fields
}

Group is a collection of Runables that should be run together.

func (*Group) Add

func (g *Group) Add(f func() error)

Add adds the given function to the group. This will panic if g or f are nil.

func (*Group) AddRunable

func (g *Group) AddRunable(r Runable)

AddRunable adds the given Runable to the group. This will panic if g is nil.

func (*Group) Run

func (g *Group) Run() error

Run executes the Runables in the group, in LIFO order. Runables will be removed from the group as they are executed. It is safe for a Runable to add itself back to the group to be executed at a later date. The first error encountered (if any) will be returned, however all Runables will be run.

func (*Group) String

func (g *Group) String() string

String returns the group name.

type Printer

type Printer interface {
	Printf(format string, v ...interface{})
}

Printer is the interface satisfied by the Printf method.

type Runable

type Runable interface {
	Run() error
}

Runable is the interface satisfied by an object with a Run method.

Jump to

Keyboard shortcuts

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