-
Notifications
You must be signed in to change notification settings - Fork 0
/
signals.go
49 lines (41 loc) · 1007 Bytes
/
signals.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
package goutils
import (
"log"
"os"
"os/signal"
)
func CleanupOnInterrupt(jobs ...func()) chan bool {
var (
signalChan = make(chan os.Signal, 1)
doneChan = make(chan bool)
)
signal.Notify(signalChan, os.Interrupt)
go func() {
for _ = range signalChan {
println("")
log.Println("===== Received an interrupt, handling cleanup services. =====")
for _, job := range jobs {
job()
}
log.Println("===== All jobs were finished, existing the program. =====")
doneChan <- true
}
}()
println("Program started, waiting interrupt signal.")
return doneChan
}
func CleanupAndExitOnInterrupt(jobs ...func()) {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt)
go func() {
for _ = range signalChan {
println("")
log.Println("===== Received an interrupt, handling cleanup services. =====")
for _, job := range jobs {
job()
}
log.Println("===== All jobs were finished, existing the program. =====")
os.Exit(0)
}
}()
}