actionhero
Version:
actionhero.js is a multi-transport API Server with integrated cluster capabilities and delayed tasks
105 lines (93 loc) • 2.98 kB
JavaScript
var cluster = require('cluster');
var readline = require('readline');
exports.start = function(binary, next){
var ActionheroPrototype = require(binary.paths.actionheroRoot + '/actionhero.js').actionheroPrototype;
// var actionheroPrototype = require('./../../actionhero.js').actionheroPrototype;
var actionhero = new ActionheroPrototype();
// number of ms to wait to do a forcible shutdown if actionhero won't stop gracefully
var shutdownTimeout = 1000 * 30;
if(process.env.ACTIONHERO_SHUTDOWN_TIMEOUT){
shutdownTimeout = parseInt(process.env.ACTIONHERO_SHUTDOWN_TIMEOUT)
}
var api = {};
var state;
var startServer = function(next){
state = 'starting';
if(cluster.isWorker){ process.send(state); }
actionhero.start(function(err, apiFromCallback){
if(err){
binary.log(err);
process.exit(1);
} else {
state = 'started';
if(cluster.isWorker){ process.send(state); }
api = apiFromCallback;
checkForInernalStop();
if(typeof next === 'function'){
next(api);
}
}
});
}
var stopServer = function(next){
state = 'stopping';
if(cluster.isWorker){ process.send(state); }
actionhero.stop(function(){
state = 'stopped';
if(cluster.isWorker){ process.send(state); }
api = null;
if(typeof next === 'function'){ next(api); }
});
}
var restartServer = function(next){
state = 'restarting';
if(cluster.isWorker){ process.send(state); }
actionhero.restart(function(err, apiFromCallback){
state = 'restarted';
if(cluster.isWorker){ process.send(state); }
api = apiFromCallback;
if(typeof next === 'function'){ next(api); }
});
}
var stopProcess = function(){
setTimeout(function(){
process.exit(1);
}, shutdownTimeout)
// finalTimer.unref();
stopServer(function(){
process.nextTick(function(){
process.exit();
});
});
}
var checkForInernalStopTimer;
var checkForInernalStop = function(){
clearTimeout(checkForInernalStopTimer);
if(actionhero.api.running !== true){
process.exit(0);
}
checkForInernalStopTimer = setTimeout(checkForInernalStop, shutdownTimeout);
}
if(cluster.isWorker){
process.on('message', function(msg){
if(msg === 'start'){ startServer() }
else if(msg === 'stop'){ stopServer() }
else if(msg === 'stopProcess'){ stopProcess() }
else if(msg === 'restart'){ restartServer() }
});
}
process.on('SIGINT', function(){ stopProcess() });
process.on('SIGTERM', function(){ stopProcess() });
process.on('SIGUSR2', function(){ restartServer() });
if(process.platform === 'win32' && !process.env.IISNODE_VERSION){
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('SIGINT', function(){
process.emit('SIGINT');
});
}
// start the server!
startServer(next);
}