diff --git a/lib/middlewares/populateSettings.js b/lib/middlewares/populateSettings.js index 146aafeb..fd3d4146 100644 --- a/lib/middlewares/populateSettings.js +++ b/lib/middlewares/populateSettings.js @@ -1,11 +1,12 @@ +const _ = require('lodash/fp') + const state = require('./state') const newSettingsLoader = require('../new-settings-loader') const helpers = require('../route-helpers') const logger = require('../logger') -const { settingsCache } = state - const populateSettings = function (req, res, next) { + const { needsSettingsReload, settingsCache } = state const operatorId = res.locals.operatorId const versionId = req.headers['config-version'] if (versionId !== state.oldVersionId) { @@ -14,20 +15,21 @@ const populateSettings = function (req, res, next) { try { const operatorSettings = settingsCache.get(operatorId) - if (!versionId && operatorSettings) { - req.settings = operatorSettings - return next() - } - - if (!versionId && !operatorSettings) { + if (!versionId && (!operatorSettings || !!needsSettingsReload[operatorId])) { return newSettingsLoader.loadLatest() .then(settings => { settingsCache.set(operatorId, settings) + delete needsSettingsReload[operatorId] req.settings = settings }) .then(() => next()) .catch(next) } + + if (!versionId && operatorSettings) { + req.settings = operatorSettings + return next() + } } catch (e) { logger.error(e) } diff --git a/lib/middlewares/state.js b/lib/middlewares/state.js index 10267d88..2599e259 100644 --- a/lib/middlewares/state.js +++ b/lib/middlewares/state.js @@ -4,6 +4,7 @@ const SETTINGS_CACHE_REFRESH = 3600 module.exports = (function () { return { oldVersionId: 'unset', + needsSettingsReload: {}, settingsCache: new NodeCache({ stdTTL: SETTINGS_CACHE_REFRESH, checkperiod: SETTINGS_CACHE_REFRESH // Clear cache every hour diff --git a/lib/poller.js b/lib/poller.js index 25011b17..914554dd 100644 --- a/lib/poller.js +++ b/lib/poller.js @@ -18,6 +18,7 @@ const util = require('util') const db = require('./db') const state = require('./middlewares/state') const processBatches = require('./tx-batching-processing') +const { getOperatorId } = require('./operator') const INCOMING_TX_INTERVAL = 30 * T.seconds const LIVE_INCOMING_TX_INTERVAL = 5 * T.seconds @@ -98,12 +99,14 @@ function reload (schema) { store.set('schema', schema) // set asyncLocalStorage so settingsLoader loads settings for the right schema return asyncLocalStorage.run(store, () => { - return settingsLoader.loadLatest().then(settings => { - const pi = plugins(settings) - cachedVariables.set(schema, { settings, pi, isReloading: false }) - logger.debug(`Settings for schema '${schema}' reloaded in poller`) - return updateAndLoadSanctions() - }) + return Promise.all([settingsLoader.loadLatest(), getOperatorId('middleware')]) + .then(([settings, operatorId]) => { + const pi = plugins(settings) + cachedVariables.set(schema, { settings, pi, isReloading: false }) + state.needsSettingsReload[operatorId.operatorId] = true + logger.debug(`Settings for schema '${schema}' reloaded in poller`) + return updateAndLoadSanctions() + }) }) }