-
Notifications
You must be signed in to change notification settings - Fork 168
/
signal.go
32 lines (27 loc) · 946 Bytes
/
signal.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
package signals
import (
"context"
"os"
"os/signal"
"syscall"
log "github.com/sirupsen/logrus"
)
// Used to ensure GracefulShutdown is called at most once. When GracefulShutdown is called,
// this channel would be closed, if another call to close it again, panic would occur.
var once = make(chan struct{})
// GracefulShutdown catches signals of Interrupt, SIGINT, SIGTERM, SIGQUIT and cancel a context.
// If any signals caught, it will call the CancelFunc to cancel a context. If a second signal
// caught, exit directly with code 1.
func GracefulShutdown(cancel context.CancelFunc) {
close(once)
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
go func() {
s := <-c
log.WithField("signal", s).Debug("System signal caught, cancel context.")
cancel()
s = <-c
log.WithField("signal", s).Debug("Another system signal caught, exit directly.")
os.Exit(1)
}()
}