-
Notifications
You must be signed in to change notification settings - Fork 231
/
run.go
97 lines (87 loc) · 2.16 KB
/
run.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
package util
import (
"bytes"
"context"
"io"
"os/exec"
"strings"
"time"
"github.com/rs/zerolog/log"
)
// CMDContext context of cmd
type CMDContext struct {
Ctx context.Context
Cmd *exec.Cmd
Name string
Stop chan struct{} // notify parent current Cmd occur error
}
// RunAndWait run cmd
func RunAndWait(cmd *exec.Cmd) (string, string, error) {
outbuf, errbuf, err := runCmd(&CMDContext{
Cmd: cmd,
Name: cmd.Path,
})
if err != nil {
return outbuf.String(), errbuf.String(), err
}
err = cmd.Wait()
return outbuf.String(), errbuf.String(), err
}
// BackgroundRun run cmd in background with context
func BackgroundRun(cmdCtx *CMDContext) error {
outbuf, errbuf, err := runCmd(cmdCtx)
if err != nil {
return err
}
go func() {
err = cmdCtx.Cmd.Wait()
stdout := strings.TrimSpace(outbuf.String())
stderr := strings.TrimSpace(errbuf.String())
if len(stdout) > 0 {
log.Debug().Msgf("[STDOUT] %s", stdout)
}
if len(stderr) > 0 {
log.Debug().Msgf("[STDERR] %s", stderr)
}
if err != nil {
log.Info().Msgf("Background task %s exit abnormally: %s", cmdCtx.Name, err.Error())
}
log.Info().Msgf("Task %s completed", cmdCtx.Name)
}()
return nil
}
// CanRun check whether a command can execute successful
func CanRun(cmd *exec.Cmd) bool {
return cmd.Run() == nil
}
func runCmd(cmdCtx *CMDContext) (*bytes.Buffer, *bytes.Buffer, error) {
cmd := cmdCtx.Cmd
log.Debug().Msgf("Task name = %s, cmd.Args = %+v", cmdCtx.Name, cmd.Args)
stdout, _ := cmd.StdoutPipe()
stderr, _ := cmd.StderrPipe()
outbuf := bytes.NewBufferString("")
errbuf := bytes.NewBufferString("")
if stdout != nil && stderr != nil {
go io.Copy(outbuf, stdout)
go io.Copy(errbuf, stderr)
}
if err := cmd.Start(); err != nil {
return outbuf, errbuf, err
}
time.Sleep(100 * time.Millisecond)
pid := cmd.Process.Pid
log.Debug().Msgf("Start %s at pid: %d", cmdCtx.Name, pid)
// will kill the process when parent cancel
go func() {
if cmdCtx.Ctx != nil {
select {
case <-cmdCtx.Ctx.Done():
err2 := cmd.Process.Kill()
if err2 != nil {
log.Debug().Msgf("Task %s(%d) killed", cmdCtx.Name, pid)
}
}
}
}()
return outbuf, errbuf, nil
}