-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.go
227 lines (182 loc) · 5.31 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
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
package main // import "github.com/TaserudConsulting/goprocmgr"
import (
"bufio"
"fmt"
"log"
"math/rand"
"os"
"os/exec"
"strings"
"syscall"
"time"
)
type Runner struct {
config *Config
ActiveProcesses map[string]*ActiveRunner
}
type LogEntry struct {
Timestamp time.Time `json:"timestamp"`
Message string `json:"message"`
Output string `json:"output"`
}
type ActiveRunner struct {
Cmd *exec.Cmd
Port uint
Logs []LogEntry
}
func (runner *Runner) Start(name string, serve *Serve) error {
// Init active processes map
if runner.ActiveProcesses == nil {
runner.ActiveProcesses = make(map[string]*ActiveRunner)
}
if _, ok := runner.config.Servers[name]; !ok {
return fmt.Errorf("unknown server %s", name)
}
if _, ok := runner.ActiveProcesses[name]; ok {
return fmt.Errorf("server is already running: %s", name)
}
// Split the command on the first space since exec.Command will
// look for the first argument only in the path as a binary name.
splitCmd := strings.SplitN(runner.config.Servers[name].Command, " ", 2)
// Set up a command.
var cmd *exec.Cmd
if runner.config.Servers[name].UseDirenv {
if len(splitCmd) > 1 {
cmd = exec.Command(
"direnv",
"exec",
".",
splitCmd[0],
"--",
splitCmd[1],
)
} else {
cmd = exec.Command(
"direnv",
"exec",
".",
runner.config.Servers[name].Command,
)
}
} else {
if len(splitCmd) > 1 {
cmd = exec.Command(splitCmd[0], splitCmd[1])
} else {
cmd = exec.Command(runner.config.Servers[name].Command)
}
}
// Store my active processes
activeRunner := ActiveRunner{Cmd: cmd}
// Specify runtime directory.
cmd.Dir = runner.config.Servers[name].Directory
// Randomize a port to supply as environment variable.
port, err := runner.randomizePortNumber()
if err != nil {
return err
}
// Store the port in the runner to expose in the API.
activeRunner.Port = port
// Set environment for running command, first inherit the env from the running command.
cmd.Env = os.Environ()
// Then, add the environment variables from the config.
cmd.Env = append(cmd.Env, fmt.Sprintf("PORT=%d", port))
cmd.Env = append(cmd.Env, fmt.Sprintf("PATH=%s", runner.config.Servers[name].Environment["PATH"]))
// Set up pipe to read stdout
stdout, err := cmd.StdoutPipe()
if err != nil {
return fmt.Errorf("failed to set up stdout pipe: %s", err)
}
// Set up pipe to read stderr
stderr, err := cmd.StderrPipe()
if err != nil {
return fmt.Errorf("failed to set up stderr pipe: %s", err)
}
// Start the command
if err := cmd.Start(); err != nil {
return fmt.Errorf("failed to start process: %s", err)
}
// Read stdout while the command is executed
stdoutScanner := bufio.NewScanner(stdout)
go func() {
for stdoutScanner.Scan() {
// Log it to the common log
activeRunner.Logs = append(activeRunner.Logs, LogEntry{
Timestamp: time.Now(),
Message: stdoutScanner.Text(),
Output: "stdout",
})
serve.stateChange <- true
}
}()
// Read stderr while the command is executed
stderrScanner := bufio.NewScanner(stderr)
go func() {
for stderrScanner.Scan() {
// Log it to the common log
activeRunner.Logs = append(activeRunner.Logs, LogEntry{
Timestamp: time.Now(),
Message: stderrScanner.Text(),
Output: "stderr",
})
serve.stateChange <- true
}
}()
// Store the Cmd process as an active process
runner.ActiveProcesses[name] = &activeRunner
// Notify state change on start
serve.stateChange <- true
return nil
}
func (runner *Runner) randomizePortNumber() (uint, error) {
portRangeSize := int(runner.config.Settings.PortRangeMax - runner.config.Settings.PortRangeMin)
if len(runner.ActiveProcesses) >= portRangeSize {
return 0, fmt.Errorf("out of ports, won't be able to find a port in configured range")
}
// Randomize ports within the range
randomPorts := rand.Perm(portRangeSize)
for _, randomPort := range randomPorts {
isPortInUse := false
randomPort += int(runner.config.Settings.PortRangeMin)
for _, activeProcess := range runner.ActiveProcesses {
if int(activeProcess.Port) == randomPort {
isPortInUse = true
break
}
}
if !isPortInUse {
return uint(randomPort), nil
}
}
return 0, fmt.Errorf("tried to randomize an unused port, failed")
}
func (runner *Runner) Stop(name string, serve *Serve) error {
// Init active processes map
if runner.ActiveProcesses == nil {
runner.ActiveProcesses = make(map[string]*ActiveRunner)
}
// If server isn't running, just abort.
if _, ok := runner.ActiveProcesses[name]; !ok {
return nil
}
// Add a go routine to check if the process is killed or not after
// we've told it to SIGTERM. If it's still running, send a SIGKILL
// instead to clean up.
go func() {
time.Sleep(60 * time.Second)
if _, ok := runner.ActiveProcesses[name]; ok {
log.Printf("Force killed the process since it was still alive after 60 seconds %s", name)
runner.ActiveProcesses[name].Cmd.Process.Kill()
// Delete old status for process
delete(runner.ActiveProcesses, name)
}
}()
// Send SIGTERM to the process
runner.ActiveProcesses[name].Cmd.Process.Signal(syscall.SIGTERM)
// Wait for process to end
runner.ActiveProcesses[name].Cmd.Wait()
// Delete old status for process
delete(runner.ActiveProcesses, name)
// Notify state change on stop
serve.stateChange <- true
return nil
}