-
Notifications
You must be signed in to change notification settings - Fork 6
/
signal.go
47 lines (43 loc) · 968 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main
import (
"net"
"os"
"os/signal"
"syscall"
"time"
)
const (
// todo get value from gokeeper
GraceStopTimeout = time.Second * 5
)
func signalHandler(llist ...net.Listener) {
ch := make(chan os.Signal, 10)
signal.Notify(ch, syscall.SIGINT, syscall.SIGUSR2)
for {
sig := <-ch
switch sig {
case syscall.SIGINT, syscall.SIGTERM:
signal.Stop(ch)
flog.Error("receive signal TERM/INT stop the process")
for _, lis := range llist {
lis.Close()
}
GlobalCancelFunc()
<-time.After(GraceStopTimeout)
flog.Error("signal handler done now end the main process")
return
case syscall.SIGUSR2:
flog.Error("receive signal USR2 restart process")
count, err := GlobalNet.StartProcess()
if err != nil {
flog.Error("restart process fail", err)
return
}
for _, lis := range llist {
lis.Close()
}
GlobalCancelFunc()
flog.Error("signal handler done now end the main process", count, err)
}
}
}