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 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)
}

View file

@ -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

View file

@ -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,9 +99,11 @@ 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 => {
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()
})