/
process.go
203 lines (165 loc) · 3.96 KB
/
process.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
package base
import (
"context"
"io"
"os"
"os/exec"
"sync"
"syscall"
"time"
"github.com/pkg/errors"
"github.com/codefly-dev/core/configurations"
"github.com/codefly-dev/core/wool"
)
/*
Still not quite correct
*/
type Process struct {
bin string
args []string
dir string
debug bool
envs []string
// internal
cmd *exec.Cmd
finished bool
w *wool.Wool
// for output
out io.Writer
// context
ctx context.Context
cancel func()
// wait for the logs to be written
wg sync.WaitGroup
pid int
}
func NewProcess(ctx context.Context, bin string) (*Process, error) {
w := wool.Get(ctx).In("runner")
if _, err := exec.LookPath(bin); err != nil {
return nil, w.Wrapf(err, "cannot find <%s>", bin)
}
ctx, cancel := context.WithCancel(ctx)
runner := &Process{
bin: bin,
finished: false,
w: w,
out: w,
ctx: ctx,
cancel: cancel,
}
return runner, nil
}
func (runner *Process) WithDir(dir string) {
runner.dir = dir
}
func (runner *Process) WithEnvironmentVariables(envs ...configurations.EnvironmentVariable) {
runner.envs = append(runner.envs, configurations.EnvironmentVariableAsStrings(envs)...)
}
func (runner *Process) WithDebug(debug bool) {
runner.debug = debug
}
func (runner *Process) WithOutput(w io.Writer) {
runner.out = w
}
func (runner *Process) Init(_ context.Context) error {
return nil
}
// Run executes and wait
func (runner *Process) Run(ctx context.Context) error {
if runner.out == nil {
return errors.New("cannot run without output")
}
// #nosec G204
runner.cmd = exec.CommandContext(ctx, runner.bin, runner.args...)
stdout, err := runner.cmd.StdoutPipe()
if err != nil {
return errors.Wrap(err, "cannot create stdout pipe")
}
stderr, err := runner.cmd.StderrPipe()
if err != nil {
return errors.Wrap(err, "cannot create stderr pipe")
}
reader := io.MultiReader(stdout, stderr)
ForwardLogs(ctx, &runner.wg, reader, runner.out)
if runner.dir != "" {
runner.cmd.Dir = runner.dir
}
runner.cmd.Env = os.Environ()
runner.cmd.Env = append(runner.cmd.Env, runner.envs...)
err = runner.cmd.Run()
if err != nil {
return runner.w.Wrapf(err, "cannot run command")
}
return nil
}
// Start executing and return
func (runner *Process) Start(ctx context.Context) error {
// #nosec G204
runner.cmd = exec.CommandContext(ctx, runner.bin, runner.args...)
stdout, err := runner.cmd.StdoutPipe()
if err != nil {
return errors.Wrap(err, "cannot create stdout pipe")
}
stderr, err := runner.cmd.StderrPipe()
if err != nil {
return errors.Wrap(err, "cannot create stderr pipe")
}
reader := io.MultiReader(stdout, stderr)
ForwardLogs(ctx, &runner.wg, reader, runner.out)
if runner.dir != "" {
runner.cmd.Dir = runner.dir
}
runner.cmd.Env = os.Environ()
runner.cmd.Env = append(runner.cmd.Env, runner.envs...)
err = runner.cmd.Start()
if err != nil {
return runner.w.Wrapf(err, "cannot run command")
}
runner.pid = runner.cmd.Process.Pid
runner.w.Debug("started process", wool.Field("pid", runner.pid))
return nil
}
func (runner *Process) Wait() error {
if runner.finished {
return nil
}
return runner.cmd.Wait()
}
func (runner *Process) Finished() bool {
return false
}
func (runner *Process) Finish() {
runner.finished = true
}
func (runner *Process) Stop() error {
if runner == nil {
return nil
}
if runner.cmd == nil {
return nil
}
err := runner.cmd.Process.Signal(syscall.SIGTERM)
if err != nil {
return runner.w.Wrapf(err, "cannot sigterm process")
}
// Wait bit
<-time.After(1 * time.Second)
runner.cancel()
// Kill the process to be sure
// Check if the process is still running
err = runner.cmd.Process.Signal(syscall.Signal(0))
if err == nil {
// Process is still running, send SIGKILL
err = runner.cmd.Process.Kill()
if err != nil {
return runner.w.Wrapf(err, "cannot kill process")
}
}
return nil
}
func (runner *Process) WithArguments(args ...string) {
runner.args = args
}
func (runner *Process) WithBin(bin string) {
runner.bin = bin
}