cleanup

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2019 License: CC0-1.0 Imports: 6 Imported by: 0

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
	...
}

This work is licensed under CC0 1.0 Universal (CC0 1.0), Public Domain Dedication. For details see:

  http://creativecommons.org/publicdomain/zero/1.0/
Online go-documentation is available at:
  https://godoc.org/bitbucket.org/pcas/tools/cleanup

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 log.Interface)

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 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