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.
mir/shutdown.go

31 lines
539 B
Go

package main
import (
"sync"
)
var shutdownHandlers []func()
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")
}
for _, f := range shutdownHandlers {
if err := f(); err != nil {
log_error.Printf("error in shutdown: %v", err)
}
}
os.Exit(status)
})
}
func onShutdown(f func()) {
shutdownHandlers = append(shutdownHandlers, f)
}