Cache project configuration details.

This avoids requesting the project configurations on every single
incoming message. A listener refreshes the data on configuration
changes.
This commit is contained in:
D. Berge
2023-10-14 20:11:18 +02:00
parent eee2a96029
commit c1e35b2459

View File

@@ -1,17 +1,43 @@
// FIXME This code is in painful need of refactoring
const { setSurvey, transaction, pool } = require('../connection');
const { listen } = require('../notify');
const { ALERT, ERROR, WARNING, NOTICE, INFO, DEBUG } = require('DOUGAL_ROOT/debug')(__filename);
let last_tstamp = 0;
async function getAllProjectConfigs () {
const client = await pool.connect();
let project_configs, listener;
const text = `SELECT schema, meta AS data FROM projects;`;
const res = await client.query(text);
client.release();
return res.rows;
async function getAllProjectConfigs () {
async function getFromDatabase () {
DEBUG("Getting project configurations");
const client = await pool.connect();
try {
const text = `
SELECT schema, meta AS data
FROM projects
WHERE (meta->>'archived')::boolean IS NOT true;
`;
const res = await client.query(text);
project_configs = res.rows;
DEBUG("Have configurations for projects", project_configs.map(i => i.data.id));
} catch (err) {
ERROR(err);
} finally {
client.release();
}
return project_configs;
}
if (project_configs) {
return project_configs;
} else {
listener = await listen(["project"], getFromDatabase);
DEBUG("Added project configuration change listener");
return await getFromDatabase();
}
}
async function getNearestPreplot (candidates) {