/
runner.go
90 lines (75 loc) · 1.56 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
package runner
import (
"context"
"io"
"os"
"os/exec"
"runtime"
"time"
"github.com/cskr/pubsub"
"go.uber.org/zap"
)
func RunServer(ctx context.Context, ps *pubsub.PubSub, r io.Reader, w io.Writer, once bool) {
zap.L().Info("starting blocking process")
for {
err := runBlocking(ctx, ps, r, w)
if err != nil {
if err == context.Canceled {
break
}
zap.L().Info("process exited with error, restarting", zap.Error(err))
} else {
zap.L().Info("process exited cleanly, restarting")
}
cleanup()
if once {
break
}
time.Sleep(time.Second * 5)
ps.Pub(struct{}{}, "info.restart")
}
}
func runBlocking(parentctx context.Context, ps *pubsub.PubSub, in io.Reader, out io.Writer) (err error) {
ctx, cancel := context.WithCancel(parentctx)
defer cancel()
var binary string
switch runtime.GOOS {
case "windows":
binary = "./samp-server.exe"
case "linux":
binary = "./samp03svr"
default:
panic("unknown OS")
}
cmd := exec.CommandContext(ctx, binary)
cmd.Stdin = in
r := cmdReader(cmd)
if err := cmd.Start(); err != nil {
return err
}
go func() {
// TODO: Handle IO copy errors.
// nolint:errcheck
io.Copy(out, r)
}()
go func() {
<-ps.SubOnce("restart")
zap.L().Info("internally triggered process restart")
cancel()
}()
return cmd.Wait()
}
func cmdReader(cmd *exec.Cmd) io.Reader {
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil
}
stderr, err := cmd.StderrPipe()
if err != nil {
return nil
}
return io.MultiReader(stdout, stderr)
}
func cleanup() {
os.Remove("server_log.txt") //nolint:errcheck
}