-
Notifications
You must be signed in to change notification settings - Fork 0
/
signal.go
71 lines (55 loc) · 1.14 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
Package signal manages signal handling.
*/
package signal
import (
"os"
"os/signal"
"syscall"
)
// Ignore sets all signals to be ignored
func Ignore() {
signal.Ignore()
}
// Hup sets f() to be run on receipt of SIGHUP
// SIGHUP is meant to be used to refresh program configuration
func Hup(f func()) {
sighup := make(chan os.Signal, 1)
signal.Notify(sighup, syscall.SIGHUP)
go func() {
for {
<-sighup
f()
}
}()
}
// Int sets f() to be run on receipt of SIGINT
// SIGINT is meant to be used to stop the program
func Int(f func()) {
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, syscall.SIGINT)
go func() {
<-sigint
f()
}()
}
// Quit sets f() to be run on receipt of SIGQUIT
// SIGQUIT is meant to be used to stop the program gracefully
func Quit(f func()) {
sigquit := make(chan os.Signal, 1)
signal.Notify(sigquit, syscall.SIGQUIT)
go func() {
<-sigquit
f()
}()
}
// Term sets f() to be run on receipt of SIGTERM
// SIGTERM is meant to be used to stop the program
func Term(f func()) {
sigterm := make(chan os.Signal, 1)
signal.Notify(sigterm, syscall.SIGTERM)
go func() {
<-sigterm
f()
}()
}