forked from iloire/watchmen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-monitor-server.js
52 lines (42 loc) · 1.47 KB
/
run-monitor-server.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
var colors = require('colors');
var program = require('commander');
var pluginLoader = require('./lib/plugin-loader');
var storageFactory = require('./lib/storage/storage-factory');
var WatchMenFactory = require('./lib/watchmen');
var sentinelFactory = require('./lib/sentinel');
var RETURN_CODES = {
OK: 0,
BAD_STORAGE: 1,
GENERIC_ERROR: 2
};
function exit(code) {
storage.quit();
process.exit(code);
}
program
.option('-e, --env [env]', 'Storage environment key', process.env.NODE_ENV || 'development')
.option('-d, --max-initial-delay [value]', 'Initial random delay max bound', 20000)
.parse(process.argv);
var storage = storageFactory.getStorageInstance(program.env);
if (!storage) {
console.error('Error creating storage for env: ', program.env);
return process.exit(RETURN_CODES.BAD_STORAGE);
}
storage.getServices({}, function (err, services) {
if (err) {
console.error('error loading services'.red);
console.error(err);
return exit(RETURN_CODES.GENERIC_ERROR);
}
var watchmen = new WatchMenFactory(services, storage);
pluginLoader.loadPlugins(watchmen, {}, function(){
watchmen.startAll({randomDelayOnInit: program.maxInitialDelay});
console.log('\nwatchmen has started. ' + services.length + ' services loaded\n');
var sentinel = new sentinelFactory(storage, watchmen, {interval: 10000});
sentinel.watch();
});
});
process.on('SIGINT', function () {
console.log('stopping watchmen..'.gray);
exit(RETURN_CODES.OK);
});