/
exec_util.go
89 lines (75 loc) · 2.13 KB
/
exec_util.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
package util
import (
"context"
"fmt"
"io"
"os/exec"
"sync"
"github.com/chrislusf/gleamold/pb"
)
// all data passing through pipe are all (size, msgpack_encoded) tuples
// The input and output should all be this msgpack format.
// Only the stdin and stdout of Pipe() is line based text.
func Execute(ctx context.Context, executeWaitGroup *sync.WaitGroup, stat *pb.InstructionStat,
name string, command *exec.Cmd,
reader io.Reader, writer io.Writer, prevIsPipe, isPipe bool, closeOutput bool,
errWriter io.Writer) error {
defer func() {
executeWaitGroup.Done()
if closeOutput {
if c, ok := writer.(io.Closer); ok {
c.Close()
}
}
}()
var wg sync.WaitGroup
if reader != nil {
if prevIsPipe && isPipe {
// println("step", name, "input is lines->lines")
command.Stdin = reader
} else if !prevIsPipe && !isPipe {
// println("step", name, "input is msgpack->msgpack")
command.Stdin = reader
} else {
inputWriter, stdinErr := command.StdinPipe()
if stdinErr != nil {
fmt.Fprintf(errWriter, "Failed to open StdinPipe: %v", stdinErr)
} else {
wg.Add(1)
if !prevIsPipe && isPipe {
// println("step", name, "input is msgpack->lines")
go ChannelToLineWriter(&wg, stat, name, reader, inputWriter, errWriter)
} else {
// println("step", name, "input is lines->msgpack")
go LineReaderToChannel(&wg, stat, name, reader, inputWriter, true, errWriter)
}
}
}
}
command.Stdout = writer
command.Stderr = errWriter
// println(name, "starting...")
if startError := command.Start(); startError != nil {
return fmt.Errorf("Start error %v: %v\n", startError, command)
}
// fmt.Printf("%s Command is waiting..\n", name)
errChan := make(chan error)
go func() {
wg.Wait()
waitError := command.Wait()
if waitError != nil {
waitError = fmt.Errorf("%s Wait error %+v.\n", name, waitError)
}
errChan <- waitError
}()
// defer fmt.Printf("%s Command is finished.\n", name)
select {
case <-ctx.Done():
println("cancel process", command.Process.Pid, name, "...")
command.Process.Kill()
command.Process.Release()
return ctx.Err()
case err := <-errChan:
return err
}
}