geck-go/daemon/wait.go

34 lines
642 B
Go
Raw Normal View History

2021-12-01 19:22:07 +00:00
package daemon
import (
"os"
"os/signal"
"syscall"
"github.com/rs/zerolog/log"
)
var (
Interrupt = os.Interrupt
Quit os.Signal = syscall.SIGQUIT
Terminate os.Signal = syscall.SIGTERM
)
// WaitForSignal waits until one of the specified signals are received.
2022-04-05 19:05:40 +00:00
func WaitForSignal(sig ...os.Signal) os.Signal {
2021-12-01 19:22:07 +00:00
ch := make(chan os.Signal, 1)
signal.Notify(ch, sig...)
s := <-ch
2022-04-05 19:05:40 +00:00
2021-12-01 19:22:07 +00:00
log.Debug().
Str("signal", s.String()).
Msg("Signal caught")
2022-04-05 19:05:40 +00:00
return s
2021-12-01 19:22:07 +00:00
}
2022-04-05 19:05:40 +00:00
// WaitForSignalsDefault waits until one of SIGINT, SIGQUIT or SIGTERM is received.
func WaitForSignalsDefault() {
2021-12-01 19:22:07 +00:00
WaitForSignal(Interrupt, Quit, Terminate)
}