diff --git a/lib/services/db/index.js b/lib/services/db/index.js index e1ae91ea..bdcf3c8c 100644 --- a/lib/services/db/index.js +++ b/lib/services/db/index.js @@ -160,7 +160,6 @@ DB.prototype.start = function(callback) { }); self._sync.on('synced', function() { - log.permitWrites = true; log.info('Initial sync complete'); }); @@ -169,8 +168,6 @@ DB.prototype.start = function(callback) { }); self.node.once('ready', function() { - log.permitWrites = false; - self._sync.initialSync(); self.node.services.bitcoind.on('tip', function(height) { diff --git a/lib/services/db/sync.js b/lib/services/db/sync.js index d5a019d7..e45df76f 100644 --- a/lib/services/db/sync.js +++ b/lib/services/db/sync.js @@ -8,6 +8,8 @@ var async = require('async'); var bitcore = require('bitcore-lib'); var BufferUtil = bitcore.util.buffer; var ProgressBar = require('progress'); +var index = require('../../index'); +var log = index.log; var green = '\u001b[42m \u001b[0m'; var red = '\u001b[41m \u001b[0m'; @@ -98,6 +100,7 @@ Sync.prototype.initialSync = function() { .pipe(processSerial); self.lastReportedBlock = self.db.tip.__height; + self.progressBar = new ProgressBar('[:bar] :percent :current blks, :blockspersec blks/sec, :elapsed secs', { complete: green, incomplete: red, @@ -105,15 +108,7 @@ Sync.prototype.initialSync = function() { clear: true }); - self.progressBar.tick(self.db.tip.__height, { - blockspersec: 0 - }); - - var timer = setInterval(function () { - var tick = self.db.tip.__height - self.lastReportedBlock; - self.progressBar.tick(tick, { blockspersec: tick }); - self.lastReportedBlock = self.db.tip.__height; - }, 1000); + var timer = setInterval(self.reportStatus.bind(this), 1000); processSerial.on('finish', function() { self.syncing = false; @@ -128,6 +123,16 @@ Sync.prototype.initialSync = function() { }; +Sync.prototype.reportStatus = function() { + if (process.stderr.isTTY) { + var tick = this.db.tip.__height - this.lastReportedBlock; + this.progressBar.tick(tick, { blockspersec: tick }); + this.lastReportedBlock = this.db.tip.__height; + } else { + log.info('Sync: current height is: ' + this.db.tip.__height); + } +}; + Sync.prototype.sync = function() { var self = this; if(this.syncing || this.db.reorg) {