2019-11-18 11:41:37 -08:00
|
|
|
package signal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
)
|
|
|
|
|
2022-05-17 08:19:03 +02:00
|
|
|
var (
|
|
|
|
onlyOneSignalHandler = make(chan struct{})
|
|
|
|
shutdownHandler chan os.Signal
|
|
|
|
)
|
2019-11-18 11:41:37 -08:00
|
|
|
|
|
|
|
// SetupSignalHandler registered for SIGTERM and SIGINT. A stop channel is returned
|
|
|
|
// which is closed on one of these signals. If a second signal is caught, the program
|
|
|
|
// is terminated with exit code 1.
|
|
|
|
func SetupSignalHandler() <-chan struct{} {
|
|
|
|
close(onlyOneSignalHandler) // panics when called twice
|
|
|
|
|
|
|
|
shutdownHandler = make(chan os.Signal, 2)
|
|
|
|
|
|
|
|
stop := make(chan struct{})
|
|
|
|
signal.Notify(shutdownHandler, shutdownSignals...)
|
|
|
|
go func() {
|
|
|
|
<-shutdownHandler
|
|
|
|
close(stop)
|
|
|
|
<-shutdownHandler
|
|
|
|
os.Exit(1) // second signal. Exit directly.
|
|
|
|
}()
|
|
|
|
|
|
|
|
return stop
|
|
|
|
}
|
|
|
|
|
|
|
|
// RequestShutdown emulates a received event that is considered as shutdown signal (SIGTERM/SIGINT)
|
|
|
|
// This returns whether a handler was notified
|
|
|
|
func RequestShutdown() bool {
|
|
|
|
if shutdownHandler != nil {
|
|
|
|
select {
|
|
|
|
case shutdownHandler <- shutdownSignals[0]:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|