onexit

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2018 License: MIT Imports: 5 Imported by: 0

README

GoDoc

onexit

Package helps with running functions on app exit (receiving an OS signal), based on their priority.

Functions will be registtered with a priority and be called based on that priority - a priority queue is implemented by employing a heap.

The last statement of main() can be <-onexit.Done() which waits for all registered functions to run, before exit.

package main

import (
	"github.com/dc0d/onexit"
)

func main() {
	onexit.Register(Logout, 100)
	onexit.Register(func() { println("\n") })

	// ...

	<-onexit.Done()
}

func init() {
	onexit.Register(SyncLogger, -100)
}

Calling os.Exit(code) explicitly, will not trigger onexit to run registered functions. Because it causes the program to exit without waiting for anything. Instead, call onexit.ForceExit(code) which waits for all registered functions to execute and then calls os.Exit(code).

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Done

func Done() <-chan struct{}

Done all registered deferred funcs are executed on signal. Call <-Done() at the end of main function (for example).

func ForceExit

func ForceExit(code int)

ForceExit ensures all registered deferred funcs are executed, and waits for completion of all of them, then calls os.Exit(code) with the provided code.

func Register

func Register(action func(), priority ...int)

Register a function to be executed on app exit (by receiving an os signal), based on priority.

Types

This section is empty.

Directories

Path Synopsis
Package fnpq contains type-specific version of same "generic" types, cpoied from standard library.
Package fnpq contains type-specific version of same "generic" types, cpoied from standard library.

Jump to

Keyboard shortcuts

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