This repository was archived by the owner on Sep 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathsignal.go
More file actions
116 lines (96 loc) · 2.57 KB
/
signal.go
File metadata and controls
116 lines (96 loc) · 2.57 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package signal
import (
"context"
"fmt"
"os"
"os/signal"
"sync"
"syscall"
)
// Handler is the default registered signal handler. It is created when this
// package is initialized.
var Handler = NewCancellable()
func init() {
signals := make(chan os.Signal, 1)
go Handler.SignalHandler(signals)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
}
// Cancellable is a cancellable process triggered via signal. It will cascade
// through the context's cancel functions destroying each build process as a
// result.
type Cancellable struct {
Exit bool
IgnoreRunners bool
mutex *sync.Mutex
files map[string]struct{}
cancelFuncs []context.CancelFunc
runners []chan struct{}
}
// NewCancellable creates a cancellable process
func NewCancellable() *Cancellable {
return &Cancellable{
Exit: true,
files: map[string]struct{}{},
mutex: new(sync.Mutex),
cancelFuncs: []context.CancelFunc{},
runners: make([]chan struct{}, 0),
}
}
// AddFile adds a temporary filename to be reaped if the action is canceled.
func (c *Cancellable) AddFile(filename string) {
c.mutex.Lock()
defer c.mutex.Unlock()
c.files[filename] = struct{}{}
}
// RemoveFile removes a file from the temporary file list.
func (c *Cancellable) RemoveFile(filename string) {
c.mutex.Lock()
defer c.mutex.Unlock()
delete(c.files, filename)
}
// AddFunc adds a cancel func to the list.
func (c *Cancellable) AddFunc(f context.CancelFunc) {
c.mutex.Lock()
defer c.mutex.Unlock()
c.cancelFuncs = append(c.cancelFuncs, f)
}
// AddRunner adds a chan struct{} to the list of runners. See BuildConfig for more.
func (c *Cancellable) AddRunner(run chan struct{}) {
c.mutex.Lock()
defer c.mutex.Unlock()
c.runners = append(c.runners, run)
}
// SignalHandler is the signal handler that will be used throughout box to
// cancel things.
func (c *Cancellable) SignalHandler(signals chan os.Signal) {
for {
<-signals
c.mutex.Lock()
funcs := c.cancelFuncs
c.cancelFuncs = []context.CancelFunc{}
files := c.files
c.files = map[string]struct{}{}
runners := c.runners
c.runners = make([]chan struct{}, 0)
c.mutex.Unlock()
fmt.Println("\n\n!!! SIGINT or SIGTERM received, crashing containers...")
for _, cancel := range funcs {
cancel()
}
if !c.IgnoreRunners {
for _, runner := range runners {
<-runner
}
}
for fn := range files {
fmt.Fprintf(os.Stderr, "Cleaning up temporary file %q", fn)
if err := os.Remove(fn); err != nil {
fmt.Fprintf(os.Stderr, ": %v", err)
}
fmt.Fprintln(os.Stderr)
}
if c.Exit {
os.Exit(1)
}
}
}