add ctrl-c behavior to daemon

This commit is contained in:
Patrick Nagurny 2015-08-24 10:13:45 -04:00
parent a9e5ee6f1a
commit db65d6476f
2 changed files with 30 additions and 2 deletions

View File

@ -15,7 +15,7 @@ var daemon = require('../').daemon({
network: process.env.BITCORENODE_NETWORK || 'livenet'
});
daemon.on('ready', function() {
daemon.start(function() {
log.info('ready');
});
@ -26,3 +26,32 @@ daemon.on('error', function(err) {
daemon.on('open', function(status) {
log.info('status="%s"', status);
});
function exitHandler(options, err) {
log.info('Stopping daemon');
if (err) {
log.error('uncaught exception:', err);
if(err.stack) {
console.log(err.stack);
}
process.exit(-1);
}
if (options.sigint) {
daemon.stop(function(err) {
if(err) {
log.error('Failed to stop services: ' + err);
return process.exit(1);
}
log.info('Halted');
process.exit(0);
});
}
}
//catches uncaught exceptions
process.on('uncaughtException', exitHandler.bind(null, {exit:true}));
//catches ctrl+c event
process.on('SIGINT', exitHandler.bind(null, {sigint:true}));

View File

@ -172,5 +172,4 @@ function exitHandler(options, err) {
process.on('uncaughtException', exitHandler.bind(null, {exit:true}));
//catches ctrl+c event
process.on('exit', exitHandler.bind(null, {exit: true}));
process.on('SIGINT', exitHandler.bind(null, {sigint:true}));