-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
graceful.go
38 lines (32 loc) · 941 Bytes
/
graceful.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
/*
Package shutdown provides simple shutdown signals handler with callback handler
*/
package shutdown
import (
"log"
"os"
"os/signal"
"syscall"
)
// GracefulStop handles signal and graceful shutdown by executing callback function
// when signal is received callback is called followed after by os.Exit(0), it is responsibility of callback to handle timeout
// if second signal is received will terminate process by a call to os.Exit(1)
func GracefulStop(stop func()) {
signalChan := make(chan os.Signal, 1)
signal.Notify(
signalChan,
syscall.SIGHUP, // kill -SIGHUP XXXX
syscall.SIGINT, // kill -SIGINT XXXX or Ctrl+c
syscall.SIGTERM, // kill -SIGTERM XXXX
syscall.SIGQUIT, // kill -SIGQUIT XXXX
)
<-signalChan
log.Print("os.Interrupt - shutting down...\n")
// terminate after second signal before callback is done
go func() {
<-signalChan
log.Fatal("os.Kill - terminating...\n")
}()
stop()
os.Exit(0)
}