forked from hashicorp/nomad
-
Notifications
You must be signed in to change notification settings - Fork 1
/
command.go
93 lines (78 loc) · 2.1 KB
/
command.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
package mock
import (
"errors"
"io"
"sync"
"time"
hclog "github.com/hashicorp/go-hclog"
bstructs "github.com/hashicorp/nomad/plugins/base/structs"
"github.com/hashicorp/nomad/plugins/drivers"
)
func runCommand(c Command, stdout, stderr io.WriteCloser, cancelCh <-chan struct{}, pluginExitTimer <-chan time.Time, logger hclog.Logger) *drivers.ExitResult {
errCh := make(chan error, 1)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
runCommandOutput(stdout, c.StdoutString, c.StdoutRepeat, c.stdoutRepeatDuration, cancelCh, logger, errCh)
}()
wg.Add(1)
go func() {
defer wg.Done()
runCommandOutput(stderr, c.StderrString, c.StderrRepeat, c.stderrRepeatDuration, cancelCh, logger, errCh)
}()
timer := time.NewTimer(c.runForDuration)
defer timer.Stop()
select {
case <-timer.C:
logger.Debug("run_for time elapsed; exiting", "run_for", c.RunFor)
case <-cancelCh:
logger.Debug("killed; exiting")
case <-pluginExitTimer:
logger.Debug("exiting plugin")
return &drivers.ExitResult{
Err: bstructs.ErrPluginShutdown,
}
case err := <-errCh:
logger.Error("error running mock task; exiting", "error", err)
return &drivers.ExitResult{
Err: err,
}
}
wg.Wait()
var exitErr error
if c.ExitErrMsg != "" {
exitErr = errors.New(c.ExitErrMsg)
}
return &drivers.ExitResult{
ExitCode: c.ExitCode,
Signal: c.ExitSignal,
Err: exitErr,
}
}
func runCommandOutput(writer io.WriteCloser,
output string, outputRepeat int, repeatDuration time.Duration,
cancelCh <-chan struct{}, logger hclog.Logger, errCh chan error) {
defer writer.Close()
if output == "" {
return
}
if _, err := io.WriteString(writer, output); err != nil {
logger.Error("failed to write to stdout", "error", err)
errCh <- err
return
}
for i := 0; i < outputRepeat; i++ {
select {
case <-cancelCh:
logger.Warn("exiting before done writing output", "i", i, "total", outputRepeat)
return
case <-time.After(repeatDuration):
if _, err := io.WriteString(writer, output); err != nil {
logger.Error("failed to write to stdout", "error", err)
errCh <- err
return
}
}
}
}