-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.go
56 lines (45 loc) · 1.06 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
package main
import (
"os"
"errors"
"log"
"os/exec"
)
func startRunner(commandToRun []string) (chan<- string, error) {
if len(commandToRun) < 1 {
return nil, errors.New("runner: no arguments passed")
}
reloadChannel := make(chan string)
go func() {
log.Printf("Starting process %s", commandToRun)
process, err := runCommand(commandToRun)
if err != nil {
log.Printf("Run failed: %s", err)
}
for {
reloadMessage := <-reloadChannel
if reloadMessage == "restart" {
if err := process.Kill(); err != nil {
log.Printf("Kill failed: %s", err)
}
log.Printf("Restarting process %s", commandToRun)
process, err = runCommand(commandToRun)
if err != nil {
log.Printf("Run failed: %s", err)
}
}
}
}()
return reloadChannel, nil
}
func runCommand(commandToRun []string) (*os.Process, error) {
programPath, err := exec.LookPath(commandToRun[0])
if err != nil {
return nil, err
}
cmd := exec.Command(programPath, commandToRun[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stdin
cmd.Run()
return cmd.Process, err
}