Merge pull request #217 from pnagurny/enhance/close-leveldb
Wait for db operations to complete before closing leveldb
This commit is contained in:
commit
a3c49dd007
|
@ -136,8 +136,16 @@ DB.prototype.start = function(callback) {
|
||||||
};
|
};
|
||||||
|
|
||||||
DB.prototype.stop = function(callback) {
|
DB.prototype.stop = function(callback) {
|
||||||
// TODO Figure out how to call this.store.close() without issues
|
var self = this;
|
||||||
setImmediate(callback);
|
|
||||||
|
// Wait until syncing stops and all db operations are completed before closing leveldb
|
||||||
|
async.whilst(function() {
|
||||||
|
return self.bitcoindSyncing;
|
||||||
|
}, function(next) {
|
||||||
|
setTimeout(next, 10);
|
||||||
|
}, function() {
|
||||||
|
self.store.close(callback);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
DB.prototype.getInfo = function(callback) {
|
DB.prototype.getInfo = function(callback) {
|
||||||
|
@ -389,7 +397,6 @@ DB.prototype.runAllBlockHandlers = function(block, add, callback) {
|
||||||
}
|
}
|
||||||
|
|
||||||
log.debug('Updating the database with operations', operations);
|
log.debug('Updating the database with operations', operations);
|
||||||
|
|
||||||
self.store.batch(operations, callback);
|
self.store.batch(operations, callback);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
@ -525,11 +532,7 @@ DB.prototype.syncRewind = function(block, done) {
|
||||||
DB.prototype.sync = function() {
|
DB.prototype.sync = function() {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
if (self.bitcoindSyncing) {
|
if (self.bitcoindSyncing || self.node.stopping || !self.tip) {
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!self.tip) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -592,13 +595,12 @@ DB.prototype.sync = function() {
|
||||||
return self.node.emit('error', err);
|
return self.node.emit('error', err);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.bitcoindSyncing = false;
|
||||||
|
|
||||||
if(self.node.stopping) {
|
if(self.node.stopping) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.bitcoindSyncing = false;
|
|
||||||
self.saveMetadata();
|
|
||||||
|
|
||||||
// If bitcoind is completely synced
|
// If bitcoind is completely synced
|
||||||
if (self.node.services.bitcoind.isSynced()) {
|
if (self.node.services.bitcoind.isSynced()) {
|
||||||
self.node.emit('synced');
|
self.node.emit('synced');
|
||||||
|
|
|
@ -276,13 +276,21 @@ describe('DB Service', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('#stop', function() {
|
describe('#stop', function() {
|
||||||
it('should immediately call the callback', function(done) {
|
it('should wait until db has stopped syncing before closing leveldb', function(done) {
|
||||||
var db = new DB(baseConfig);
|
var db = new DB(baseConfig);
|
||||||
|
db.store = {
|
||||||
|
close: sinon.stub().callsArg(0)
|
||||||
|
};
|
||||||
|
db.bitcoindSyncing = true;
|
||||||
|
|
||||||
db.stop(function(err) {
|
db.stop(function(err) {
|
||||||
should.not.exist(err);
|
should.not.exist(err);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
setTimeout(function() {
|
||||||
|
db.bitcoindSyncing = false;
|
||||||
|
}, 15);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue