Files
dougal-software/lib/www/server/index.js

69 lines
1.8 KiB
JavaScript
Raw Permalink Normal View History

#!/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);
async function main () {
// Check that we're running against the correct database version
const version = require('./lib/version');
INFO("Running version", await version.describe());
version.compatible()
.then( (versions) => {
2023-10-14 20:02:04 +02:00
try {
const api = require('./api');
const ws = require('./ws');
const periodicTasks = require('./periodic-tasks').init();
2023-10-14 20:02:04 +02:00
const { fork } = require('child_process');
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);
periodicTasks.start();
2023-10-14 20:02:04 +02:00
const eventManagerPath = [__dirname, "events"].join("/");
const eventManager = fork(eventManagerPath, /*{ stdio: 'ignore' }*/);
2023-10-14 20:07:19 +02:00
process.on("SIGINT", async () => {
DEBUG("Interrupted (SIGINT)");
eventManager.kill()
await periodicTasks.cleanup();
process.exit(0);
})
process.on("SIGHUP", async () => {
DEBUG("Stopping (SIGHUP)");
eventManager.kill()
await periodicTasks.cleanup();
process.exit(0);
})
process.on('beforeExit', async () => {
DEBUG("Preparing to exit");
eventManager.kill()
await periodicTasks.cleanup();
});
process.on('exit', async () => {
DEBUG("Exiting");
// eventManager.kill()
// periodicTasks.cleanup();
});
2023-10-14 20:02:04 +02:00
} catch (err) {
ERROR(err);
process.exit(2);
}
})
.catch( ({current, wanted, component}) => {
console.error(`Fatal error: incompatible ${component} version ${current} (wanted: ${wanted})`);
ERROR(`Fatal error: incompatible ${component} version ${current} (wanted: ${wanted})`);
process.exit(1);
});
}
main();