-
Notifications
You must be signed in to change notification settings - Fork 1
/
noderunner.js
executable file
·92 lines (78 loc) · 3.14 KB
/
noderunner.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
var MongoClient = require('mongodb').MongoClient,
nconf = require('nconf'),
Immediate = require('./lib/queue/immediate'),
Planned = require('./lib/queue/planned'),
History = require('./lib/queue/history'),
Gui = require('./lib/gui'),
Watchdog = require('./lib/watchdog');
// Config from ENV, CLI, default file and local file
nconf.argv().env().file('custom', {file: 'custom/config.json'}).file({file: 'defaults.json'}).defaults({'logLevel':'error'});
// Init logger
var logger = require('./lib/logger')(nconf.get('logLevel'));
var immediate, planned, history, watchdog, gui;
// Mongo connection check
var mongoTimeout;
function tryMongoConnection() {
var options = {
server: { reconnectTries: 100, reconnectInterval: 1000, socketOptions: { keepAlive: 1, connectTimeoutMS: 30000 } },
replset: { socketOptions: { keepAlive: 1, connectTimeoutMS: 30000 } }
};
MongoClient.connect(nconf.get('mongoDSN'), options, function(err, db){
if (err) {
logger.error('Mongo connection error, try in 10 secs. ', err);
clearTimeout(mongoTimeout);
mongoTimeout = setTimeout(tryMongoConnection, 3000)
} else {
logger.info('Connected to mongo queuerunner DB');
immediate = new Immediate(db, nconf, logger).run();
planned = new Planned(db, nconf, logger).run();
history = new History(db, nconf, logger).run();
watchdog = new Watchdog(db, nconf, logger).run(immediate);
if (nconf.get('gui:enable')) {
gui = new Gui(db, nconf, logger, {
immediate: immediate,
planned: planned,
history: history,
}, watchdog).run();
}
}
});
}
tryMongoConnection();
logger.on('logging', function (transport, level, msg, meta) {
if (level == 'error' && meta.message && meta.message == 'topology was destroyed') {
logger.warn('MONGO TOPOLOGY DESTRUCTION DETECTED - stopping queues and reconnecting mongo');
if (nconf.get('gui:enable')) {
gui.stop();
}
planned.stop();
history.stop();
watchdog.stop();
immediate.stop(function(){
logger.warn('IMMEDIATE: instance with broken mongo just stopped');
});
tryMongoConnection();
}
});
// Graceful restart handler
process.on( "SIGABRT", function() {
var timeout = nconf.get('gracefulShutdownTimeout');
logger.warn('SHUTDOWN: Graceful shutdown request detected. Stop queues and wait for '+timeout/1000+' seconds.');
if (nconf.get('gui:enable')) {
gui.stop();
}
planned.stop();
history.stop();
watchdog.stop();
immediate.stop(function(){
logger.warn('SHUTDOWN: Last thread finished. Exitting in 3 secs...');
// if some db query running, give it some time to finish
setTimeout(function(){
process.exit();
}, 3000);
});
setTimeout(function() {
logger.warn('SHUTDOWN: Graceful shutdown timeout exceeded. Exitting now...');
process.exit();
}, timeout);
});