-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
269 lines (188 loc) · 7.67 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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
var agent = require('./agent'),
nats = require('nats').connect(),
os = require('os'),
fs = require('fs'),
path = require('path'),
respawn = require('respawn'),
humixLogger = require('humix-logger'),
async = require('async'),
config = require('./config');
var modules = {};
var statusCheckHandle;
var log;
var thinkURL;
var senseId;
/* Constants */
var STATUS_CHECK_INTERVAL = 3000;
var STATUS_CHECK_TIMEOUT = 5000;
humixSenseInit();
try {
agent.init(thinkURL, senseId, {autoreconnect: true, logger: log});
agent.start();
} catch (e) {
log.error('Error: '+e);
}
function humixSenseInit(){
var config;
var defaultConfigPath = path.resolve(os.homedir(), '.humix/config.js');
if(fs.existsSync(defaultConfigPath)){
defaultConfigFile = require(defaultConfigPath);
if(defaultConfigFile && defaultConfigFile['sense']){
config = defaultConfigFile['sense'];
}
}else{
config = require('./config.js');
}
thinkURL = config.thinkURL || process.exit(1);
senseId = config.senseId || 'humix-sense-default';
if ( config.log){
var logfile = config.log.filename || 'humix-sense.log';
var fileLevel = config.log.fileLevel || 'info';
var consoleLevel = config.log.consoleLevel || 'debug';
log = humixLogger.createLogger('Humix-Sense', {filename:logfile,
fileLevel: fileLevel,
consoleLevel:consoleLevel});
}else{
log = humixLogger.createLogger('Humix-Sense')
}
log.info("Init Humix Sense. Using Think URL:"+ thinkURL);
// starting core modules
var coreModulePath = path.join(__dirname,'modules/core/');
if(fs.existsSync(coreModulePath)){
fs.readdir(coreModulePath,function(err, coreModules){
if(err){
log.error('Failed to read core modules. Error:'+err);
return;
}
coreModules.map(function (m) {
return path.join(coreModulePath, m);
}).filter(function (m) {
return fs.statSync(m).isDirectory();
}).forEach(function (m) {
log.info("loading module : %s", m);
var p = respawn(['npm','start'],{cwd:m});
p.on('stdout', function(data) {
// sometimes sense module emit multiple JSON logs to this,
// so need split each JSON and output to file/console
data.toString().split('\n').forEach(function (e, i, a) {
try {
// quick quess if data is JSON object received from bunyan logging style
var m = JSON.parse(e);
log.debug(m.msg);
} catch (err) {
if (e.trim().length > 0) {
log.error(e);
}
}
});
});
p.on('stderr', function (data) {
data.toString().split('\n').forEach(function (e, i, a) {
try {
var m = JSON.parse(e);
log.error(m.msg);
} catch (err) {
if (e.trim().length > 0) {
log.error(e);
}
}
});
});
p.on('spawn', function () {
log.info('process for module ['+m+'] spawned')
});
p.on('exit', function (code, signal) {
log.error({msg: 'process exited, code: ' + code + ' signal: ' + signal});
});
p.start();
});
});
}
statusCheckHandle = startStatusCheck();
}
function startStatusCheck() {
return setInterval(function () {
var moduleStatus = [];
var myPromise = function (ms, module, callback) {
return new Promise(function(resolve, reject) {
callback(resolve, reject);
setTimeout(function() {
reject('Status Check promise timed out after ' + ms + ' ms');
}, ms);
});
}
async.eachSeries(Object.keys(modules), function (module, cb) {
log.debug('checking status of ' + module);
myPromise(STATUS_CHECK_TIMEOUT, module, function (resolve, reject) {
var t = 'humix.sense.mgmt.' + module + ".ping";
nats.request(t, null, { 'max': 1 }, function (res) {
resolve('success');
})
}).then(function (result) {
log.debug('connection with module ' + module + " succeed");
moduleStatus.push({ moduleId: module, status: 'connected' });
cb(null);
}).catch(function () {
log.error('connection with module ' + module + " failed");
moduleStatus.push({ moduleId: module, status: 'disconnected' });
cb(null);
})
}, function (err) {
agent.publish('humix-think', 'module.status', moduleStatus);
});
},STATUS_CHECK_INTERVAL);
}
agent.events.on('module.command', function(data) {
log.info('Command: '+JSON.stringify(data));
var module = data.commandType;
var command = data.commandName;
var topic = 'humix.sense.'+module+'.command.'+command;
log.debug('topic: '+topic + ', data: '+JSON.stringify(data.commandData));
if(modules.hasOwnProperty(module) && modules[module].commands.indexOf(command) != -1 ){
if (data.syncCmdId) {
// TODO: handle timeout here
data.commandData.syncCmdId = data.syncCmdId;
nats.request(topic, JSON.stringify(data.commandData), { 'max': 1 }, function (res) {
agent.publish_syncResult(data.syncCmdId, res);
})
} else {
nats.publish(topic, JSON.stringify(data.commandData));
}
log.debug('publish command:'+command+", module:"+module);
}else{
log.info('skip command');
}
});
// handle module registration
nats.subscribe('humix.sense.mgmt.register', function(request, replyto){
log.info("Receive registration :"+ request);
var requestModule = JSON.parse(request);
if(modules.hasOwnProperty(requestModule.moduleName)){
log.info('Module [' + requestModule.moduleName + '] already register. Skip');
nats.publish(replyto,'module already registered');
return;
}
modules[requestModule.moduleName] = requestModule;
var eventPrefix = 'humix.sense.'+requestModule.moduleName+".event";
for ( var i in requestModule.events){
var event = requestModule.events[i];
var module = requestModule.moduleName;
var topic = eventPrefix + "." + event;
log.info("subscribing topic:"+ topic);
(function(topic,module,event){
nats.subscribe(topic, function(data){
log.debug('about to publish topic:'+topic+", data:"+data);
agent.publish(module, event, data);
});
})(topic,module,event);
}
// register the module to humix-think
agent.publish('humix-think', 'registerModule', requestModule);
log.debug('current modules:'+JSON.stringify(modules));
nats.publish(replyto,'module registration succeed');
});
process.on('SIGTERM', function() {
if (agent.getState() === 'RUNNING') {
agent.stop();
}
});