-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
worker_pool.go
131 lines (118 loc) · 2.43 KB
/
worker_pool.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
package main
import (
"bytes"
"context"
"fmt"
"html/template"
"io"
"os/exec"
"strings"
"sync"
"time"
)
type WorkerPool struct {
args []string
cmd string
concurrency int
ctx context.Context
err *writer
out *writer
queue queue
runner func(line string)
start time.Time
template *template.Template
}
// NewWorkerPool is designed to set up the worker pool for use.
func NewWorkerPool(
ctx context.Context,
stdout, stderr io.Writer,
reader io.Reader,
splitChar byte,
cmd, tmpl string,
args []string,
concurrency int,
) (*WorkerPool, error) {
var (
parsedTmpl *template.Template
path string
err error
)
if cmd != "" {
path, err = exec.LookPath(cmd)
if err != nil {
return nil, err
}
}
w := &WorkerPool{
args: args,
cmd: path,
concurrency: concurrency,
ctx: ctx,
err: newWriter(stderr),
out: newWriter(stdout),
queue: newQueue(reader, splitChar, concurrency),
start: time.Now(),
template: parsedTmpl,
}
if tmpl != "" {
parsedTmpl, err = template.New("cmd").Funcs(tmplFuncs).Parse(tmpl)
if err != nil {
return nil, err
}
w.template = parsedTmpl
w.runner = w.runCmdTemplate
} else {
w.runner = w.runCmd
}
return w, nil
}
func (w *WorkerPool) run() {
wg := sync.WaitGroup{}
for i := 0; i < w.concurrency; i++ {
wg.Add(1)
go w.startWorker(&wg)
}
wg.Wait()
w.out.Flush()
w.err.Flush()
}
func (w *WorkerPool) startWorker(wg *sync.WaitGroup) {
defer wg.Done()
for {
// Check if the user cancelled the run
select {
case <-w.ctx.Done():
return
case line, open := <-w.queue.ch:
if !open {
return
}
w.runner(line)
}
}
}
func (w *WorkerPool) runCmd(input string) {
args := append(w.args, input)
cmd := newCmd(w.out, w.err, w.cmd, args...)
if err := cmd.Run(); err != nil {
fmt.Fprintf(w.err, "Failed to run command: `%s %s` %s\n", w.cmd, strings.Join(args, " "), err)
}
}
func (w *WorkerPool) runCmdTemplate(input string) {
buf := bytes.Buffer{}
ctx := Ctx{
Cmd: w.cmd,
Input: input,
Start: w.start,
Time: time.Now(),
}
if err := w.template.Execute(&buf, ctx); err != nil {
fmt.Fprintf(w.err, "%s\n", err)
return
}
words := shellParser(buf.String())
cmd := newCmd(w.out, w.err, words[0], words[1:]...)
if err := cmd.Run(); err != nil {
fmt.Fprintf(w.err, "Failed to run command `%s`: %s\n", buf.String(), err)
}
}