Skip to content

run functions on app exit (receiving os signal)

License

Notifications You must be signed in to change notification settings

wirekang/onexit

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

run functions on app exit (receiving os signal)

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%