-
Notifications
You must be signed in to change notification settings - Fork 15
/
worker.go
119 lines (92 loc) · 2 KB
/
worker.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
package main
import (
"bufio"
"context"
"io"
"os/exec"
"sync"
)
// Command collects all information of one invocation of a command.
type Command struct {
Cmd string
Args []string
ID int
Tag string
}
// Run executes the command.
func (c *Command) Run(ctx context.Context, outCh chan<- Status) error {
cmd := exec.Command(c.Cmd, c.Args...) //nolint:gosec
// make sure the new process and all children get a new process group ID
createProcessGroup(cmd)
// done is closed when the process has exited
done := make(chan struct{})
// wg tracks all goroutines started
var wg sync.WaitGroup
// start a goroutine which kills the process group when the context is cancelled
wg.Add(1)
go func() {
select {
case <-ctx.Done():
_ = killProcessGroup(cmd)
case <-done:
}
wg.Done()
}()
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return err
}
wg.Add(1)
go c.tagLines(&wg, false, stdout, outCh) //nolint:wsl
wg.Add(1)
go c.tagLines(&wg, true, stderr, outCh) //nolint:wsl
err = cmd.Run()
close(done)
wg.Wait()
return err
}
func (c *Command) tagLines(wg *sync.WaitGroup, isError bool, input io.Reader, out chan<- Status) {
defer wg.Done()
sc := bufio.NewScanner(input)
for sc.Scan() {
out <- Status{
Error: isError,
Tag: c.Tag,
ID: c.ID,
Message: sc.Text(),
}
}
}
func worker(wg *sync.WaitGroup, in <-chan *Command, outCh chan<- Status) {
defer wg.Done()
for cmd := range in {
outCh <- Status{
Tag: cmd.Tag,
ID: cmd.ID,
Start: true,
}
ctx := context.Background()
var cancel context.CancelFunc
if opts.workerTimeout > 0 {
ctx, cancel = context.WithTimeout(context.Background(), opts.workerTimeout)
}
err := cmd.Run(ctx, outCh)
finalStatus := Status{
Tag: cmd.Tag,
ID: cmd.ID,
Done: true,
}
if err != nil {
finalStatus.Error = true
finalStatus.Message = err.Error()
}
outCh <- finalStatus
if cancel != nil {
cancel()
}
}
}