fix: add config reload flags to the state middleware
This commit is contained in:
parent
cfb360ab4e
commit
5945f9d31b
3 changed files with 20 additions and 14 deletions
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue