-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstop.go
77 lines (56 loc) · 977 Bytes
/
stop.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package stop
import (
"os"
"os/signal"
"sync"
"syscall"
)
var state struct {
sync.Mutex
interrupted bool
killed bool
terminated bool
}
func Listen() {
state = struct {
sync.Mutex
interrupted bool
killed bool
terminated bool
}{}
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGINT, syscall.SIGKILL, syscall.SIGTERM)
go func() {
s := <-c
state.Lock()
defer state.Unlock()
switch s {
case syscall.SIGINT:
state.interrupted = true
case syscall.SIGKILL:
state.killed = true
case syscall.SIGTERM:
state.terminated = true
}
}()
}
func Interrupted() bool {
state.Lock()
defer state.Unlock()
return state.interrupted
}
func Killed() bool {
state.Lock()
defer state.Unlock()
return state.killed
}
func Terminated() bool {
state.Lock()
defer state.Unlock()
return state.terminated
}
func Stopped() bool {
state.Lock()
defer state.Unlock()
return state.interrupted || state.terminated || state.killed
}