-
Notifications
You must be signed in to change notification settings - Fork 0
/
MTAServer.go
192 lines (156 loc) · 4.12 KB
/
MTAServer.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
package main
import (
"bufio"
"bytes"
"container/ring"
"errors"
"fmt"
"io"
"os"
"os/exec"
"sync"
"time"
)
// MTAServer type represents an MTA Server process
type MTAServer struct {
Path string
Process *exec.Cmd
Running bool
Stdin io.WriteCloser
Stdout io.ReadCloser
OutputBuffer *ring.Ring
WaitMutex sync.Mutex
}
// NewMTAServer instantiates a new MTA server instance
func NewMTAServer(path string, watchdogEnabled bool) *MTAServer {
server := new(MTAServer)
server.Path = path
server.OutputBuffer = ring.New(5000)
server.Process = nil
// Start watchdog
if watchdogEnabled {
fmt.Println("Starting watchdog...")
go server.WatchProcess()
}
return server
}
func (server *MTAServer) Start() error {
// Don't start processes twice
if server.Process != nil && server.Process.Process != nil && server.Process.ProcessState != nil && !server.Process.ProcessState.Exited() {
return errors.New("Process is already running")
}
// Spawn process
server.Process = exec.Command(server.Path, "-n", "-t", "-u")
// Get stdin
var err error
server.Stdin, err = server.Process.StdinPipe()
if err != nil {
return err
}
// Get stdout
server.Stdout, err = server.Process.StdoutPipe()
if err != nil {
return err
}
// Capture output into ring buffer
scanner := bufio.NewScanner(server.Stdout)
go func() {
for scanner.Scan() {
text := scanner.Text()
fmt.Println(text)
server.OutputBuffer.Value = text
server.OutputBuffer = server.OutputBuffer.Next()
}
}()
server.Process.Stderr = os.Stderr
server.Running = true
// Call Run in a goroutine
// Start doesn't work as it doesn't set the ProcessState correctly
go func() {
// Process.Run below calls Wait internally
// However, only 1 wait can be used at the same time
// so if we want 'Restart' to work correctly,
// we can't use 'Wait' in 'Stop'
// Our solution is to abuse a mutex as a condition variable
server.WaitMutex.Lock()
defer server.WaitMutex.Unlock()
server.Process.Run()
}()
return nil
}
func (server *MTAServer) Stop(wait bool) error {
if server.Process == nil || server.Process.Process == nil || server.Running == false {
return errors.New("Process not started")
}
server.Running = false
err := server.Process.Process.Signal(os.Interrupt)
if err != nil {
return err
}
// Wait for the server to stop
if wait {
// This mutex is signalled when the server has stopped
server.WaitMutex.Lock()
server.WaitMutex.Unlock()
}
return nil
}
func (server *MTAServer) Restart() error {
// Send stop signal
err := server.Stop(true)
if err != nil {
return err
}
// Start server
return server.Start()
}
func (server *MTAServer) ExecCommand(command string) error {
_, err := io.WriteString(server.Stdin, command+"\n")
if err != nil {
fmt.Println(err)
}
return err
}
func (server *MTAServer) TailBuffer() string {
// Make string from output buffer
var buffer bytes.Buffer
server.OutputBuffer.Do(func(line interface{}) {
if line != nil {
buffer.WriteString(line.(string))
buffer.WriteString("\n")
}
})
return buffer.String()
}
func (server *MTAServer) WatchProcess() {
for {
// Wait a few seconds
time.Sleep(5 * time.Second)
// Don't start a server if none is running
if !server.Running || server.Process == nil || server.Process.ProcessState == nil {
continue
}
// Check if the process crashed
if server.Process.ProcessState.Exited() || !server.Process.ProcessState.Success() {
fmt.Println("\nA crash was detected. Attempting a restart...\n")
// Call stop to ensure everything is cleaned up correctly
// but ignore errors
server.Stop(true)
// A crash/unexpected exit has been detected, so restart now
err := server.Start()
// But don't repeat if startup failed
if err != nil {
break
}
}
}
}
func (server *MTAServer) Status() *MTAStatusInfoMessage {
message := MTAStatusInfoMessage{Running: server.Running}
message.Process = fmt.Sprintf("%x", server.Process)
if server.Process != nil {
message.ProcessProcess = fmt.Sprintf("%x", server.Process.Process)
message.ProcessStatus = server.Process.ProcessState.String()
}
return &message
}