2021-09-27 11:40:38 -04:00
|
|
|
const path = require("path");
|
|
|
|
const Bree = require("bree");
|
|
|
|
const { SHARE_ENV } = require("worker_threads");
|
2022-04-13 11:33:37 -04:00
|
|
|
const { log } = require("../src/util");
|
2022-04-05 07:41:29 -04:00
|
|
|
let bree;
|
2021-09-27 11:40:38 -04:00
|
|
|
const jobs = [
|
|
|
|
{
|
|
|
|
name: "clear-old-data",
|
2021-10-09 12:16:29 -04:00
|
|
|
interval: "at 03:14",
|
2021-10-23 09:44:21 -04:00
|
|
|
},
|
2021-09-27 11:40:38 -04:00
|
|
|
];
|
|
|
|
|
2022-04-20 14:56:40 -04:00
|
|
|
/**
|
|
|
|
* Initialize background jobs
|
|
|
|
* @param {Object} args Arguments to pass to workers
|
|
|
|
* @returns {Bree}
|
|
|
|
*/
|
2021-09-27 11:40:38 -04:00
|
|
|
const initBackgroundJobs = function (args) {
|
2022-04-05 07:41:29 -04:00
|
|
|
bree = new Bree({
|
2021-09-27 11:40:38 -04:00
|
|
|
root: path.resolve("server", "jobs"),
|
|
|
|
jobs,
|
|
|
|
worker: {
|
|
|
|
env: SHARE_ENV,
|
|
|
|
workerData: args,
|
|
|
|
},
|
2021-10-09 11:33:47 -04:00
|
|
|
workerMessageHandler: (message) => {
|
2022-04-13 11:33:37 -04:00
|
|
|
log.info("jobs", message);
|
2021-10-09 11:33:47 -04:00
|
|
|
}
|
2021-09-27 11:40:38 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
bree.start();
|
|
|
|
return bree;
|
|
|
|
};
|
|
|
|
|
2023-01-05 17:19:05 -05:00
|
|
|
/** Stop all background jobs if running */
|
2022-04-05 07:41:29 -04:00
|
|
|
const stopBackgroundJobs = function () {
|
|
|
|
if (bree) {
|
|
|
|
bree.stop();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-27 11:40:38 -04:00
|
|
|
module.exports = {
|
2022-04-05 07:41:29 -04:00
|
|
|
initBackgroundJobs,
|
|
|
|
stopBackgroundJobs
|
2021-09-27 11:40:38 -04:00
|
|
|
};
|