Created
November 30, 2014 01:59
-
-
Save ae6rt/a5bbe626f5334f976bd8 to your computer and use it in GitHub Desktop.
A shutdown-hook scaffold for Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package lifecycle | |
| import ( | |
| "log" | |
| "os" | |
| "os/signal" | |
| ) | |
| var targets []Shutdowner | |
| type Shutdowner interface { | |
| Shutdown() | |
| } | |
| func init() { | |
| targets = make([]Shutdowner, 0) | |
| go func() { | |
| c := make(chan os.Signal, 1) | |
| signal.Notify(c, os.Interrupt, os.Kill) | |
| <-c | |
| log.Println("Shutdown...") | |
| for _, sd := range targets { | |
| sd.Shutdown() | |
| } | |
| os.Exit(0) | |
| }() | |
| } | |
| func AddShutdownHook(sd Shutdowner) { | |
| targets = append(targets, sd) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment