From 1e9307f4a0d1fb5ea6858c913675fa9567e991b0 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Tue, 14 Jan 2014 16:45:25 -0300 Subject: [PATCH] sync retry after RPC error --- .gitignore | 3 +-- lib/Sync.js | 47 ++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index daa35293..888b5ce4 100644 --- a/.gitignore +++ b/.gitignore @@ -8,7 +8,7 @@ lib-cov *.pid *.gz *.swp - +tags pids logs results @@ -23,7 +23,6 @@ node_modules peerdb.json npm-debug.log -node_modules .nodemonignore .DS_Store diff --git a/lib/Sync.js b/lib/Sync.js index 6b4fe0c9..3e345b5a 100644 --- a/lib/Sync.js +++ b/lib/Sync.js @@ -199,19 +199,35 @@ function spec() { }; Sync.prototype.init = function(opts) { - if (!(opts && opts.skip_db_connection)) { - mongoose.connect(config.db); - } - this.db = mongoose.connection; this.rpc = new RpcClient(config.bitcoind); - this.db.on('error', console.error.bind(console, 'connection error:')); + + if (!(opts && opts.skip_db_connection)) { + mongoose.connect(config.db, {server: {auto_reconnect: true}} ); + } + + this.db = mongoose.connection; + + this.db.on('error', function(err) { + console.log('connection error:' + err); + moogose.disconnect(); + }); + + this.db.on('disconnect', function(err) { + console.log('disconnect:' + err); + mongoose.connect(config.db, {server: {auto_reconnect: true}} ); + }); + }; Sync.prototype.import_history = function(opts, next) { var that = this; + + var retry_attemps = 100; + var retry_secs = 2; + this.db.once('open', function() { async.series([ function(cb) { @@ -240,10 +256,23 @@ function spec() { }, function(cb) { + function sync() { + that.syncBlocks( function(err) { + + + if (err && err.message.match(/ECONNREFUSED/) && retry_attemps--){ + setTimeout(function() { + console.log("Retrying in %d secs ", retry_secs); + sync(); + }, retry_secs * 1000); + } + else + return next(err); + }); + } + if (!opts.skip_blocks) { - that.syncBlocks( cb); - } else { - cb(); + sync(); } }, /* Exploding happens on block insertion @@ -267,7 +296,7 @@ function spec() { } */ ], function(err) { - return next(err); + return next(err); }); }); };