forked from adnanh/webhook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signals.go
49 lines (44 loc) · 935 Bytes
/
signals.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//go:build !windows
package main
import (
"log"
"log/slog"
"os"
"os/signal"
"syscall"
)
func setupSignals(notifyReload func()) {
slog.Info("setting up os signal watcher")
signals := make(chan os.Signal, 1)
signal.Notify(
signals,
syscall.SIGUSR1,
syscall.SIGHUP,
syscall.SIGTERM,
os.Interrupt,
)
go func() {
slog.Info("os signal watcher ready")
for {
sig := <-signals
switch sig {
case syscall.SIGUSR1, syscall.SIGHUP:
slog.Warn("caught signal", "signal", sig)
notifyReload()
case os.Interrupt, syscall.SIGTERM:
log.Printf("caught %s signal; exiting\n", sig)
slog.Warn("caught signal", "signal", sig)
// todo: do proper shutdown, by notifying main loop, and remove this
if pidFile != nil {
err := pidFile.Remove()
if err != nil {
log.Print(err)
}
}
os.Exit(0)
default:
log.Printf("caught unhandled signal %+v\n", sig)
}
}
}()
}