2021-10-09 15:33:47 +00:00
|
|
|
const { parentPort, workerData } = require("worker_threads");
|
|
|
|
const Database = require("../database");
|
|
|
|
const path = require("path");
|
|
|
|
|
2022-04-16 10:10:51 +00:00
|
|
|
/**
|
2022-04-22 17:42:47 +00:00
|
|
|
* Send message to parent process for logging
|
|
|
|
* since worker_thread does not have access to stdout, this is used
|
|
|
|
* instead of console.log()
|
2022-04-16 10:10:51 +00:00
|
|
|
* @param {any} any The message to log
|
|
|
|
*/
|
2021-10-09 15:33:47 +00:00
|
|
|
const log = function (any) {
|
|
|
|
if (parentPort) {
|
|
|
|
parentPort.postMessage(any);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-04-16 10:10:51 +00:00
|
|
|
/**
|
|
|
|
* Exit the worker process
|
|
|
|
* @param {number} error The status code to exit
|
|
|
|
*/
|
2021-10-09 15:33:47 +00:00
|
|
|
const exit = function (error) {
|
2022-04-17 07:43:03 +00:00
|
|
|
if (error && error !== 0) {
|
2021-10-09 15:33:47 +00:00
|
|
|
process.exit(error);
|
|
|
|
} else {
|
|
|
|
if (parentPort) {
|
|
|
|
parentPort.postMessage("done");
|
|
|
|
} else {
|
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-04-16 10:10:51 +00:00
|
|
|
/** Connects to the database */
|
2021-10-09 15:33:47 +00:00
|
|
|
const connectDb = async function () {
|
|
|
|
const dbPath = path.join(
|
|
|
|
process.env.DATA_DIR || workerData["data-dir"] || "./data/"
|
|
|
|
);
|
|
|
|
|
|
|
|
Database.init({
|
|
|
|
"data-dir": dbPath,
|
|
|
|
});
|
|
|
|
|
|
|
|
await Database.connect();
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
log,
|
|
|
|
exit,
|
|
|
|
connectDb,
|
|
|
|
};
|