-
Notifications
You must be signed in to change notification settings - Fork 249
/
signals.go
50 lines (44 loc) · 966 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
50
//+build mage
package main
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"time"
)
// Exits after receiving SIGHUP
func ExitsAfterSighup(ctx context.Context) {
sigC := make(chan os.Signal, 1)
signal.Notify(sigC, syscall.SIGHUP)
<-sigC
fmt.Println("received sighup")
}
// Exits after SIGINT and wait
func ExitsAfterSigint(ctx context.Context) {
sigC := make(chan os.Signal, 1)
signal.Notify(sigC, syscall.SIGINT)
<-sigC
fmt.Printf("exiting...")
time.Sleep(200 * time.Millisecond)
fmt.Println("done")
}
// Exits after ctx cancel and wait
func ExitsAfterCancel(ctx context.Context) {
defer func() {
fmt.Println("deferred cleanup")
}()
<-ctx.Done()
fmt.Printf("exiting...")
time.Sleep(200 * time.Millisecond)
fmt.Println("done")
}
// Ignores all signals, requires killing via timeout or second SIGINT
func IgnoresSignals(ctx context.Context) {
sigC := make(chan os.Signal, 1)
signal.Notify(sigC, syscall.SIGINT)
for {
<-sigC
}
}