fix: add config reload flags to the state middleware

This commit is contained in:
Sérgio Salgado 2022-01-19 02:13:02 +00:00
parent cfb360ab4e
commit 5945f9d31b
3 changed files with 20 additions and 14 deletions

View file

@ -1,11 +1,12 @@
const _ = require('lodash/fp')
const state = require('./state') const state = require('./state')
const newSettingsLoader = require('../new-settings-loader') const newSettingsLoader = require('../new-settings-loader')
const helpers = require('../route-helpers') const helpers = require('../route-helpers')
const logger = require('../logger') const logger = require('../logger')
const { settingsCache } = state
const populateSettings = function (req, res, next) { const populateSettings = function (req, res, next) {
const { needsSettingsReload, settingsCache } = state
const operatorId = res.locals.operatorId const operatorId = res.locals.operatorId
const versionId = req.headers['config-version'] const versionId = req.headers['config-version']
if (versionId !== state.oldVersionId) { if (versionId !== state.oldVersionId) {
@ -14,20 +15,21 @@ const populateSettings = function (req, res, next) {
try { try {
const operatorSettings = settingsCache.get(operatorId) const operatorSettings = settingsCache.get(operatorId)
if (!versionId && operatorSettings) { if (!versionId && (!operatorSettings || !!needsSettingsReload[operatorId])) {
req.settings = operatorSettings
return next()
}
if (!versionId && !operatorSettings) {
return newSettingsLoader.loadLatest() return newSettingsLoader.loadLatest()
.then(settings => { .then(settings => {
settingsCache.set(operatorId, settings) settingsCache.set(operatorId, settings)
delete needsSettingsReload[operatorId]
req.settings = settings req.settings = settings
}) })
.then(() => next()) .then(() => next())
.catch(next) .catch(next)
} }
if (!versionId && operatorSettings) {
req.settings = operatorSettings
return next()
}
} catch (e) { } catch (e) {
logger.error(e) logger.error(e)
} }

View file

@ -4,6 +4,7 @@ const SETTINGS_CACHE_REFRESH = 3600
module.exports = (function () { module.exports = (function () {
return { return {
oldVersionId: 'unset', oldVersionId: 'unset',
needsSettingsReload: {},
settingsCache: new NodeCache({ settingsCache: new NodeCache({
stdTTL: SETTINGS_CACHE_REFRESH, stdTTL: SETTINGS_CACHE_REFRESH,
checkperiod: SETTINGS_CACHE_REFRESH // Clear cache every hour checkperiod: SETTINGS_CACHE_REFRESH // Clear cache every hour

View file

@ -18,6 +18,7 @@ const util = require('util')
const db = require('./db') const db = require('./db')
const state = require('./middlewares/state') const state = require('./middlewares/state')
const processBatches = require('./tx-batching-processing') const processBatches = require('./tx-batching-processing')
const { getOperatorId } = require('./operator')
const INCOMING_TX_INTERVAL = 30 * T.seconds const INCOMING_TX_INTERVAL = 30 * T.seconds
const LIVE_INCOMING_TX_INTERVAL = 5 * T.seconds const LIVE_INCOMING_TX_INTERVAL = 5 * T.seconds
@ -98,12 +99,14 @@ function reload (schema) {
store.set('schema', schema) store.set('schema', schema)
// set asyncLocalStorage so settingsLoader loads settings for the right schema // set asyncLocalStorage so settingsLoader loads settings for the right schema
return asyncLocalStorage.run(store, () => { return asyncLocalStorage.run(store, () => {
return settingsLoader.loadLatest().then(settings => { return Promise.all([settingsLoader.loadLatest(), getOperatorId('middleware')])
const pi = plugins(settings) .then(([settings, operatorId]) => {
cachedVariables.set(schema, { settings, pi, isReloading: false }) const pi = plugins(settings)
logger.debug(`Settings for schema '${schema}' reloaded in poller`) cachedVariables.set(schema, { settings, pi, isReloading: false })
return updateAndLoadSanctions() state.needsSettingsReload[operatorId.operatorId] = true
}) logger.debug(`Settings for schema '${schema}' reloaded in poller`)
return updateAndLoadSanctions()
})
}) })
} }