-
Notifications
You must be signed in to change notification settings - Fork 136
/
reflex.go
346 lines (310 loc) · 8.19 KB
/
reflex.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package main
import (
"bufio"
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"os/signal"
"strings"
"sync"
"syscall"
"time"
"github.com/creack/pty"
)
// A Reflex is a single watch + command to execute.
type Reflex struct {
id int
source string // Describes what config/line defines this Reflex
startService bool
backlog Backlog
matcher Matcher
onlyFiles bool
onlyDirs bool
command []string
subSymbol string
done chan struct{}
mu *sync.Mutex // protects killed and running
killed bool
running bool
timeout time.Duration
// Used for services (startService = true)
cmd *exec.Cmd
tty *os.File
}
// NewReflex prepares a Reflex from a Config, with sanity checking.
func NewReflex(c *Config) (*Reflex, error) {
matcher, err := ParseMatchers(c.regexes, c.inverseRegexes, c.globs, c.inverseGlobs)
if err != nil {
return nil, fmt.Errorf("error parsing glob/regex: %s", err)
}
if !c.allFiles {
matcher = multiMatcher{defaultExcludeMatcher, matcher}
}
if len(c.command) == 0 {
return nil, errors.New("must give command to execute")
}
if c.subSymbol == "" {
return nil, errors.New("substitution symbol must be non-empty")
}
substitution := false
for _, part := range c.command {
if strings.Contains(part, c.subSymbol) {
substitution = true
break
}
}
var backlog Backlog
if substitution {
if c.startService {
return nil, errors.New("using --start-service does not work with a command that has a substitution symbol")
}
backlog = NewUniqueFilesBacklog()
} else {
backlog = NewUnifiedBacklog()
}
if c.onlyFiles && c.onlyDirs {
return nil, errors.New("cannot specify both --only-files and --only-dirs")
}
if c.shutdownTimeout <= 0 {
return nil, errors.New("shutdown timeout cannot be <= 0")
}
reflex := &Reflex{
id: reflexID,
source: c.source,
startService: c.startService,
backlog: backlog,
matcher: matcher,
onlyFiles: c.onlyFiles,
onlyDirs: c.onlyDirs,
command: c.command,
subSymbol: c.subSymbol,
done: make(chan struct{}),
timeout: c.shutdownTimeout,
mu: &sync.Mutex{},
}
reflexID++
return reflex, nil
}
func (r *Reflex) String() string {
var buf bytes.Buffer
fmt.Fprintln(&buf, "Reflex from", r.source)
fmt.Fprintln(&buf, "| ID:", r.id)
for _, matcherInfo := range strings.Split(r.matcher.String(), "\n") {
fmt.Fprintln(&buf, "|", matcherInfo)
}
if r.onlyFiles {
fmt.Fprintln(&buf, "| Only matching files.")
} else if r.onlyDirs {
fmt.Fprintln(&buf, "| Only matching directories.")
}
if !r.startService {
fmt.Fprintln(&buf, "| Substitution symbol", r.subSymbol)
}
replacer := strings.NewReplacer(r.subSymbol, "<filename>")
command := make([]string, len(r.command))
for i, part := range r.command {
command[i] = replacer.Replace(part)
}
fmt.Fprintln(&buf, "| Command:", command)
fmt.Fprintln(&buf, "+---------")
return buf.String()
}
// filterMatching passes on messages matching the regex/glob.
func (r *Reflex) filterMatching(out chan<- string, in <-chan string) {
for name := range in {
if !r.matcher.Match(name) {
continue
}
if r.onlyFiles || r.onlyDirs {
stat, err := os.Stat(name)
if err != nil {
continue
}
if (r.onlyFiles && stat.IsDir()) || (r.onlyDirs && !stat.IsDir()) {
continue
}
}
out <- name
}
}
// batch receives file notification events and batches them up. It's a bit
// tricky, but here's what it accomplishes:
// * When we initially get a message, wait a bit and batch messages before
// trying to send anything. This is because the file events come in bursts.
// * Once it's time to send, don't do it until the out channel is unblocked.
// In the meantime, keep batching. When we've sent off all the batched
// messages, go back to the beginning.
func (r *Reflex) batch(out chan<- string, in <-chan string) {
const silenceInterval = 300 * time.Millisecond
for name := range in {
r.backlog.Add(name)
timer := time.NewTimer(silenceInterval)
outer:
for {
select {
case name := <-in:
r.backlog.Add(name)
if !timer.Stop() {
<-timer.C
}
timer.Reset(silenceInterval)
case <-timer.C:
for {
select {
case name := <-in:
r.backlog.Add(name)
case out <- r.backlog.Next():
if r.backlog.RemoveOne() {
break outer
}
}
}
}
}
}
}
// runEach runs the command on each name that comes through the names channel.
// Each {} is replaced by the name of the file. The output of the command is
// passed line-by-line to the stdout chan.
func (r *Reflex) runEach(names <-chan string) {
for name := range names {
if r.startService {
if r.Running() {
infoPrintln(r.id, "Killing service")
r.terminate()
}
infoPrintln(r.id, "Starting service")
r.runCommand(name, stdout)
} else {
r.runCommand(name, stdout)
<-r.done
r.mu.Lock()
r.running = false
r.mu.Unlock()
}
}
}
func (r *Reflex) terminate() {
r.mu.Lock()
r.killed = true
r.mu.Unlock()
// Write ascii 3 (what you get from ^C) to the controlling pty.
// (This won't do anything if the process already died as the write will
// simply fail.)
r.tty.Write([]byte{3})
timer := time.NewTimer(r.timeout)
sig := syscall.SIGINT
for {
select {
case <-r.done:
return
case <-timer.C:
if sig == syscall.SIGINT {
infoPrintln(r.id, "Sending SIGINT signal...")
} else {
infoPrintln(r.id, "Sending SIGKILL signal...")
}
// Instead of killing the process, we want to kill its
// whole pgroup in order to clean up any children the
// process may have created.
if err := syscall.Kill(-1*r.cmd.Process.Pid, sig); err != nil {
infoPrintln(r.id, "Error killing:", err)
if err.(syscall.Errno) == syscall.ESRCH { // no such process
return
}
}
// After SIGINT doesn't do anything, try SIGKILL next.
timer.Reset(r.timeout)
sig = syscall.SIGKILL
}
}
}
func replaceSubSymbol(command []string, subSymbol string, name string) []string {
replacer := strings.NewReplacer(subSymbol, name)
newCommand := make([]string, len(command))
for i, c := range command {
newCommand[i] = replacer.Replace(c)
}
return newCommand
}
var seqCommands = &sync.Mutex{}
// runCommand runs the given Command. All output is passed line-by-line to the
// stdout channel.
func (r *Reflex) runCommand(name string, stdout chan<- OutMsg) {
command := replaceSubSymbol(r.command, r.subSymbol, name)
cmd := exec.Command(command[0], command[1:]...)
r.cmd = cmd
if flagSequential {
seqCommands.Lock()
}
tty, err := pty.Start(cmd)
if err != nil {
infoPrintln(r.id, err)
return
}
r.tty = tty
// Handle pty size.
chResize := make(chan os.Signal, 1)
signal.Notify(chResize, syscall.SIGWINCH)
go func() {
for range chResize {
// Intentionally ignore errors in case stdout is not a tty
pty.InheritSize(os.Stdout, tty)
}
}()
chResize <- syscall.SIGWINCH // Initial resize.
go func() {
scanner := bufio.NewScanner(tty)
// Allow for lines up to 100 MB.
scanner.Buffer(nil, 100e6)
for scanner.Scan() {
stdout <- OutMsg{r.id, scanner.Text()}
}
if err := scanner.Err(); errors.Is(err, bufio.ErrTooLong) {
infoPrintln(r.id, "Error: subprocess emitted a line longer than 100 MB")
}
// Intentionally ignore other scanner errors. Unfortunately,
// the pty returns a read error when the child dies naturally,
// so I'm just going to ignore errors here unless I can find a
// better way to handle it.
}()
r.mu.Lock()
r.running = true
r.mu.Unlock()
go func() {
err := cmd.Wait()
if !r.Killed() && err != nil {
stdout <- OutMsg{r.id, fmt.Sprintf("(error exit: %s)", err)}
}
r.done <- struct{}{}
signal.Stop(chResize)
close(chResize)
if flagSequential {
seqCommands.Unlock()
}
}()
}
func (r *Reflex) Start(changes <-chan string) {
filtered := make(chan string)
batched := make(chan string)
go r.filterMatching(filtered, changes)
go r.batch(batched, filtered)
go r.runEach(batched)
if r.startService {
// Easy hack to kick off the initial start.
infoPrintln(r.id, "Starting service")
r.runCommand("", stdout)
}
}
func (r *Reflex) Killed() bool {
r.mu.Lock()
defer r.mu.Unlock()
return r.killed
}
func (r *Reflex) Running() bool {
r.mu.Lock()
defer r.mu.Unlock()
return r.running
}