-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathmonitorAgent.js
More file actions
executable file
·221 lines (200 loc) · 5.61 KB
/
Copy pathmonitorAgent.js
File metadata and controls
executable file
·221 lines (200 loc) · 5.61 KB
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
var sclient = require('socket.io-client');
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var utils = require('./util/utils');
var protocol = require('./util/protocol');
var logger = require('pomelo-logger').getLogger('pomelo-admin', __filename);
var ST_INITED = 1;
var ST_CONNECTED = 2;
var ST_REGISTERED = 3;
var ST_CLOSED = 4;
var STATUS_INTERVAL = 5 * 1000; // 60 seconds
/**
* MonitorAgent Constructor
*
* @class MasterAgent
* @constructor
* @param {Object} opts construct parameter
* opts.consoleService {Object} consoleService
* opts.id {String} server id
* opts.type {String} server type, 'master', 'connector', etc.
* opts.info {Object} more server info for current server, {id, serverType, host, port}
* @api public
*/
var MonitorAgent = function(opts) {
EventEmitter.call(this);
this.consoleService = opts.consoleService;
this.id = opts.id;
this.type = opts.type;
this.info = opts.info;
this.socket = null;
this.reqId = 1;
this.callbacks = {};
this.state = ST_INITED;
};
util.inherits(MonitorAgent, EventEmitter);
module.exports = MonitorAgent;
/**
* register and connect to master server
*
* @param {String} port
* @param {String} host
* @param {Function} cb callback function
* @api public
*/
MonitorAgent.prototype.connect = function(port, host, cb) {
if (this.state > ST_INITED) {
logger.error('monitor client has connected or closed.');
return;
}
this.socket = sclient.connect(host + ':' + port, {'force new connection': true, 'reconnect': true, 'max reconnection attempts': 20});
var self = this;
this.socket.on('register', function(msg) {
if (msg && msg.code === protocol.PRO_OK) {
self.state = ST_REGISTERED;
utils.invokeCallback(cb);
} else {
self.emit('close');
logger.error('server %j %j register master failed', self.id, self.type);
utils.invokeCallback(cb, msg);
}
});
this.socket.on('monitor', function(msg) {
if (self.state !== ST_REGISTERED) {
return;
}
msg = protocol.parse(msg);
if (msg.command) {
// a command from master
self.consoleService.command(msg.command, msg.moduleId, msg.body, function(err, res) {
//notify should not have a callback
});
} else {
if (msg.respId) {
// a response from monitor
var cb = self.callbacks[msg.respId];
if (!cb) {
logger.warn('unknown resp id:' + msg.respId);
return;
}
delete self.callbacks[msg.respId];
utils.invokeCallback(cb, msg.error, msg.body);
return;
}
// request from master
self.consoleService.execute(msg.moduleId, 'monitorHandler', msg.body, function(err, res) {
if (protocol.isRequest(msg)) {
var resp = protocol.composeResponse(msg, err, res);
if (resp) {
self.socket.emit('monitor', resp);
}
} else {
//notify should not have a callback
logger.error('notify should not have a callback.');
}
});
}
});
this.socket.on('connect', function() {
if (self.state > ST_INITED) {
//ignore reconnect
return;
}
self.state = ST_CONNECTED;
var req = {
id: self.id,
type: 'monitor',
serverType: self.type,
pid: process.pid,
info: self.info
};
var authServer = self.consoleService.authServer;
var env = self.consoleService.env;
authServer(req, env, function(token){
req['token'] = token;
self.socket.emit('register', req);
});
});
this.socket.on('error', function(err) {
if (self.state < ST_CONNECTED) {
// error occurs during connecting stage
utils.invokeCallback(cb, err);
} else {
self.emit('error', err);
}
});
this.socket.on('disconnect', function(reason) {
self.state = ST_CLOSED;
self.emit('close');
});
this.socket.on('reconnect', function(){
self.state = ST_CONNECTED;
var req = {
id: self.id,
type: 'monitor',
serverType: self.type,
pid: process.pid,
info: self.info
};
self.socket.emit('reconnect', req, process.pid);
});
this.socket.on('reconnect_ok', function(msg){
if(msg && msg.code === protocol.PRO_OK) {
self.state = ST_REGISTERED;
}
});
};
/**
* close monitor agent
*
* @api public
*/
MonitorAgent.prototype.close = function() {
if (this.state >= ST_CLOSED) {
return;
}
this.state = ST_CLOSED;
this.socket.disconnect();
};
/**
* set module
*
* @param {String} moduleId module id/name
* @param {Object} value module object
* @api public
*/
MonitorAgent.prototype.set = function(moduleId, value) {
this.consoleService.set(moduleId, value);
};
/**
* get module
*
* @param {String} moduleId module id/name
* @api public
*/
MonitorAgent.prototype.get = function(moduleId) {
return this.consoleService.get(moduleId);
};
/**
* notify master server without callback
*
* @param {String} moduleId module id/name
* @param {Object} msg message
* @api public
*/
MonitorAgent.prototype.notify = function(moduleId, msg) {
if (this.state !== ST_REGISTERED) {
logger.error('agent can not notify now, state:' + this.state);
return;
}
this.socket.emit('monitor', protocol.composeRequest(null, moduleId, msg));
};
MonitorAgent.prototype.request = function(moduleId, msg, cb) {
if (this.state !== ST_REGISTERED) {
logger.error('agent can not request now, state:' + this.state);
return;
}
var reqId = this.reqId++;
this.callbacks[reqId] = cb;
this.socket.emit('monitor', protocol.composeRequest(reqId, moduleId, msg));
};