-
Notifications
You must be signed in to change notification settings - Fork 1
/
sig_int.go
50 lines (36 loc) · 863 Bytes
/
sig_int.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
package meta
import (
"context"
"os"
"os/signal"
"syscall"
"github.com/simiancreative/simiango/logger"
)
var cleaners []func()
var done context.Context
var exit context.Context
var initialized bool
func AddCleanup(cleaner func()) {
cleaners = append(cleaners, cleaner)
}
func CatchSig() (context.Context, context.Context) {
if initialized {
return done, exit
}
initialized = true
done, doneCancel := context.WithCancel(context.Background())
exit, exitCancel := context.WithCancel(context.Background())
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
go func() {
sig := <-sigs
logger.Printf("Meta: begin shutdown (%+v)", sig)
doneCancel()
for _, cleaner := range cleaners {
cleaner()
}
logger.Printf("Meta: shutdown complete")
exitCancel()
}()
return done, exit
}