-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommand_exec.go
258 lines (220 loc) · 7.91 KB
/
command_exec.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
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"path/filepath"
"sync"
"time"
"github.com/go-zero-boilerplate/loggers"
"github.com/golang-devops/exec-logger/exec_logger_constants"
"github.com/golang-devops/exec-logger/sleep_durations"
)
func NewCommandExecer(logger loggers.LoggerStdIO, stdErrIsError bool, timeoutKillDuration time.Duration, recordResourceUsage bool, runArgs []string) *commandExecer {
statusHandler := &execStatusHandler{
localContextFilePath: exec_logger_constants.LOCAL_CONTEXT_FILE_NAME,
aliveFilePath: exec_logger_constants.ALIVE_FILE_NAME,
exitedFilePath: exec_logger_constants.EXITED_FILE_NAME,
mustAbortFilePath: exec_logger_constants.MUST_ABORT_FILE_NAME,
recordResourceUsageFilePath: exec_logger_constants.RECORD_RESOURCE_USAGE_FILE_NAME,
}
return &commandExecer{
logger: logger,
logFilePath: exec_logger_constants.LOG_FILE_NAME,
stdErrIsError: stdErrIsError,
timeoutKillDuration: timeoutKillDuration,
recordResourceUsage: recordResourceUsage,
runArgs: runArgs,
statusHandler: statusHandler,
stdioHandler: nil, //Set inside `Run` method
}
}
type commandExecer struct {
logger loggers.LoggerStdIO
logFilePath string
stdErrIsError bool
timeoutKillDuration time.Duration
recordResourceUsage bool
runArgs []string
statusHandler *execStatusHandler
stdioHandler *stdioHandler
}
func (c *commandExecer) abortProcess(cmd *exec.Cmd) {
defer func() {
if rec := recover(); rec != nil {
c.stdioHandler.writeErrorLine(fmt.Sprintf("Kill process attempt recovered, recovery: %+v", rec))
}
}()
pid := cmd.Process.Pid
force := true
if killErr := KillProcessTree(pid, force); killErr != nil { //if killErr := cmd.Process.Kill(); killErr != nil {
c.stdioHandler.writeErrorLine(fmt.Sprintf("Cannot kill process with PID %d, error: %s", pid, killErr.Error()))
}
c.stdioHandler.writeFileLine(fmt.Sprintf("Successfully killed process with PID %d", pid))
}
func (c *commandExecer) cleanupBeforeStarting() error {
if err := os.Remove(c.statusHandler.aliveFilePath); err != nil {
if !os.IsNotExist(err) {
return fmt.Errorf("Cannot remove alive file '%s', error: %s", c.statusHandler.aliveFilePath, err.Error())
}
}
if err := os.Remove(c.statusHandler.exitedFilePath); err != nil {
if !os.IsNotExist(err) {
return fmt.Errorf("Cannot remove exited file '%s', error: %s", c.statusHandler.exitedFilePath, err.Error())
}
}
if err := os.Remove(c.statusHandler.mustAbortFilePath); err != nil {
if !os.IsNotExist(err) {
return fmt.Errorf("Cannot remove must-abort file '%s', error: %s", c.statusHandler.mustAbortFilePath, err.Error())
}
}
if err := os.Remove(c.statusHandler.recordResourceUsageFilePath); err != nil {
if !os.IsNotExist(err) {
return fmt.Errorf("Cannot remove resource-usage file '%s', error: %s", c.statusHandler.recordResourceUsageFilePath, err.Error())
}
}
return nil
}
func (c *commandExecer) runCommand() (exitCode int, returnErr error) {
if err := c.cleanupBeforeStarting(); err != nil {
return -1, err
}
cmd := exec.Command(c.runArgs[0], c.runArgs[1:]...)
stdout, err := cmd.StdoutPipe()
if err != nil {
return -1, err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return -1, err
}
err = cmd.Start()
if err != nil {
return -1, err
}
err = c.statusHandler.WriteLocalContextFile()
if err != nil {
c.stdioHandler.writeErrorLine(fmt.Sprintf("Cannot write local context, error: %s", err.Error()))
//do not want to exit due to this error
}
c.stdioHandler.writeFileLine(fmt.Sprintf("Process started with PID %d", cmd.Process.Pid))
go func(sh *execStatusHandler) {
for {
if tmpErr := sh.WriteAlive(); tmpErr != nil {
c.stdioHandler.writeErrorLine(fmt.Sprintf("Cannot write alive file, error: %s", tmpErr.Error()))
}
time.Sleep(2 * time.Second)
}
}(c.statusHandler)
procID := cmd.Process.Pid
if c.recordResourceUsage {
c.stdioHandler.writeFileLine("Starting to record resource usage")
go func(sh *execStatusHandler) {
iterationsPerDuration := 10
durationList := []time.Duration{
500 * time.Millisecond,
2 * time.Second,
10 * time.Second,
30 * time.Second,
}
durationIncreaser := sleep_durations.New(iterationsPerDuration, durationList)
for {
if tmpErr := sh.WriteResourceUsage(procID); tmpErr != nil {
c.stdioHandler.writeErrorLine(fmt.Sprintf("Cannot write resource-usage file, error: %s", tmpErr.Error()))
}
time.Sleep(durationIncreaser.Next())
}
}(c.statusHandler)
}
go func(sh *execStatusHandler) {
for {
if mustAbort, checkErr := sh.CheckMustAbort(); checkErr != nil {
c.stdioHandler.writeFileLine(fmt.Sprintf("Unable to check for abort request, error: %s", checkErr.Error()))
} else if mustAbort {
c.stdioHandler.writeFileLine("Got ABORT message")
c.abortProcess(cmd)
break
}
time.Sleep(2 * time.Second)
}
}(c.statusHandler)
c.stdioHandler.stdoutScanner = bufio.NewScanner(stdout)
c.stdioHandler.stderrScanner = bufio.NewScanner(stderr)
var wg sync.WaitGroup
wg.Add(2)
go c.stdioHandler.startScanningStdout(&wg)
go c.stdioHandler.startScanningStderr(&wg)
var waitErr error
timeoutOccurred := false
if c.timeoutKillDuration > 0 {
c.stdioHandler.writeFileLine(fmt.Sprintf("Using timeout of '%s' for process", c.timeoutKillDuration.String()))
done := make(chan error)
go func() { done <- cmd.Wait() }()
select {
case waitErr = <-done:
waitErr = waitErr
case <-time.After(c.timeoutKillDuration):
c.stdioHandler.writeFileLine(fmt.Sprintf("Timeout of %s reached, now aborting", c.timeoutKillDuration.String()))
c.abortProcess(cmd)
timeoutOccurred = true
}
} else {
c.stdioHandler.writeFileLine("No timeout set for process")
waitErr = cmd.Wait()
}
//TODO: Just give things time to cool down, like writing of the "Successfully killed process" log. This can however be improved with a WaitGroup
time.Sleep(500 * time.Millisecond)
if waitErr != nil {
if exitCode, ok := getExitCodeFromError(waitErr); ok {
return exitCode, waitErr
}
return -1, waitErr
}
wg.Wait()
if timeoutOccurred {
return -1, fmt.Errorf("The command timed out after '%s'", c.timeoutKillDuration.String())
}
if c.stdioHandler.commandHadStdErr && c.stdErrIsError {
return -1, fmt.Errorf("The command finished running but had error lines (written to stderr).")
}
return 0, nil
}
func (c *commandExecer) Run() (exitCode int, returnErr error) {
err := os.Remove(c.logFilePath)
if err != nil && !os.IsNotExist(err) {
return -1, fmt.Errorf("Failure to remove log file, error: %s", err.Error())
}
parentDir := filepath.Dir(c.logFilePath)
if err := os.MkdirAll(parentDir, 0755); err != nil {
return -1, fmt.Errorf("Unable to create parent dir '%s' of log file, error: %s", parentDir, err.Error())
}
logFile, err := os.OpenFile(c.logFilePath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0655)
if err != nil {
return -1, fmt.Errorf("Failure to open log file '%s' for writing, error; %s", c.logFilePath, err.Error())
}
defer logFile.Close()
c.stdioHandler = &stdioHandler{
logger: c.logger,
writer: logFile,
}
startTime := time.Now()
c.stdioHandler.writeFileLine(fmt.Sprintf("Exec-logger version %s", Version))
c.stdioHandler.writeFileLine(fmt.Sprintf("Calling commandline: %s", joinCommandLine(c.runArgs)))
exitCode, err = c.runCommand()
exitCodeMsg := fmt.Sprintf("Command exited with code %d", exitCode)
if exitCode != 0 {
c.stdioHandler.writeErrorLine(exitCodeMsg)
} else {
c.stdioHandler.writeFileLine(exitCodeMsg)
}
totalDuration := time.Now().Sub(startTime)
c.statusHandler.WriteExitedJson(exitCode, err, totalDuration)
c.stdioHandler.writeFileLine(fmt.Sprintf("Total duration was %s", totalDuration.String()))
if err != nil {
returnErr = fmt.Errorf("Unable to run command, error: %s", err.Error())
c.stdioHandler.writeErrorLine(returnErr.Error())
return exitCode, returnErr
}
return 0, nil
}