-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.go
189 lines (170 loc) · 3.83 KB
/
core.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
package main
import (
"encoding/json"
"errors"
"github.com/gorilla/websocket"
"log"
tests "netip-core/benchmark"
"netip-core/collector"
"netip-core/info"
"os"
"os/signal"
"time"
)
type connectResponse struct {
responseBase
}
type connectPayload struct {
payloadBase
Info *info.Info `json:"info"`
}
func main() {
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
destroy := make(chan bool, 1)
connect(&connectPayload{
payloadBase: payloadBase{
Service: "core",
},
Info: info.Get(),
})
col := collector.New()
chGeneralTests := make(chan *tests.Result)
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
// reader from nodes-handler
go func() {
for {
if wsConnect == nil {
time.Sleep(time.Second)
continue
}
// ReadMessage needs for ping-pong
code, message, err := wsConnect.ReadMessage()
if err != nil {
if !websocket.IsCloseError(err, 1006) {
log.Println("ws read err:", err, code)
}
time.Sleep(time.Second)
continue
}
var res struct {
Command string `json:"command"`
Runtime int `json:"runtime"`
}
err = json.Unmarshal(message, &res)
if err != nil {
log.Println("ws rx unmarshal err:", err)
continue
}
switch res.Command {
case "general-tests":
go tests.NewGeneralTests(false, res.Runtime, chGeneralTests)
case "services-destroy":
destroy <- true
return
}
}
}()
log.Println("ready to work")
// writer to nodes-handler
for {
select {
case <-destroy:
log.Println("service destroyed")
log.Println("------")
log.Println("below remains to execution manually:")
log.Println("# docker rm -f netip.core")
log.Println("------")
_ = wsConnect.Close()
<-destroy
// ticker-sender stats core
case <-ticker.C:
if wsConnect == nil {
continue
}
j := struct {
Event string `json:"event"`
CollectCore collector.CollectCore `json:"collectCore"`
}{
Event: "collect-core",
CollectCore: col.Get(),
}
err := wsConnect.WriteJSON(j)
if err != nil {
// ! lock this select before established connection
connectDegrade(errors.New("ticker write err: " + err.Error()))
continue
}
// chan-sender who logged terminals
case wl, ok := <-col.ChanWhoLogged:
if wsConnect == nil || !ok {
continue
}
j := struct {
Event string `json:"event"`
WhoLogged *collector.WhoLogged `json:"whoLogged"`
}{
Event: "who-logged",
WhoLogged: wl,
}
err := wsConnect.WriteJSON(j)
if err != nil {
log.Println("who logged write err:", err)
continue
}
// chan-sender processes
case ps, ok := <-col.ChanProcesses:
if wsConnect == nil || !ok {
continue
}
j := struct {
Event string `json:"event"`
Processes *collector.Processes `json:"processes"`
}{
Event: "processes",
Processes: ps,
}
err := wsConnect.WriteJSON(j)
if err != nil {
log.Println("processes write err:", err)
continue
}
// chan-sender general-test
case gt, ok := <-chGeneralTests:
if wsConnect == nil || !ok {
continue
}
j := struct {
Event string `json:"event"`
BmsTests *tests.Result `json:"bmsTests"`
}{
Event: "bms-general-tests",
BmsTests: gt,
}
err := wsConnect.WriteJSON(j)
if err != nil {
log.Println("general-test write err:", err)
continue
}
// handler terminate
case <-interrupt:
log.Println("interrupt")
if wsConnect == nil {
return
}
err := wsConnect.WriteMessage(
websocket.CloseMessage,
websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
if err != nil {
log.Println("write close err:", err)
return
}
err = wsConnect.Close()
if err != nil {
log.Println("close conn err:", err)
}
return
}
}
}