-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatchdog.go
255 lines (235 loc) · 7.73 KB
/
watchdog.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/**
* (C) 2021 Mitsubishi Electrics Automotive
* (C) 2020 Geotab Inc
*
* All files and artifacts in the repository at https://github.com/MEAE-GOT/WAII
* are licensed under the provisions of the license provided by the LICENSE file in this repository.
*
**/
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"net/url"
"strconv"
"time"
"github.com/akamensky/argparse"
"github.com/gorilla/websocket"
"github.com/josesnchz/WAII/utils"
)
const portNum = 8080 // WS mgr portnum
const urlPath = ""
const subscribeCommand = `{"action":"subscribe", "path":"Vehicle/Cabin/Door/Count", "filter":"$intervalEQ5", "requestId":"999"}`
const subscribePeriod = 5 // set to value X set in "$intervalEQX" above
var addr *string
func initSubscribeSession(addr *string) *websocket.Conn {
dataSessionUrl := url.URL{Scheme: "ws", Host: *addr, Path: urlPath}
dataConn, _, err := websocket.DefaultDialer.Dial(dataSessionUrl.String(), nil)
if err != nil {
utils.Error.Fatal("Data session dial error:" + err.Error())
} else {
err = dataConn.WriteMessage(websocket.TextMessage, []byte(subscribeCommand))
if err != nil {
utils.Warning.Println("Watchdog subscribe error:" + err.Error())
dataConn = nil
}
}
return dataConn
}
func receiveSubscriptions(dataConn *websocket.Conn, triggerChannel chan string) {
for {
dataConn.SetReadDeadline(time.Now().Add(2 * subscribePeriod * time.Second))
_, response, err := dataConn.ReadMessage()
if err != nil {
utils.Error.Println("Watchdog read error:", err)
return
}
// utils.Info.Println("Watchdog subscription received.Message=%s", response)
triggerChannel <- string(response)
}
}
func executeStopme() {
// out, err := exec.Command("/bin/sh", "-c", "./W3CServer.sh", "stopme").CombinedOutput()
out, err := exec.Command("./W3CServer.sh", "stopme").CombinedOutput()
if err != nil {
utils.Error.Printf("Script execute error:%s", string(out))
utils.Error.Printf("Script execute error:%v", err)
utils.Error.Println("Script execute error command: stopme")
} else {
utils.Info.Println("Script stopme executed successfully.")
time.Sleep(5 * time.Second)
}
}
func executeNoScriptStartme() {
res := startServerCore()
if res == true {
time.Sleep(5 * time.Second)
res := startServiceManager()
if res == true {
time.Sleep(2 * time.Second)
res := startWSManager()
if res == true {
time.Sleep(2 * time.Second)
res := startHTTPManager()
if res == true {
time.Sleep(2 * time.Second)
res := startAGTServer()
if res == true {
time.Sleep(2 * time.Second)
res := startATServer()
if res == true {
utils.Info.Println("Script startme executed successfully.")
time.Sleep(10 * time.Second)
}
}
}
}
}
}
}
func startServerCore() bool {
out, err := exec.Command("screen", "-d", "-m", "-t", "serverCore", "sh", "servercore.sh").CombinedOutput()
if err != nil {
utils.Error.Printf("Script execute error:%s", string(out))
utils.Error.Printf("Script execute error:%v", err)
utils.Error.Println("Script execute error command: screen -d -m -S serverCore")
} else {
time.Sleep(1 * time.Second)
utils.Info.Println("ServerCore started successfully.")
return true
}
return false
}
func startServiceManager() bool {
out, err := exec.Command("screen", "-d", "-m", "-t", "serviceMgr", "sh", "servicemanager.sh").CombinedOutput()
if err != nil {
utils.Error.Printf("Script execute error:%s", string(out))
utils.Error.Printf("Script execute error:%v", err)
utils.Error.Println("Script execute error command: screen -d -m -S serviceMgr")
} else {
time.Sleep(1 * time.Second)
utils.Info.Println("Service Manager started successfully.")
return true
}
return false
}
func startWSManager() bool {
out, err := exec.Command("screen", "-d", "-m", "-t", "wsMgr", "sh", "wsmanager.sh").CombinedOutput()
if err != nil {
utils.Error.Printf("Script execute error:%s", string(out))
utils.Error.Printf("Script execute error:%v", err)
utils.Error.Println("Script execute error command: screen -d -m -S wsMgr")
} else {
time.Sleep(1 * time.Second)
utils.Info.Println("WebSocket Manager started successfully.")
return true
}
return false
}
func startHTTPManager() bool {
out, err := exec.Command("screen", "-d", "-m", "-t", "httpMgr", "sh", "httpmanager.sh").CombinedOutput()
if err != nil {
utils.Error.Printf("Script execute error:%s", string(out))
utils.Error.Printf("Script execute error:%v", err)
utils.Error.Println("Script execute error command: screen -d -m -S httpMgr")
} else {
time.Sleep(1 * time.Second)
utils.Info.Println("HTTP Manager started successfully.")
return true
}
return false
}
func startAGTServer() bool {
out, err := exec.Command("screen", "-d", "-m", "-t", "agtServer", "sh", "agtserver.sh").CombinedOutput()
if err != nil {
utils.Error.Printf("Script execute error:%s", string(out))
utils.Error.Printf("Script execute error:%v", err)
utils.Error.Println("Script execute error command: screen -d -m -S agtServer")
} else {
time.Sleep(1 * time.Second)
utils.Info.Println("AGT server started successfully.")
return true
}
return false
}
func startATServer() bool {
out, err := exec.Command("screen", "-d", "-m", "-t", "atServer", "sh", "atserver.sh").CombinedOutput()
if err != nil {
utils.Error.Printf("Script execute error:%s", string(out))
utils.Error.Printf("Script execute error:%v", err)
utils.Error.Println("Script execute error command: screen -d -m -S atServer")
} else {
time.Sleep(1 * time.Second)
utils.Info.Println("AT server started successfully.")
return true
}
return false
}
/**
* restartServer:
* 1. call stopme script. 2. call startme script 3. wait 30 secs 4. kill receiveSubscriptions()
* 5. rerun initSubscribeSession 6. start receiveSubscriptions() 7. set wdTimer
**/
func restartServer(addr *string, triggerChannel chan string) (*websocket.Conn, *time.Timer) {
executeStopme()
executeNoScriptStartme()
time.Sleep(10 * time.Second)
// receiveSubscriptions() killed by its own timeout
dataConn := initSubscribeSession(addr)
if dataConn != nil {
go receiveSubscriptions(dataConn, triggerChannel)
}
wdTimer := time.NewTimer(2 * subscribePeriod * time.Second)
return dataConn, wdTimer
}
func main() {
// Create new parser object
parser := argparse.NewParser("print", "watchdog")
// Create string flag
logFile := parser.Flag("", "logfile", &argparse.Options{Required: false, Help: "outputs to logfile in ./logs folder"})
logLevel := parser.Selector("", "loglevel", []string{"trace", "debug", "info", "warn", "error", "fatal", "panic"}, &argparse.Options{
Required: false,
Help: "changes log output level",
Default: "info"})
ipAddr := parser.String("", "ip", &argparse.Options{
Required: true,
Help: "Ip adress "})
// Parse input
err := parser.Parse(os.Args)
if err != nil {
fmt.Print(parser.Usage(err))
}
if len(*ipAddr) < 10 {
utils.Error.Println("Program must be provided server IP address as input.")
return
}
triggerChan := make(chan string)
utils.InitLog("watchdog-log.txt", "./logs", *logFile, *logLevel)
addr = flag.String("addr", *ipAddr+":"+strconv.Itoa(portNum), "http service address")
dataConn := initSubscribeSession(addr)
if dataConn == nil {
return
}
go receiveSubscriptions(dataConn, triggerChan)
wdTimer := time.NewTimer(2 * subscribePeriod * time.Second)
for {
select {
case <-triggerChan:
wdTimer.Stop()
wdTimer = time.NewTimer(2 * subscribePeriod * time.Second)
case <-wdTimer.C:
//fmt.Println("Gen2 server crashed, restart initiated.")
utils.Error.Println("Gen2 server crashed, restart initiated.")
dataConn.Close()
dataConn, wdTimer = restartServer(addr, triggerChan)
if dataConn == nil {
utils.Error.Println("Gen2 server failed to restart.")
return
}
default:
time.Sleep(1 * time.Second)
}
}
}