geck-go/daemon/wait.go

31 lines
626 B
Go
Raw Normal View History

2021-12-01 20:22:07 +01:00
package daemon
import (
2023-09-01 13:09:25 +02:00
"log/slog"
2021-12-01 20:22:07 +01:00
"os"
"os/signal"
"syscall"
)
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 21:05:40 +02:00
func WaitForSignal(sig ...os.Signal) os.Signal {
2021-12-01 20:22:07 +01:00
ch := make(chan os.Signal, 1)
signal.Notify(ch, sig...)
s := <-ch
2022-04-05 21:05:40 +02:00
2023-09-01 13:09:25 +02:00
slog.Debug("Signal caught", "signal", s.String())
2022-04-05 21:05:40 +02:00
return s
2021-12-01 20:22:07 +01:00
}
2022-04-05 21:05:40 +02:00
// WaitForSignalsDefault waits until one of SIGINT, SIGQUIT or SIGTERM is received.
func WaitForSignalsDefault() os.Signal {
return WaitForSignal(Interrupt, Quit, Terminate)
2021-12-01 20:22:07 +01:00
}