mirror of
https://gitlab.com/wgp/dougal/software.git
synced 2025-12-06 07:47:07 +00:00
The events listener now uses a proper self-consuming queue and the event handlers have been rewritten accordingly. The way this works is that running init() on the handlers library instantiates the handlers and returns two higher-order functions, prepare() and despatch(). A call to the latter of these is appended to the queue with each new incoming event. The handlers have access to a context object (ctx) which may be used to persist data between calls and/or exchange data between handlers. This is used notably to give the handlers access to project configurations, which are themselves refreshed by a project configuration change handler (DetectProjectConfigurationChange).
61 lines
1.5 KiB
JavaScript
61 lines
1.5 KiB
JavaScript
const project = require('../../lib/db/project');
|
|
const { ALERT, ERROR, WARNING, NOTICE, INFO, DEBUG } = require('DOUGAL_ROOT/debug')(__filename);
|
|
|
|
class DetectProjectConfigurationChange {
|
|
|
|
author = `*${this.constructor.name}*`;
|
|
|
|
constructor (ctx) {
|
|
DEBUG(`${this.author} instantiated`);
|
|
|
|
// Grab project configurations.
|
|
// NOTE that this will run asynchronously
|
|
this.run({channel: "project"}, ctx);
|
|
}
|
|
|
|
async run (data, ctx) {
|
|
|
|
if (!data || data.channel !== "project") {
|
|
return;
|
|
}
|
|
|
|
// Project notifications, as of this writing, most likely
|
|
// do not carry payloads as those exceed the notification
|
|
// size limit.
|
|
// For our purposes, we do not care as we just re-read all
|
|
// the configurations for all non-archived projects.
|
|
|
|
try {
|
|
DEBUG("Project configuration change detected")
|
|
|
|
const projects = await project.get();
|
|
|
|
const _ctx_data = {};
|
|
for (let pid of projects.map(i => i.pid)) {
|
|
DEBUG("Retrieving configuration for", pid);
|
|
const cfg = await project.configuration.get(pid);
|
|
if (cfg?.archived === true) {
|
|
DEBUG(pid, "is archived. Ignoring");
|
|
continue;
|
|
}
|
|
|
|
DEBUG("Saving configuration for", pid);
|
|
_ctx_data[pid] = cfg;
|
|
}
|
|
|
|
if (! ("projects" in ctx)) {
|
|
ctx.projects = {};
|
|
}
|
|
|
|
ctx.projects.configuration = _ctx_data;
|
|
DEBUG("Committed project configuration to ctx.projects.configuration");
|
|
|
|
} catch (err) {
|
|
DEBUG(`${this.author} error`, err);
|
|
throw err;
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = DetectProjectConfigurationChange;
|