better signal handling.

This commit is contained in:
Christopher Jeffrey 2014-09-22 14:35:11 -07:00
parent f69c49aba1
commit 5a696ec535
2 changed files with 24 additions and 23 deletions

View File

@ -17,7 +17,7 @@ bitcoind.start(function(err) {
setTimeout(function() {
(function next(hash) {
return bitcoind.getBlock(hash, function(err, block) {
if (err) return console.log(err.message);
if (err) return print(err.message);
print(block);
if (block.tx.length && block.tx[0].txid) {
var txid = block.tx[0].txid;

View File

@ -32,32 +32,26 @@ Bitcoin.prototype.start = function(callback) {
var self = this;
var none = {};
var isSignal = {};
var sigint = { name: 'SIGINT', signal: isSignal };
var sighup = { name: 'SIGHUP', signal: isSignal };
var sigquit = { name: 'SIGQUIT', signal: isSignal };
var exitCaught = none;
var errorCaught = none;
this.log_pipe = bitcoindjs.start(function(err, status) {
process.on('SIGINT', function() {
if (process.listeners('SIGINT').length > 1) {
return;
}
if (!self._shutdown) {
process.exit(0);
} else {
self.stop();
exitCaught = 0;
}
});
process.on('SIGHUP', function() {
if (process.listeners('SIGHUP').length > 1) {
return;
}
if (!self._shutdown) {
process.exit(0);
} else {
self.stop();
exitCaught = 0;
}
[sigint, sighup, sigquit].forEach(function(signal) {
process.on(signal.name, signal.listener = function() {
if (process.listeners(signal.name).length > 1) {
return;
}
if (!self._shutdown) {
process.exit(0);
} else {
self.stop();
exitCaught = signal;
}
});
});
var exit = process.exit;
@ -115,6 +109,13 @@ Bitcoin.prototype.start = function(callback) {
delete self._shutdown;
if (exitCaught !== none) {
if (exitCaught.signal === isSignal) {
process.removeListener(exitCaught.name, exitCaught.listener);
setImmediate(function() {
process.kill(process.pid, exitCaught.name);
});
return;
}
return self._exit(exitCaught);
}