UNPKG

@openinc/parse-server-opendash

Version:
134 lines (133 loc) 4.97 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const bree_1 = __importDefault(require("bree")); const dayjs_1 = __importDefault(require("dayjs")); const duration_1 = __importDefault(require("dayjs/plugin/duration")); dayjs_1.default.extend(duration_1.default); class BreeInstance { constructor() { } static getBree() { if (this.instance === null) { // Create a new instance of Bree this.instance = new bree_1.default({ // Bree configuration options errorHandler: (error, workerMetadata) => { console.error(`An error occurred in worker ${workerMetadata.name} with the following error: ${error}`); }, workerMessageHandler: (message) => { console.log(`Worker ${message.name} sent the following message: ${message.message}`); }, root: false, //path.join(__dirname, "..", "jobs") jobs: this.jobs, acceptedExtensions: ["js"], defaultExtension: "js", outputWorkerMetadata: false, }); } return this.instance; } static getAllJobs() { return this.jobs; } /** * Converts a cron-like string into a human-readable format. * * @param value - The cron-like string to convert. * @returns A human-readable string representing the cron-like schedule. */ static createHumanReadableFormat(value) { //value looks like this: "1 1 1 1 1 1 1" //1. seconds //2. minutes //3. hours //4. days //5. weeks //6. months //7. years const valueArray = value.split(" "); //Should be in the format: "every 1 second" when second is 1 //If any value is 0, it should be ignored //Values should be separated by a comma const returnArray = []; if (valueArray[0] !== "0") { returnArray.push(`every ${valueArray[0]} seconds`); } if (valueArray[1] !== "0") { returnArray.push(`every ${valueArray[1]} minutes`); } if (valueArray[2] !== "0") { returnArray.push(`every ${valueArray[2]} hours`); } if (valueArray[3] !== "0") { returnArray.push(`every ${valueArray[3]} days`); } if (valueArray[4] !== "0") { returnArray.push(`every ${valueArray[4]} weeks`); } if (valueArray[5] !== "0") { returnArray.push(`every ${valueArray[5]} months`); } if (valueArray[6] !== "0") { returnArray.push(`every ${valueArray[6]} years`); } return returnArray.join(", "); } /** * Calculates how much earlier a job should be scheduled based on the user input. * Users can set an offset like "2 weeks" or "3 days" to schedule the job earlier. * This function will substract the offset from startdate and return a new date. */ static calculateOffsetToExecution(startdate, notifyBeforeDue) { const result = (0, dayjs_1.default)(startdate).subtract(dayjs_1.default.duration({ days: notifyBeforeDue.value })); return result.toDate(); } static async addJob(newjob) { if (newjob === undefined) { console.error("newjob is undefined"); return; } //Get all jobs from bree and check whether the job already exists const jobs = BreeInstance.getAllJobs(); let jobExists = false; for (const job of jobs) { if (job.name === newjob.name) { jobExists = true; break; } } if (jobExists) { console.log(`Job with name ${newjob.name} already exists`); return; } console.log(`Adding job with jobname ${newjob.name}`); return await BreeInstance.getBree().add(newjob); } static async removeJob(jobName) { console.log(`Removing job with jobname ${jobName}`); return BreeInstance.getBree().remove(jobName); } static getJobByName(jobName) { const jobs = BreeInstance.getAllJobs(); for (const job of jobs) { if (job.name === jobName) { return job; } } return undefined; } /** * Constructs a job name based on the schedule ID and job type. * @param id - The ID of the schedule. * @param jobType - The type of the job (e.g., "notification before due"). * @returns A string representing the constructed job name. */ static constructJobName(id, jobType) { return `${id}_${jobType}`; } } BreeInstance.instance = null; BreeInstance.jobs = []; exports.default = BreeInstance;