Feat: use pg-promise notify for poller update
This commit is contained in:
parent
4ce4a32502
commit
121ced632e
2 changed files with 14 additions and 2 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
const _ = require('lodash/fp')
|
const _ = require('lodash/fp')
|
||||||
const db = require('./db')
|
const db = require('./db')
|
||||||
const migration = require('./config-migration')
|
const migration = require('./config-migration')
|
||||||
|
const { asyncLocalStorage } = require('./async-storage')
|
||||||
|
|
||||||
const OLD_SETTINGS_LOADER_SCHEMA_VERSION = 1
|
const OLD_SETTINGS_LOADER_SCHEMA_VERSION = 1
|
||||||
const NEW_SETTINGS_LOADER_SCHEMA_VERSION = 2
|
const NEW_SETTINGS_LOADER_SCHEMA_VERSION = 2
|
||||||
|
|
@ -73,7 +74,10 @@ function saveConfig (config) {
|
||||||
return loadLatestConfigOrNone()
|
return loadLatestConfigOrNone()
|
||||||
.then(currentConfig => {
|
.then(currentConfig => {
|
||||||
const newConfig = _.assign(currentConfig, config)
|
const newConfig = _.assign(currentConfig, config)
|
||||||
return db.none(configSql, ['config', { config: newConfig }, true, NEW_SETTINGS_LOADER_SCHEMA_VERSION])
|
return db.tx(t => {
|
||||||
|
return t.none(configSql, ['config', { config: newConfig }, true, NEW_SETTINGS_LOADER_SCHEMA_VERSION])
|
||||||
|
.then(() => t.none('NOTIFY $1:name, $2', ['poller', asyncLocalStorage.getStore().get('schema')]))
|
||||||
|
}).catch(console.error)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ const { asyncLocalStorage, defaultStore } = require('./async-storage')
|
||||||
const settingsLoader = require('./new-settings-loader')
|
const settingsLoader = require('./new-settings-loader')
|
||||||
const NodeCache = require('node-cache')
|
const NodeCache = require('node-cache')
|
||||||
const util = require('util')
|
const util = require('util')
|
||||||
|
const db = require('./db')
|
||||||
|
|
||||||
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
|
||||||
|
|
@ -74,11 +75,18 @@ cachedVariables.on('expired', (key, val) => {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
db.connect({ direct: true }).then(sco => {
|
||||||
|
sco.client.on('notification', data => {
|
||||||
|
return reload(data.payload)
|
||||||
|
})
|
||||||
|
return sco.none('LISTEN $1:name', 'poller')
|
||||||
|
}).catch(console.error)
|
||||||
|
|
||||||
function reload (schema) {
|
function reload (schema) {
|
||||||
const store = defaultStore()
|
const store = defaultStore()
|
||||||
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
|
||||||
asyncLocalStorage.run(store, () => {
|
return asyncLocalStorage.run(store, () => {
|
||||||
return settingsLoader.loadLatest().then(settings => {
|
return settingsLoader.loadLatest().then(settings => {
|
||||||
const pi = plugins(settings)
|
const pi = plugins(settings)
|
||||||
cachedVariables.set(schema, { settings, pi, isReloading: false })
|
cachedVariables.set(schema, { settings, pi, isReloading: false })
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue