-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
274 lines (226 loc) · 6.49 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
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"github.com/fatih/semgroup"
"golang.org/x/net/context"
"log"
"os"
"os/exec"
"strconv"
"strings"
"syscall"
"time"
)
var cmdInterpreter = os.Getenv("SHELL")
const example = "`./threadme -t5 -cmd 'echo \"{{N}}:{{LINE}}\"'`"
func main() {
cmd := flag.String("cmd", "", "Command to execute")
fpath := flag.String("f", "", "file to read for workers and replace {{LINE}} in cmd")
threads := flag.Int("c", 5, "Concurrency")
delayMs := flag.Int("d", 10, "Delay in milliseconds")
wtime := flag.Int("tl", 60*1000, "Time limit for single job in milliseconds")
n := flag.Int("n", 100, "If no file given this is how many jobs will be performed")
stopOnMsg := flag.String("stop-on", "", "If any job gets this message, all stops")
whileMsg := flag.String("while", "", "Keep running while all have this message")
interpreter := flag.String("interpreter", cmdInterpreter, "Interpreter for command")
forever := flag.Bool("forever", false, "Run forever")
flag.Parse()
// check flag conflicts
if *forever && *fpath != "" {
log.Fatalf("You must choose one: `--forever` or `-fpath` flag!")
}
if *interpreter != "" {
cmdInterpreter = *interpreter
}
*cmd = strings.TrimSpace(*cmd)
if *cmd == "" {
log.Fatalf("Empty command. %s", example)
}
if *threads <= 1 {
log.Fatalf("Why you need `threadme` if no concurrency given..? (think)")
}
if *delayMs < 0 {
*delayMs = 1
}
if *forever {
*n = 0
*delayMs = 250 // larger delay
}
var flines []string
if *fpath != "" {
_, err := os.Stat(*fpath)
if err != nil {
log.Fatalf("\n\nFile `%s` error: %s\n", *fpath, err)
return
}
// file exists, try to read
lines, err := readLines(*fpath)
if err != nil {
log.Fatalf("\n\nFILE READ ERROR!\n%s\n", err)
return
}
flines = lines
}
fmt.Printf("%20s: [%s]\n", "Command", *cmd)
if *interpreter != os.Getenv("SHELL") {
fmt.Printf("%20s: [%s]\n", "Interpreter", *interpreter)
}
if len(flines) > 0 {
fmt.Printf("%20s: [%s]\n", "File to read", *fpath)
fmt.Printf("%20s: [%d]\n", "Lines in file", len(flines))
} else if *forever {
fmt.Printf("%20s: ∞ \n", "Run forever")
} else {
fmt.Printf("%20s: [%d]\n", "Job count to perform", *n)
}
fmt.Printf("%20s: [%d]\n", "Threads", *threads)
fmt.Printf("%20s: %d ms\n", "Delay", *delayMs)
fmt.Printf("%20s: %d ms\n", "Timeout for single job", *wtime)
if *stopOnMsg != "" {
fmt.Printf("%20s: '%s'\n", "Stop if contains", *stopOnMsg)
}
if *whileMsg != "" {
fmt.Printf("%20s: '%s'\n", "Continue while", *whileMsg)
}
fmt.Println(strings.Repeat("-", 80))
var tStart = time.Now()
defer func() {
log.Printf("> Duration: %s ", time.Since(tStart))
}()
var maxWorkers = *threads
ctx, cancelAll := context.WithCancel(context.Background())
sg := semgroup.NewGroup(ctx, int64(maxWorkers))
go func() {
<-ctx.Done()
// The context was canceled
log.Printf("> Stopping all workers!")
log.Printf("> Duration: %s ", time.Since(tStart))
os.Exit(1)
}()
// fill `flines` with `n` numbers and use same `for` loop
if len(flines) == 0 {
for i := 0; i < *n; i++ {
flines = append(flines, strconv.Itoa(i))
}
}
total := len(flines)
sTotal := fmt.Sprintf("%d", total)
if *forever {
sTotal = "∞"
}
for index := 0; ; index++ {
i := index
// stop on limit reached if not forever
if !*forever && (i > total-1) {
break
}
// Replace variables
flcmd := *cmd
flcmd = strings.ReplaceAll(flcmd, "{{N}}", fmt.Sprintf("%d", i))
// Replace variables from file
if !*forever && i <= total-1 {
flcmd = strings.ReplaceAll(flcmd, "{{LINE}}", flines[i])
}
sg.Go(func() error {
// log.Printf("`%v`", flcmd)
cmdOut, errBuf, err := runBashWithTimeout(time.Duration(*wtime)*time.Millisecond, flcmd)
cmdOut = bytes.TrimSpace(cmdOut)
// log.Printf("%v -- %v -- %v", cmdOut, errBuf, err)
errStr := ""
if errBuf != nil && len(errBuf) > 0 {
errStr += strings.TrimSpace(string(errBuf)) + "; "
}
if err != nil {
errStr += strings.TrimSpace(string(err.Error())) + "; "
}
errStr = strings.TrimSpace(errStr)
if errStr != "" {
log.Printf("ERROR: [%d/%s] [%s] ==> [%s]", i, sTotal, flcmd, errStr)
// do not mark semgroup job with error. we printed it
return nil
}
log.Printf("[%d/%s] [%s] ==> [%s]", i, sTotal, flcmd, cmdOut)
// Stop if not expected message
if *whileMsg != "" && !strings.Contains(string(cmdOut), *whileMsg) {
log.Printf("> No `while message` found: %s", cmdOut)
cancelAll()
return nil
}
// Stop all workers when specific string seen
if *stopOnMsg != "" {
var needToStop bool
if strings.Contains(string(cmdOut), *stopOnMsg) {
log.Printf("> Stop output message found: %s", cmdOut)
needToStop = true
}
if strings.Contains(errStr, *stopOnMsg) {
log.Printf("> Stop error message found: %s", errStr)
needToStop = true
}
if needToStop {
cancelAll()
return nil
}
}
time.Sleep(time.Duration(*delayMs) * time.Millisecond)
return nil
})
}
if err := sg.Wait(); err != nil {
fmt.Println(err)
}
}
// readLines reads a whole file
// and returns a slice of its lines.
func readLines(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
// Execute bash command with valid timeout
// kill process if too long execution
func runBashWithTimeout(timeout time.Duration, cmdstr string) ([]byte, []byte, error) {
// Run command in env as whole
// Useful when need to execute command with wildcard so these characters
// is not treated as string
name := cmdInterpreter
args := []string{
"-c",
strings.TrimPrefix(cmdstr, cmdInterpreter+" -c "), // as one argument
}
cmd := exec.Command(name, args...) // #nosec G204
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
bufOut := &bytes.Buffer{}
cmd.Stdout = bufOut
bufErr := &bytes.Buffer{}
cmd.Stderr = bufErr
if err := cmd.Start(); err != nil {
return nil, nil, err
}
if timeout > 0 {
go func() {
time.Sleep(timeout) // wait in background
pgid, err := syscall.Getpgid(cmd.Process.Pid)
if err == nil {
// log.Printf("[ KILL ] Kill process of command: %s", name)
if err := syscall.Kill(-pgid, 15); err != nil { // note the minus sign
// skip error check
log.Printf("(Warning: %s)", err)
}
}
}()
}
err := cmd.Wait()
return bufOut.Bytes(), bufErr.Bytes(), err
}