|
|
@ -6,99 +6,15 @@ var nconf = require('nconf'),
|
|
|
|
async = require('async'),
|
|
|
|
async = require('async'),
|
|
|
|
pidFilePath = __dirname + '/pidfile',
|
|
|
|
pidFilePath = __dirname + '/pidfile',
|
|
|
|
output = fs.openSync(__dirname + '/logs/output.log', 'a'),
|
|
|
|
output = fs.openSync(__dirname + '/logs/output.log', 'a'),
|
|
|
|
numCPUs = require('os').cpus().length;
|
|
|
|
numCPUs = require('os').cpus().length,
|
|
|
|
|
|
|
|
Loader = {
|
|
|
|
/* TODO
|
|
|
|
timesStarted: 0,
|
|
|
|
* pidFile and reset timer
|
|
|
|
shutdown_queue: [],
|
|
|
|
* logging
|
|
|
|
js: {
|
|
|
|
* restart signal from child
|
|
|
|
cache: undefined,
|
|
|
|
*/
|
|
|
|
map: undefined
|
|
|
|
|
|
|
|
}
|
|
|
|
// pidFilePath = __dirname + '/pidfile',
|
|
|
|
};
|
|
|
|
// output = fs.openSync(__dirname + '/logs/output.log', 'a'),
|
|
|
|
|
|
|
|
// start = function() {
|
|
|
|
|
|
|
|
// var nbb_start = function(callback) {
|
|
|
|
|
|
|
|
// if (timesStarted > 3) {
|
|
|
|
|
|
|
|
// console.log('\n[loader] Experienced three start attempts in 10 seconds, most likely an error on startup. Halting.');
|
|
|
|
|
|
|
|
// return nbb_stop();
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// timesStarted++;
|
|
|
|
|
|
|
|
// if (startTimer) {
|
|
|
|
|
|
|
|
// clearTimeout(startTimer);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// startTimer = setTimeout(resetTimer, 1000*10);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if (nbb) {
|
|
|
|
|
|
|
|
// nbbOld = nbb;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// nbb = require('child_process').fork('./app', process.argv.slice(2), {
|
|
|
|
|
|
|
|
// env: process.env
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// nbb.on('message', function(message) {
|
|
|
|
|
|
|
|
// if (message && typeof message === 'object' && message.action) {
|
|
|
|
|
|
|
|
// switch (message.action) {
|
|
|
|
|
|
|
|
// case 'ready':
|
|
|
|
|
|
|
|
// if (!callback) return nbb.send('bind');
|
|
|
|
|
|
|
|
// callback();
|
|
|
|
|
|
|
|
// break;
|
|
|
|
|
|
|
|
// case 'restart':
|
|
|
|
|
|
|
|
// nbb_restart();
|
|
|
|
|
|
|
|
// break;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// nbb.on('exit', function(code, signal) {
|
|
|
|
|
|
|
|
// if (code) {
|
|
|
|
|
|
|
|
// nbb_start();
|
|
|
|
|
|
|
|
// } else {
|
|
|
|
|
|
|
|
// nbb_stop();
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
// },
|
|
|
|
|
|
|
|
// nbb_stop = function() {
|
|
|
|
|
|
|
|
// if (startTimer) {
|
|
|
|
|
|
|
|
// clearTimeout(startTimer);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// nbb.kill();
|
|
|
|
|
|
|
|
// if (fs.existsSync(pidFilePath)) {
|
|
|
|
|
|
|
|
// var pid = parseInt(fs.readFileSync(pidFilePath, { encoding: 'utf-8' }), 10);
|
|
|
|
|
|
|
|
// if (process.pid === pid) {
|
|
|
|
|
|
|
|
// fs.unlinkSync(pidFilePath);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// },
|
|
|
|
|
|
|
|
// nbb_restart = function() {
|
|
|
|
|
|
|
|
// nbb_start(function() {
|
|
|
|
|
|
|
|
// nbbOld.removeAllListeners('exit').on('exit', function() {
|
|
|
|
|
|
|
|
// nbb.send('bind');
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
// nbbOld.kill();
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
// },
|
|
|
|
|
|
|
|
// resetTimer = function() {
|
|
|
|
|
|
|
|
// clearTimeout(startTimer);
|
|
|
|
|
|
|
|
// timesStarted = 0;
|
|
|
|
|
|
|
|
// },
|
|
|
|
|
|
|
|
// timesStarted = 0,
|
|
|
|
|
|
|
|
// startTimer;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// process.on('SIGINT', nbb_stop);
|
|
|
|
|
|
|
|
// process.on('SIGTERM', nbb_stop);
|
|
|
|
|
|
|
|
// process.on('SIGHUP', nbb_restart);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// nbb_start();
|
|
|
|
|
|
|
|
// },
|
|
|
|
|
|
|
|
// nbb, nbbOld;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var Loader = {
|
|
|
|
|
|
|
|
timesStarted: 0,
|
|
|
|
|
|
|
|
shutdown_queue: []
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Loader.init = function() {
|
|
|
|
Loader.init = function() {
|
|
|
|
cluster.setupMaster({
|
|
|
|
cluster.setupMaster({
|
|
|
@ -111,7 +27,14 @@ Loader.init = function() {
|
|
|
|
if (message && typeof message === 'object' && message.action) {
|
|
|
|
if (message && typeof message === 'object' && message.action) {
|
|
|
|
switch (message.action) {
|
|
|
|
switch (message.action) {
|
|
|
|
case 'ready':
|
|
|
|
case 'ready':
|
|
|
|
console.log('[cluster] Child Process (' + worker.process.pid + ') listening for connections.');
|
|
|
|
if (Loader.js.cache) {
|
|
|
|
|
|
|
|
worker.send({
|
|
|
|
|
|
|
|
action: 'js-propagate',
|
|
|
|
|
|
|
|
cache: Loader.js.cache,
|
|
|
|
|
|
|
|
map: Loader.js.map
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
worker.send('bind');
|
|
|
|
worker.send('bind');
|
|
|
|
|
|
|
|
|
|
|
|
// Kill an instance in the shutdown queue
|
|
|
|
// Kill an instance in the shutdown queue
|
|
|
@ -131,6 +54,9 @@ Loader.init = function() {
|
|
|
|
Loader.reload();
|
|
|
|
Loader.reload();
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 'js-propagate':
|
|
|
|
case 'js-propagate':
|
|
|
|
|
|
|
|
Loader.js.cache = message.cache;
|
|
|
|
|
|
|
|
Loader.js.map = message.map;
|
|
|
|
|
|
|
|
|
|
|
|
var otherWorkers = Object.keys(cluster.workers).filter(function(worker_id) {
|
|
|
|
var otherWorkers = Object.keys(cluster.workers).filter(function(worker_id) {
|
|
|
|
return parseInt(worker_id, 10) !== parseInt(worker.id, 10);
|
|
|
|
return parseInt(worker_id, 10) !== parseInt(worker.id, 10);
|
|
|
|
});
|
|
|
|
});
|
|
|
@ -141,7 +67,6 @@ Loader.init = function() {
|
|
|
|
map: message.map
|
|
|
|
map: message.map
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 'user:connect':
|
|
|
|
case 'user:connect':
|
|
|
|
case 'user:disconnect':
|
|
|
|
case 'user:disconnect':
|
|
|
@ -152,6 +77,10 @@ Loader.init = function() {
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cluster.on('listening', function(worker) {
|
|
|
|
|
|
|
|
console.log('[cluster] Child Process (' + worker.process.pid + ') listening for connections.');
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
function notifyWorkers(currentWorker, msg) {
|
|
|
|
function notifyWorkers(currentWorker, msg) {
|
|
|
|
Object.keys(cluster.workers).forEach(function(id) {
|
|
|
|
Object.keys(cluster.workers).forEach(function(id) {
|
|
|
|
cluster.workers[id].send(msg);
|
|
|
|
cluster.workers[id].send(msg);
|
|
|
@ -184,7 +113,6 @@ Loader.init = function() {
|
|
|
|
process.on('SIGHUP', Loader.restart);
|
|
|
|
process.on('SIGHUP', Loader.restart);
|
|
|
|
|
|
|
|
|
|
|
|
Loader.start();
|
|
|
|
Loader.start();
|
|
|
|
// fs.writeFile(__dirname + '/pidfile', process.pid);
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Loader.start = function() {
|
|
|
|
Loader.start = function() {
|
|
|
@ -228,28 +156,4 @@ if (nconf.get('daemon') !== false) {
|
|
|
|
fs.writeFile(__dirname + '/pidfile', process.pid);
|
|
|
|
fs.writeFile(__dirname + '/pidfile', process.pid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Loader.init();
|
|
|
|
Loader.init();
|
|
|
|
|
|
|
|
|
|
|
|
// Start the daemon!
|
|
|
|
|
|
|
|
// if (nconf.get('daemon') !== false) {
|
|
|
|
|
|
|
|
// // Check for a still-active NodeBB process
|
|
|
|
|
|
|
|
// if (fs.existsSync(pidFilePath)) {
|
|
|
|
|
|
|
|
// try {
|
|
|
|
|
|
|
|
// var pid = fs.readFileSync(pidFilePath, { encoding: 'utf-8' });
|
|
|
|
|
|
|
|
// process.kill(pid, 0);
|
|
|
|
|
|
|
|
// process.exit();
|
|
|
|
|
|
|
|
// } catch (e) {
|
|
|
|
|
|
|
|
// fs.unlinkSync(pidFilePath);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // Daemonize and record new pid
|
|
|
|
|
|
|
|
// require('daemon')({
|
|
|
|
|
|
|
|
// stdout: output
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
// fs.writeFile(__dirname + '/pidfile', process.pid);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// start();
|
|
|
|
|
|
|
|
// } else {
|
|
|
|
|
|
|
|
// start();
|
|
|
|
|
|
|
|
// }
|
|
|
|
|