You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
var shutdownHandlers []func() error
|
|
|
|
var shutdownOnce sync.Once
|
|
|
|
|
|
|
|
func shutdown(cause error) {
|
|
|
|
shutdownOnce.Do(func() {
|
|
|
|
status := 0
|
|
|
|
if cause != nil {
|
|
|
|
status = 1
|
|
|
|
log_error.Printf("shutting down due to error: %v", cause)
|
|
|
|
} else {
|
|
|
|
log_info.Print("shutting down")
|
|
|
|
}
|
|
|
|
if len(shutdownHandlers) > 0 {
|
|
|
|
for i := len(shutdownHandlers) - 1; i >= 0; i-- {
|
|
|
|
f := shutdownHandlers[i]
|
|
|
|
if err := f(); err != nil {
|
|
|
|
log_error.Printf("error in shutdown: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
os.Exit(status)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func onShutdown(f func() error) {
|
|
|
|
shutdownHandlers = append(shutdownHandlers, f)
|
|
|
|
}
|