diff --git a/bin/lamassu-server b/bin/lamassu-server index 923b3dd2..d86a9c90 100755 --- a/bin/lamassu-server +++ b/bin/lamassu-server @@ -2,6 +2,9 @@ const app = require('../lib/app') -process.on('unhandledRejection', err => console.log(err.stack)) +process.on('unhandledRejection', err => { + console.log('Unhandled rejection') + console.log(err.stack) +}) app.run() diff --git a/lib/app.js b/lib/app.js index 288b523e..efd79f12 100644 --- a/lib/app.js +++ b/lib/app.js @@ -2,6 +2,8 @@ const fs = require('fs') const http = require('http') const https = require('https') const express = require('express') +const loop = require('reoccur') + const routes = require('./routes') const plugins = require('./plugins') const logger = require('./logger') @@ -13,6 +15,20 @@ const options = require('./options') const devMode = argv.dev || argv.http || options.http function run () { + let count = 0 + return loop((recur, resolve, reject) => { + return runOnce() + .then(resolve) + .catch(err => { + count += 1 + logger.debug(err) + logger.debug('[%d] Retrying in 10s...', count) + setTimeout(recur, 10000) + }) + }) +} + +function runOnce () { const app = express() const localApp = express() @@ -21,47 +37,45 @@ function run () { return configManager.load() .then(config => { - plugins.configure(config) - plugins.startPolling() - plugins.startCheckingNotification() + return plugins.configure(config) + .then(() => { + plugins.startPolling() + plugins.startCheckingNotification() - const httpsServerOptions = { - key: fs.readFileSync(options.keyPath), - cert: fs.readFileSync(options.certPath), - requestCert: true - } + const httpsServerOptions = { + key: fs.readFileSync(options.keyPath), + cert: fs.readFileSync(options.certPath), + requestCert: true + } - const server = devMode - ? http.createServer(app) - : https.createServer(httpsServerOptions, app) + const server = devMode + ? http.createServer(app) + : https.createServer(httpsServerOptions, app) - const port = 3000 - const localPort = 3030 - const localServer = http.createServer(localApp) + const port = 3000 + const localPort = 3030 + const localServer = http.createServer(localApp) - if (options.devMode) logger.info('In dev mode') + if (options.devMode) logger.info('In dev mode') - const opts = { - app, - localApp, - devMode, - plugins - } + const opts = { + app, + localApp, + devMode, + plugins + } - routes.init(opts) + routes.init(opts) - server.listen(port, () => { - console.log('lamassu-server listening on port ' + - port + ' ' + (devMode ? '(http)' : '(https)')) + server.listen(port, () => { + console.log('lamassu-server listening on port ' + + port + ' ' + (devMode ? '(http)' : '(https)')) + }) + + localServer.listen(localPort, 'localhost', () => { + console.log('lamassu-server listening on local port ' + localPort) + }) }) - - localServer.listen(localPort, 'localhost', () => { - console.log('lamassu-server listening on local port ' + localPort) - }) - }) - .catch(err => { - console.error(err) - process.exit(1) }) } diff --git a/lib/config-manager.js b/lib/config-manager.js index 812d865a..020b2e06 100644 --- a/lib/config-manager.js +++ b/lib/config-manager.js @@ -12,8 +12,9 @@ function loadAccounts () { const toFields = fieldArr => R.fromPairs(R.map(r => [r.code, r.value], fieldArr)) const toPairs = r => [r.code, toFields(r.fields)] - return db.one('select data from user_config where type=$1', 'accounts') + return db.oneOrNone('select data from user_config where type=$1', 'accounts') .then(function (data) { + if (!data) return {} return R.fromPairs(R.map(toPairs, data.data.accounts)) }) } diff --git a/lib/plugins.js b/lib/plugins.js index d39d0c8d..a74c2952 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -165,96 +165,95 @@ exports.loadOrConfigPlugin = loadOrConfigPlugin exports.configure = function configure (config) { cachedConfig = config - const accounts = configManager.loadAccounts() - const cryptoCodes = getCryptoCodes() console.log('DEBUG30') - cryptoCodes.forEach(cryptoCode => { - console.log('DEBUG31') - const cryptoScopedConfig = configManager.cryptoScoped(cryptoCode, cachedConfig) + return configManager.loadAccounts() + .then(accounts => { + cryptoCodes.forEach(cryptoCode => { + console.log('DEBUG31') + const cryptoScopedConfig = configManager.cryptoScoped(cryptoCode, cachedConfig) - console.log('DEBUG31.1') + console.log('DEBUG31.1') - // TICKER [required] configure (or load) - loadOrConfigPlugin( - tickerPlugins[cryptoCode], - 'ticker', - cryptoCode, - cryptoScopedConfig, - accounts, - {currency: deviceCurrency}, - function onTickerChange (newTicker) { - tickerPlugins[cryptoCode] = newTicker - pollRate(cryptoCode) - } - ) + // TICKER [required] configure (or load) + loadOrConfigPlugin( + tickerPlugins[cryptoCode], + 'ticker', + cryptoCode, + cryptoScopedConfig, + accounts, + {currency: deviceCurrency}, + function onTickerChange (newTicker) { + tickerPlugins[cryptoCode] = newTicker + pollRate(cryptoCode) + } + ) - console.log('DEBUG31.2') + console.log('DEBUG31.2') - // Give each crypto a different derived seed so as not to allow any - // plugin to spend another plugin's funds - const cryptoSeed = hkdf.derive(cryptoCode, 32) + // Give each crypto a different derived seed so as not to allow any + // plugin to spend another plugin's funds + const cryptoSeed = hkdf.derive(cryptoCode, 32) - loadOrConfigPlugin( - walletPlugins[cryptoCode], - 'wallet', - cryptoCode, - cryptoScopedConfig, - accounts, - {masterSeed: cryptoSeed}, - function onWalletChange (newWallet) { - walletPlugins[cryptoCode] = newWallet - pollBalance(cryptoCode) - } - ) + loadOrConfigPlugin( + walletPlugins[cryptoCode], + 'wallet', + cryptoCode, + cryptoScopedConfig, + accounts, + {masterSeed: cryptoSeed}, + function onWalletChange (newWallet) { + walletPlugins[cryptoCode] = newWallet + pollBalance(cryptoCode) + } + ) - tradesQueues[cryptoCode] = tradesQueues[cryptoCode] || [] + tradesQueues[cryptoCode] = tradesQueues[cryptoCode] || [] - loadOrConfigPlugin( - traderPlugins[cryptoCode], - 'trader', - cryptoCode, - cryptoScopedConfig, - accounts, + loadOrConfigPlugin( + traderPlugins[cryptoCode], + 'trader', + cryptoCode, + cryptoScopedConfig, + accounts, + null, + function onTraderChange (newTrader) { + traderPlugins[cryptoCode] = newTrader + if (newTrader === null) stopTrader(cryptoCode) + else startTrader(cryptoCode) + } + ) + }) + + const unscopedCfg = configManager.unscoped(cachedConfig) + + // ID VERIFIER [optional] configure (or load) + idVerifierPlugin = loadOrConfigPlugin( + idVerifierPlugin, + 'idVerifier', null, - function onTraderChange (newTrader) { - traderPlugins[cryptoCode] = newTrader - if (newTrader === null) stopTrader(cryptoCode) - else startTrader(cryptoCode) - } + unscopedCfg, + accounts + ) + + emailPlugin = loadOrConfigPlugin( + emailPlugin, + 'email', + null, + unscopedCfg, + accounts + ) + + smsPlugin = loadOrConfigPlugin( + smsPlugin, + 'sms', + null, + unscopedCfg, + accounts ) }) - - const unscopedCfg = configManager.unscoped(cachedConfig) - - // ID VERIFIER [optional] configure (or load) - idVerifierPlugin = loadOrConfigPlugin( - idVerifierPlugin, - 'idVerifier', - null, - unscopedCfg, - accounts - ) - - emailPlugin = loadOrConfigPlugin( - emailPlugin, - 'email', - null, - unscopedCfg, - accounts - ) - - smsPlugin = loadOrConfigPlugin( - smsPlugin, - 'sms', - null, - unscopedCfg, - accounts - ) - - console.log('DEBUG33') } function getConfig (machineId) { diff --git a/lib/routes.js b/lib/routes.js index 803ab5c5..43e503ac 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -87,7 +87,7 @@ function poll (req, res) { console.log('DEBUG2: %j, %s, %s', reboots, reboot, pid) const langs = config.languages.machineLanguages - console.log('DEBUG33') + console.log('DEBUG33.1') const locale = { currency: config.currencies.fiatCurrency, @@ -411,8 +411,8 @@ function init (opts) { localApp.post('/dbChange', (req, res) => { return configManager.load() .then(config => { - plugins.configure(config) - logger.info('Config reloaded') + return plugins.configure(config) + .then(() => logger.info('Config reloaded')) }) .catch(logger.error) }) diff --git a/package.json b/package.json index 8a210d71..dcd8784a 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "pify": "^2.3.0", "pretty-ms": "^2.1.0", "ramda": "^0.21.0", + "reoccur": "^1.0.0", "winston": "^2.3.0" }, "repository": { diff --git a/yarn.lock b/yarn.lock index 81da68a7..2daa357d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1301,6 +1301,10 @@ referrer-policy@1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/referrer-policy/-/referrer-policy-1.0.0.tgz#f60eedc92f942b01a6118121ec932d66e8fd7e14" +reoccur: + version "1.0.0" + resolved "https://registry.yarnpkg.com/reoccur/-/reoccur-1.0.0.tgz#62578914654cfe7583caf7a609ed30a3e9a4197d" + repeating@^2.0.0: version "2.0.1" resolved "https://registry.yarnpkg.com/repeating/-/repeating-2.0.1.tgz#5214c53a926d3552707527fbab415dbc08d06dda"