-
Notifications
You must be signed in to change notification settings - Fork 6
/
runner.go
164 lines (137 loc) · 3.46 KB
/
runner.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
package gaper
import (
"errors"
"fmt"
"io"
"os"
"os/exec"
"runtime"
"syscall"
"time"
)
// OSWindows is used to check if current OS is a Windows
const OSWindows = "windows"
// os errors
var errFinished = errors.New("os: process already finished")
// Runner is a interface for the run process
type Runner interface {
Run() (*exec.Cmd, error)
Kill() error
Errors() chan error
Exited() bool
IsRunning() bool
ExitStatus(err error) int
}
type runner struct {
bin string
args []string
writerStdout io.Writer
writerStderr io.Writer
command *exec.Cmd
starttime time.Time
errors chan error
end chan bool // used internally by Kill to wait a process die
}
// NewRunner creates a new runner
func NewRunner(wStdout io.Writer, wStderr io.Writer, bin string, args []string) Runner {
return &runner{
bin: bin,
args: args,
writerStdout: wStdout,
writerStderr: wStderr,
starttime: time.Now(),
errors: make(chan error),
end: make(chan bool),
}
}
// Run executes the project binary
func (r *runner) Run() (*exec.Cmd, error) {
logger.Info("Starting program")
if r.command != nil && !r.Exited() {
return r.command, nil
}
if err := r.runBin(); err != nil {
return nil, fmt.Errorf("error running: %v", err)
}
return r.command, nil
}
// Kill the current process running for the Golang project
func (r *runner) Kill() error { // nolint gocyclo
if r.command == nil || r.command.Process == nil {
return nil
}
done := make(chan error)
go func() {
<-r.end
close(done)
}()
// Trying a "soft" kill first
if runtime.GOOS == OSWindows {
if err := r.command.Process.Kill(); err != nil {
return err
}
} else if err := r.command.Process.Signal(os.Interrupt); err != nil {
return err
}
// Wait for our process to die before we return or hard kill after 3 sec
select {
case <-time.After(3 * time.Second):
if err := r.command.Process.Kill(); err != nil {
errMsg := err.Error()
// ignore error if the processed has been killed already
if errMsg != errFinished.Error() && errMsg != os.ErrInvalid.Error() {
return fmt.Errorf("failed to kill: %v", err)
}
}
case <-done:
}
r.command = nil
return nil
}
// Exited checks if the process has exited
func (r *runner) Exited() bool {
return r.command != nil && r.command.ProcessState != nil && r.command.ProcessState.Exited()
}
// IsRunning returns if the process is running
func (r *runner) IsRunning() bool {
return r.command != nil && r.command.Process != nil && r.command.Process.Pid > 0
}
// Errors get errors occurred during the build
func (r *runner) Errors() chan error {
return r.errors
}
// ExitStatus resolves the exit status
func (r *runner) ExitStatus(err error) int {
var exitStatus int
if exiterr, ok := err.(*exec.ExitError); ok {
if status, oks := exiterr.Sys().(syscall.WaitStatus); oks {
exitStatus = status.ExitStatus()
}
}
return exitStatus
}
func (r *runner) runBin() error {
r.command = exec.Command(r.bin, r.args...) // nolint gas
stdout, err := r.command.StdoutPipe()
if err != nil {
return err
}
stderr, err := r.command.StderrPipe()
if err != nil {
return err
}
// TODO: handle or log errors
go io.Copy(r.writerStdout, stdout) // nolint errcheck
go io.Copy(r.writerStderr, stderr) // nolint errcheck
err = r.command.Start()
if err != nil {
return err
}
r.starttime = time.Now()
// wait for exit errors
go func() {
r.errors <- r.command.Wait()
r.end <- true
}()
return nil
}