feat: add operatorId to l-a-s middlewares

fix: make changes to db event handler to receive more complex payloads
feat: machine actions previously on REST now work based on notifications
feat: state middleware now operates based on operatorId as well
chore: remove old localAppRoutes related code
This commit is contained in:
Sérgio Salgado 2021-09-17 16:57:28 +01:00
parent 32e5b1ba87
commit 7135a03654
11 changed files with 71 additions and 78 deletions

View file

@ -16,6 +16,7 @@ const settingsLoader = require('./new-settings-loader')
const NodeCache = require('node-cache')
const util = require('util')
const db = require('./db')
const state = require('./middlewares/state')
const INCOMING_TX_INTERVAL = 30 * T.seconds
const LIVE_INCOMING_TX_INTERVAL = 5 * T.seconds
@ -77,7 +78,15 @@ cachedVariables.on('expired', (key, val) => {
db.connect({ direct: true }).then(sco => {
sco.client.on('notification', data => {
return reload(data.payload)
const parsedData = JSON.parse(data.payload)
switch (parsedData.type) {
case 'reload':
return reload(parsedData.schema)
case 'machineAction':
return machineAction(parsedData.action, parsedData.value)
default:
break
}
})
return sco.none('LISTEN $1:name', 'poller')
}).catch(console.error)
@ -90,12 +99,35 @@ function reload (schema) {
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`)
logger.debug(`Settings for schema '${schema}' reloaded in poller`)
return updateAndLoadSanctions()
})
})
}
function machineAction (type, value) {
const deviceId = value.deviceId
const operatorId = value.operatorId
const pid = state.pids?.[operatorId]?.[deviceId]?.pid
switch (type) {
case 'reboot':
logger.debug(`Rebooting machine '${deviceId}' from operator ${operatorId}`)
state.reboots[operatorId] = { [deviceId]: pid }
break
case 'shutdown':
logger.debug(`Shutting down machine '${deviceId}' from operator ${operatorId}`)
state.shutdowns[operatorId] = { [deviceId]: pid }
break
case 'restartServices':
logger.debug(`Restarting services of machine '${deviceId}' from operator ${operatorId}`)
state.restartServicesMap[operatorId] = { [deviceId]: pid }
break
default:
break
}
}
function pi () { return cachedVariables.get(asyncLocalStorage.getStore().get('schema')).pi }
function settings () { return cachedVariables.get(asyncLocalStorage.getStore().get('schema')).settings }