diff --git a/package.json b/package.json index fc55f165cb3..70b9ac6bad1 100644 --- a/package.json +++ b/package.json @@ -88,6 +88,7 @@ "ampersand-sync-localforage": "^0.1.1", "ampersand-view": "^8.0.0", "ampersand-view-switcher": "^2.0.0", + "backoff": "^2.4.1", "bootstrap": "https://github.com/twbs/bootstrap/archive/v3.3.5.tar.gz", "browserify": "^10.2.4", "bugsnag-js": "^2.4.8", @@ -119,8 +120,8 @@ "mocha": "^2.2.5", "moment": "^2.10.3", "mongodb-extended-json": "^1.3.1", - "mongodb-js-precommit": "^0.2.2", "mongodb-js-fmt": "^0.0.3", + "mongodb-js-precommit": "^0.2.2", "mongodb-language-model": "^0.2.1", "mongodb-schema": "^3.3.0", "mousetrap": "^1.5.3", diff --git a/src/app.js b/src/app.js index c812a64e8eb..c0f490b1436 100644 --- a/src/app.js +++ b/src/app.js @@ -2,6 +2,8 @@ var pkg = require('../package.json'); var app = require('ampersand-app'); +var backoff = require('backoff'); + app.extend({ // @todo (imlucas) Move to config // `scout-server` to point at. @@ -30,6 +32,31 @@ var Statusbar = require('./statusbar'); var debug = require('debug')('scout:app'); +function getConnection(model, done) { + function _fetch(fn) { + model.fetch({ + success: function() { + debug('_fetch connection succeeded!'); + fn(); + }, + error: function() { + debug('_fetch connection failed', arguments); + fn(new Error('Error retrieving connection details')); + } + }); + } + + var call = backoff.call(_fetch, done); + call.setStrategy(new backoff.ExponentialStrategy({ + randomisationFactor: 0, + initialDelay: 10, + maxDelay: 500 + })); + call.failAfter(10); + call.start(); +} + + // Inter-process communication with main process (Electron window) var ipc = window.require('ipc'); @@ -253,25 +280,21 @@ app.extend({ }); debug('looking up connection `%s`...', connection_id); - state.connection.fetch({ - success: function() { - app.statusbar.show('Connection details loaded! Initializing client...'); - - var endpoint = app.endpoint; - var connection = state.connection.serialize(); - - app.client = getOrCreateClient(endpoint, connection) - .on('readable', state.onClientReady.bind(state)) - .on('error', state.onFatalError.bind(state, 'create client')); - - state.startClientStalledTimer(); - }, - error: function() { - // @todo (imlucas) `ampersand-sync-localforage` currently drops - // the real error so for now just use a generic. - state.onFatalError(state, 'fetch connection', - new Error('Error retrieving connection. Please reload the page.')); + getConnection(state.connection, function(err) { + if (err) { + state.onFatalError('fetch connection', err); + return; } + app.statusbar.show('Connecting to MongoDB...'); + + var endpoint = app.endpoint; + var connection = state.connection.serialize(); + + app.client = getOrCreateClient(endpoint, connection) + .on('readable', state.onClientReady.bind(state)) + .on('error', state.onFatalError.bind(state, 'create client')); + + state.startClientStalledTimer(); }); }); // set up ipc