2020-09-13 15:35:36 +02:00
|
|
|
#!/usr/bin/node
|
2020-08-08 23:59:13 +02:00
|
|
|
|
2023-09-15 14:22:02 +02:00
|
|
|
const { ERROR, INFO, DEBUG } = require('DOUGAL_ROOT/debug')(__filename);
|
2022-05-12 22:09:08 +02:00
|
|
|
|
2025-08-15 14:45:46 +02:00
|
|
|
async function getProjectConfigurations (opts = {}) {
|
|
|
|
|
const includeArchived = {includeArchived: false, ...opts};
|
|
|
|
|
let projectConfigurations = {};
|
|
|
|
|
try {
|
|
|
|
|
const db = require('./lib/db');
|
|
|
|
|
const pids = (await db.project.get())
|
|
|
|
|
.filter(i => includeArchived || !i.archived)
|
|
|
|
|
.map(i => i.pid);
|
|
|
|
|
for (const pid of pids) {
|
|
|
|
|
DEBUG(`Reading project configuration for ${pid}`);
|
|
|
|
|
const cfg = await db.project.configuration.get(pid);
|
|
|
|
|
projectConfigurations[pid] = cfg;
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
ERROR("Failed to get project configurations");
|
|
|
|
|
ERROR(err);
|
|
|
|
|
}
|
|
|
|
|
return projectConfigurations;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-06 22:52:45 +01:00
|
|
|
async function main () {
|
|
|
|
|
// Check that we're running against the correct database version
|
|
|
|
|
const version = require('./lib/version');
|
2022-05-12 22:09:08 +02:00
|
|
|
INFO("Running version", await version.describe());
|
2022-02-06 22:52:45 +01:00
|
|
|
version.compatible()
|
2025-08-15 14:45:46 +02:00
|
|
|
.then( async (versions) => {
|
2023-10-14 20:02:04 +02:00
|
|
|
try {
|
|
|
|
|
const api = require('./api');
|
|
|
|
|
const ws = require('./ws');
|
2023-10-14 20:04:52 +02:00
|
|
|
const periodicTasks = require('./periodic-tasks').init();
|
2025-08-15 14:45:46 +02:00
|
|
|
const { setupEventHandlers } = require('./events');
|
2020-10-02 01:34:49 +02:00
|
|
|
|
2023-10-14 20:02:04 +02:00
|
|
|
const port = process.env.HTTP_PORT || 3000;
|
|
|
|
|
const host = process.env.HTTP_HOST || "127.0.0.1";
|
|
|
|
|
const path = process.env.HTTP_PATH ?? "/api";
|
|
|
|
|
const server = api.start(port, host, path);
|
|
|
|
|
ws.start(server);
|
2020-08-08 23:59:13 +02:00
|
|
|
|
2023-10-14 20:02:04 +02:00
|
|
|
INFO("Versions:", versions);
|
2020-10-02 01:34:49 +02:00
|
|
|
|
2023-10-14 20:04:52 +02:00
|
|
|
periodicTasks.start();
|
2022-02-27 18:36:30 +01:00
|
|
|
|
2025-08-15 14:45:46 +02:00
|
|
|
const projectConfigurations = await getProjectConfigurations();
|
|
|
|
|
const handlerSystem = await setupEventHandlers(projectConfigurations);
|
2023-10-14 20:02:04 +02:00
|
|
|
|
2023-10-14 20:07:19 +02:00
|
|
|
process.on("SIGINT", async () => {
|
|
|
|
|
DEBUG("Interrupted (SIGINT)");
|
2025-08-15 14:45:46 +02:00
|
|
|
handlerSystem.close();
|
2023-10-14 20:07:19 +02:00
|
|
|
await periodicTasks.cleanup();
|
|
|
|
|
process.exit(0);
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
process.on("SIGHUP", async () => {
|
|
|
|
|
DEBUG("Stopping (SIGHUP)");
|
2025-08-15 14:45:46 +02:00
|
|
|
handlerSystem.close();
|
2023-10-14 20:07:19 +02:00
|
|
|
await periodicTasks.cleanup();
|
|
|
|
|
process.exit(0);
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
process.on('beforeExit', async () => {
|
|
|
|
|
DEBUG("Preparing to exit");
|
2025-08-15 14:45:46 +02:00
|
|
|
handlerSystem.close();
|
2023-10-14 20:07:19 +02:00
|
|
|
await periodicTasks.cleanup();
|
|
|
|
|
});
|
|
|
|
|
|
2023-10-14 20:02:54 +02:00
|
|
|
process.on('exit', async () => {
|
|
|
|
|
DEBUG("Exiting");
|
|
|
|
|
});
|
2023-10-14 20:02:04 +02:00
|
|
|
} catch (err) {
|
|
|
|
|
ERROR(err);
|
|
|
|
|
process.exit(2);
|
|
|
|
|
}
|
2022-02-06 22:52:45 +01:00
|
|
|
})
|
2022-02-27 18:36:30 +01:00
|
|
|
.catch( ({current, wanted, component}) => {
|
|
|
|
|
console.error(`Fatal error: incompatible ${component} version ${current} (wanted: ${wanted})`);
|
2022-05-12 22:09:08 +02:00
|
|
|
ERROR(`Fatal error: incompatible ${component} version ${current} (wanted: ${wanted})`);
|
2022-02-06 22:52:45 +01:00
|
|
|
process.exit(1);
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-10-02 01:34:49 +02:00
|
|
|
|
2022-02-06 22:52:45 +01:00
|
|
|
main();
|