fix: separate poller and middleware postgres listeners
This commit is contained in:
parent
5945f9d31b
commit
b98d73cd6e
4 changed files with 64 additions and 36 deletions
|
|
@ -1,10 +1,64 @@
|
|||
const _ = require('lodash/fp')
|
||||
|
||||
const db = require('../db')
|
||||
const state = require('./state')
|
||||
const newSettingsLoader = require('../new-settings-loader')
|
||||
const helpers = require('../route-helpers')
|
||||
const logger = require('../logger')
|
||||
|
||||
db.connect({ direct: true }).then(sco => {
|
||||
sco.client.on('notification', data => {
|
||||
const parsedData = JSON.parse(data.payload)
|
||||
switch (parsedData.type) {
|
||||
case 'reload':
|
||||
return reload(parsedData.operatorId)
|
||||
default:
|
||||
break
|
||||
}
|
||||
})
|
||||
return sco.none('LISTEN $1:name', 'reload')
|
||||
}).catch(console.error)
|
||||
|
||||
db.connect({ direct: true }).then(sco => {
|
||||
sco.client.on('notification', data => {
|
||||
const parsedData = JSON.parse(data.payload)
|
||||
switch (parsedData.type) {
|
||||
case 'machineAction':
|
||||
return machineAction(parsedData.action, parsedData.value)
|
||||
default:
|
||||
break
|
||||
}
|
||||
})
|
||||
return sco.none('LISTEN $1:name', 'machineAction')
|
||||
}).catch(console.error)
|
||||
|
||||
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 reload (operatorId) {
|
||||
state.needsSettingsReload[operatorId.operatorId] = true
|
||||
}
|
||||
|
||||
const populateSettings = function (req, res, next) {
|
||||
const { needsSettingsReload, settingsCache } = state
|
||||
const operatorId = res.locals.operatorId
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue