-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
142 lines (125 loc) · 4.04 KB
/
index.js
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
const MessageTypes = require('./message-types')
const WebSocketChannel = require('./websocket-channel')
const ErrorTypes = require('./error-types')
class MonitoringService {
constructor(monitoringServerUrl, serviceToken, statsDataSource, statsSyncTimeout = 1000, verbose = false) {
if (!monitoringServerUrl)
throw new Error('monitoringServerUrl is required')
if (!serviceToken)
throw new Error('serviceToken is required')
if (!statsDataSource)
throw new Error('statsDataSource is required')
this.wsChannel = new WebSocketChannel({
url: monitoringServerUrl,
serviceToken,
onOpen: () => this.__onOpen(),
onMessage: (message) => this.__onMessage(message),
onClose: () => this.__onClose(),
onError: e => this.__onError(e)
})
this.statsDataSource = statsDataSource
this.statsSyncTimeout = statsSyncTimeout
this.__verbose = verbose
}
__log(...args) {
if (this.__verbose)
console.log(...args)
}
connect() {
if (this.wsChannel.isConnected)
return
this.wsChannel.connect()
this.__runStatiscticsWorker(1)
}
terminate() {
this.__timeoutId && clearTimeout(this.__timeoutId)
this.__timeoutId = undefined
this.wsChannel.close()
}
send(data, type = MessageTypes.LOG) {
if (!this.wsChannel.isConnected || !this.__timeoutId)
return
this.wsChannel.notify(JSON.stringify({
type,
data
}))
}
__runStatiscticsWorker(timeout = null) {
this.__timeoutId = setTimeout(
() => this.__sendStatisctics(),
timeout
)
}
__statsSendAttempts = 0
__sendStatisctics() {
try {
if (!this.wsChannel.isConnected || !this.__timeoutId) {
this.__incSendAttempts()
return
}
const statistics = this.statsDataSource()
//if data is empty, do not send it
if (!statistics) {
this.__incSendAttempts()
return
}
this.send({stats: statistics}, MessageTypes.LOG)
this.__resetSendAttempts()
} catch (e) {
this.__log('Error on log sending', e)
} finally {
if (this.__timeoutId)
this.__runStatiscticsWorker(this.__getTimeout())
}
}
__incSendAttempts() {
//restrict count to avoid huge timeouts
if (this.__statsSendAttempts < 5)
this.__statsSendAttempts++
}
__resetSendAttempts() {
if (this.__statsSendAttempts > 0)
this.__statsSendAttempts = 0
}
__getTimeout() {
if (this.__statsSendAttempts === 0)
return this.statsSyncTimeout
const timeout = Math.pow(2, this.__statsSendAttempts) * 1000
if (timeout > this.statsSyncTimeout)
return this.statsSyncTimeout
return timeout
}
__onMessage(message) {
switch (message.type) {
case MessageTypes.RESULT:
this.__log(`${this.name} received result`, message.data)
break
default:
this.__log('unknown message type', message.type, message.data)
}
}
__onOpen() {
try {
this.wsChannel.notify(JSON.stringify({
type: MessageTypes.SETTINGS,
data: {
serviceTimeout: this.statsSyncTimeout * 2
}
}))
} catch (e) {
this.__log('Error on settings sending', e)
}
}
__onClose() {
this.__log('Monitoring server connection closed')
}
__onError(error) {
if (error.code === 'ECONNREFUSED') {
if (error.connectionAttempts === 1)
console.error('Connection refused.')
} else {
console.error('Error occured. ', error)
}
}
}
module.exports = {MonitoringService, ErrorTypes}