-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmain.go
408 lines (312 loc) · 7.88 KB
/
main.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package main
import (
"bufio"
"context"
"fmt"
"io"
"os"
"runtime"
"sort"
"strings"
"sync"
"time"
"github.com/fatih/color"
"github.com/fd0/termstatus"
"github.com/spf13/pflag"
)
var opts = struct {
threads int
placeholder string
workerTimeout time.Duration
useNullSeparator bool
hideJobID bool
hideTimestamp bool
hideName bool
}{}
// ScanNullSeparatedValues splits data by null bytes.
func ScanNullSeparatedValues(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
for i, b := range data {
if b == 0 {
return i + 1, data[:i], nil
}
}
if atEOF {
return len(data), data, nil
}
return 0, nil, nil
}
func parseInput(ch chan<- *Command, jobNumCh chan<- int, cmd string, args []string) {
defer close(ch)
sc := bufio.NewScanner(os.Stdin)
if opts.useNullSeparator {
sc.Split(ScanNullSeparatedValues)
}
jobnum := 0
defer func() {
jobNumCh <- jobnum
close(jobNumCh)
}()
for sc.Scan() {
jobnum++
cmdName := cmd
cmdArgs := make([]string, 0, len(args))
line := strings.TrimSpace(sc.Text())
if line == "" {
fmt.Fprintf(os.Stderr, "ignoring empty item\n")
continue
}
cmdName = strings.ReplaceAll(cmdName, opts.placeholder, line)
for _, arg := range args {
cmdArgs = append(cmdArgs, strings.ReplaceAll(arg, opts.placeholder, line))
}
ch <- &Command{
ID: jobnum,
Tag: line,
Cmd: cmdName,
Args: cmdArgs,
}
if jobnum%10 == 0 {
jobNumCh <- jobnum
}
}
}
func checkForPlaceholder(cmdname string, args []string) {
if cmdname == opts.placeholder {
return
}
for _, arg := range args {
if strings.Contains(arg, opts.placeholder) {
return
}
}
fmt.Fprintf(os.Stderr, "no placeholder found\n")
os.Exit(2) //nolint:gomnd
}
// Status is one message printed by a command.
type Status struct {
ID int
Tag string
Message string
Error bool
Done bool
Start bool
}
//nolint:gomnd
func formatDuration(d time.Duration) string {
sec := uint64(d / time.Second)
hours := sec / 3600
sec -= hours * 3600
min := sec / 60
sec -= min * 60
if hours > 0 {
return fmt.Sprintf("%d:%02d:%02d", hours, min, sec)
}
return fmt.Sprintf("%d:%02d", min, sec)
}
var (
colorNumber = color.New(color.Reset, color.FgGreen).SprintFunc()
colorTimestamp = color.New(color.Reset, color.FgBlue).SprintFunc()
colorTag = color.New(color.Reset, color.FgYellow).SprintFunc()
colorError = color.New(color.Reset, color.FgRed, color.Bold).SprintFunc()
colorStatusLine = color.New(color.Bold, color.ReverseVideo).SprintFunc()
)
var (
lastLineCount = 0
lastLineCountReduction time.Time
smoothLines = 0
etaEWMA *ewma
lastETA time.Duration
lastETAUpdate time.Time
)
func updateTerminal(t *termstatus.Terminal, stats Stats, data map[string]string) {
keys := make([]string, 0, len(data))
for k := range data {
keys = append(keys, k)
}
sort.Strings(keys)
var status string
if stats.jobsFinal {
if etaEWMA == nil {
etaEWMA = newEWMA(stats.start, stats.jobs)
}
etaEWMA.Report(stats.processed)
if time.Since(lastETAUpdate) > time.Second {
lastETA = etaEWMA.ETA()
lastETAUpdate = time.Now()
}
eta := "---"
if lastETA > 0 {
eta = formatDuration(lastETA)
}
status = fmt.Sprintf("[%s] %d/%d processed (%d failed) ETA %v, %d/%d workers:",
formatDuration(time.Since(stats.start)),
stats.processed,
stats.jobs,
stats.failed,
eta,
len(data),
opts.threads)
} else {
status = fmt.Sprintf("[%s] %d/%d+ processed (%d failed), %d/%d workers:",
formatDuration(time.Since(stats.start)),
stats.processed,
stats.jobs,
stats.failed,
len(data),
opts.threads)
}
lines := make([]string, 0, len(data)+3) //nolint:gomnd
lines = append(lines, colorStatusLine(status))
for _, key := range keys {
lines = append(lines, data[key])
}
lineCount := len(lines)
if lineCount > smoothLines {
smoothLines = lineCount
} else if lineCount < lastLineCount {
lastLineCountReduction = time.Now()
}
if time.Since(lastLineCountReduction) > 500*time.Millisecond {
smoothLines = lineCount
}
for i := 0; i < smoothLines-lineCount; i++ {
lines = append(lines, "")
}
lastLineCount = lineCount
t.SetStatus(lines)
}
const timeFormat = "2006-01-02 15:04:05"
// Stats contains information about jobs.
type Stats struct {
start time.Time
jobs int
jobsFinal bool
processed int
failed int
}
const statusUpdateInterval = 200 * time.Millisecond
//nolint:gocognit
func status(ctx context.Context, wg *sync.WaitGroup, t *termstatus.Terminal, outCh <-chan Status, inCount <-chan int) {
defer wg.Done()
data := make(map[string]string)
ticker := time.NewTicker(statusUpdateInterval)
defer ticker.Stop()
stats := Stats{
start: time.Now(),
}
defer func() {
fmt.Fprintf(color.Output, "\nprocessed %d items (%d failures) in %s\n",
stats.processed,
stats.failed,
formatDuration(time.Since(stats.start)))
}()
for {
select {
case <-ctx.Done():
return
case s, ok := <-outCh:
if !ok {
return
}
var msg string
if s.Message != "" {
msg = s.Message
if s.Error {
msg = fmt.Sprintf("%v %v", colorError("error"), colorError(s.Message))
}
}
if msg != "" {
m := ""
if !opts.hideJobID {
m += colorNumber(s.ID) + " "
}
if !opts.hideTimestamp {
m += colorTimestamp(time.Now().Format(timeFormat)) + " "
}
if !opts.hideName {
m += colorTag(s.Tag) + " "
}
t.Print(m + msg)
}
data[s.Tag] = fmt.Sprintf("%v %v", colorTag(s.Tag), msg)
if s.Done {
stats.processed++
if s.Error {
stats.failed++
}
delete(data, s.Tag)
}
updateTerminal(t, stats, data)
case jobNum, ok := <-inCount:
if !ok {
stats.jobsFinal = true
inCount = nil
continue
}
stats.jobs = jobNum
updateTerminal(t, stats, data)
case <-ticker.C:
updateTerminal(t, stats, data)
}
}
}
type fakeTerminal struct {
io.Writer
fd uintptr
}
func (t fakeTerminal) Fd() uintptr { return t.fd }
const commandBuffer = 50000
func main() {
pflag.IntVarP(&opts.threads, "procs", "p", runtime.NumCPU(), "number of parallel programs")
pflag.StringVar(&opts.placeholder, "replace", "{}", "replace this string in the command to run")
pflag.DurationVar(&opts.workerTimeout, "timeout", 0*time.Second, "set maximum runtime per queued job (0s == no limit)")
pflag.BoolVarP(&opts.useNullSeparator, "null", "0", false, "use null bytes as input separator")
pflag.BoolVar(&opts.hideJobID, "no-id", false, "hide the job id in the log")
pflag.BoolVar(&opts.hideTimestamp, "no-timestamp", false, "hide the time stamp in the log")
pflag.BoolVar(&opts.hideName, "no-name", false, "hide the job name in the log")
pflag.Parse()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var t *termstatus.Terminal
if runtime.GOOS == "windows" {
t = termstatus.New(&fakeTerminal{color.Output, os.Stdout.Fd()}, os.Stderr, false)
} else {
t = termstatus.New(os.Stdout, os.Stderr, false)
}
outCh := make(chan Status)
jobNumCh := make(chan int)
var statusWg sync.WaitGroup
statusWg.Add(1)
go func() {
t.Run(ctx)
statusWg.Done()
}()
statusWg.Add(1)
go status(ctx, &statusWg, t, outCh, jobNumCh)
ch := make(chan *Command, commandBuffer)
var workersWg sync.WaitGroup
for i := 0; i < opts.threads; i++ {
workersWg.Add(1)
go worker(&workersWg, ch, outCh)
}
args := pflag.Args()
if len(args) == 0 {
fmt.Fprintf(os.Stderr, "no command given\n")
pflag.Usage()
// yes, I know, calling os.Exit() ignores the defer "cancel()" call at
// the beginning of main(), but we're quitting the process here so it
// doesn't matter
os.Exit(1) //nolint:gocritic
}
cmdname := args[0]
args = args[1:]
checkForPlaceholder(cmdname, args)
go parseInput(ch, jobNumCh, cmdname, args)
workersWg.Wait()
close(outCh)
cancel()
statusWg.Wait()
}