From db65d6476f323ed2a143b62f860633ed1678a932 Mon Sep 17 00:00:00 2001 From: Patrick Nagurny Date: Mon, 24 Aug 2015 10:13:45 -0400 Subject: [PATCH] add ctrl-c behavior to daemon --- bin/start-libbitcoind.js | 31 ++++++++++++++++++++++++++++++- bin/start.js | 1 - 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/bin/start-libbitcoind.js b/bin/start-libbitcoind.js index a6299a33..a7f7299d 100644 --- a/bin/start-libbitcoind.js +++ b/bin/start-libbitcoind.js @@ -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})); \ No newline at end of file diff --git a/bin/start.js b/bin/start.js index 5579e310..9673da85 100644 --- a/bin/start.js +++ b/bin/start.js @@ -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}));