handle signals and stop correctly

This commit is contained in:
Patrick Nagurny 2015-08-21 15:39:46 -04:00
parent 164a2cad75
commit b0ab45f3f7
3 changed files with 16 additions and 26 deletions

View File

@ -146,8 +146,6 @@ node.on('stopping', function() {
clearInterval(interval); clearInterval(interval);
}); });
process.stdin.resume();//so the program will not close instantly
function exitHandler(options, err) { function exitHandler(options, err) {
if (err) { if (err) {
log.error('uncaught exception:', err); log.error('uncaught exception:', err);
@ -157,7 +155,6 @@ function exitHandler(options, err) {
process.exit(-1); process.exit(-1);
} }
if (options.sigint) { if (options.sigint) {
log.info('Stopping Services');
node.stop(function(err) { node.stop(function(err) {
if(err) { if(err) {
log.error('Failed to stop services: ' + err); log.error('Failed to stop services: ' + err);
@ -170,21 +167,10 @@ function exitHandler(options, err) {
} }
} }
//catches ctrl+c event
process.on('SIGINT', exitHandler.bind(null, {sigint:true}));
/*setTimeout(function() {
log.info('Stopping Services');
node.stop(function(err) {
if(err) {
log.error('Failed to stop services: ' + err);
return process.exit(1);
}
log.info('Halted');
process.exit(0);
});
}, 10000);*/
//catches uncaught exceptions //catches uncaught exceptions
process.on('uncaughtException', exitHandler.bind(null, {exit:true})); 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}));

View File

@ -400,10 +400,12 @@ Node.prototype._initialize = function() {
// Notify that there is a new tip // Notify that there is a new tip
this.bitcoind.on('tip', function(height) { this.bitcoind.on('tip', function(height) {
var percentage = self.bitcoind.syncPercentage(); if(!self.stopping) {
log.info('Bitcoin Core Daemon New Height:', height, 'Percentage:', percentage); var percentage = self.bitcoind.syncPercentage();
self.bitcoindHeight = height; log.info('Bitcoin Core Daemon New Height:', height, 'Percentage:', percentage);
self._syncBitcoind(); self.bitcoindHeight = height;
self._syncBitcoind();
}
}); });
this.bitcoind.on('error', function(err) { this.bitcoind.on('error', function(err) {
@ -478,7 +480,7 @@ Node.prototype.start = function(callback) {
}; };
Node.prototype.stop = function(callback) { Node.prototype.stop = function(callback) {
log.info('Stopping Bitcore Node'); log.info('Beginning shutdown');
var self = this; var self = this;
var services = this.getServiceOrder().reverse(); var services = this.getServiceOrder().reverse();

View File

@ -104,6 +104,8 @@ static bool g_testnet = false;
static bool g_regtest = false; static bool g_regtest = false;
static bool g_txindex = false; static bool g_txindex = false;
static boost::thread_group threadGroup;
/** /**
* Private Structs * Private Structs
* Used for async functions and necessary linked lists at points. * Used for async functions and necessary linked lists at points.
@ -685,7 +687,6 @@ start_node(void) {
static void static void
start_node_thread(void) { start_node_thread(void) {
boost::thread_group threadGroup;
CScheduler scheduler; CScheduler scheduler;
// Workaround for AppInit2() arg parsing. Not ideal, but it works. // Workaround for AppInit2() arg parsing. Not ideal, but it works.
@ -798,7 +799,6 @@ start_node_thread(void) {
*/ */
NAN_METHOD(StopBitcoind) { NAN_METHOD(StopBitcoind) {
fprintf(stderr, "Stopping Bitcoind please wait!\n");
Isolate* isolate = Isolate::GetCurrent(); Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate); HandleScope scope(isolate);
@ -840,7 +840,9 @@ NAN_METHOD(StopBitcoind) {
static void static void
async_stop_node(uv_work_t *req) { async_stop_node(uv_work_t *req) {
async_node_data *data = static_cast<async_node_data*>(req->data); async_node_data *data = static_cast<async_node_data*>(req->data);
StartShutdown(); StartShutdown();
while(!shutdown_complete) { while(!shutdown_complete) {
usleep(1E6); usleep(1E6);
} }