mirror of
https://gitlab.com/wgp/dougal/software.git
synced 2025-12-06 09:57:09 +00:00
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:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user