@openinc/parse-server-opendash
Version:
Parse Server Cloud Code for open.INC Stack.
124 lines (123 loc) • 5.82 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.initScheduling = initScheduling;
exports.addJobToBree = addJobToBree;
const path_1 = __importDefault(require("path"));
const catchError_1 = require("../../../helper/catchError");
const types_1 = require("../../../types");
const cron_1 = __importDefault(require("../../cron"));
/**
* This function is called upon initialization and uses bree to schedule all jobs.
*/
async function initScheduling() {
console.log("Init scheduling Maintenance_Schedule_Template jobs");
//Get all Maintenance_Schedule_Template objects
const [queryError, query] = await (0, catchError_1.catchError)(new Parse.Query(types_1.Maintenance_Schedule_Template).find({
useMasterKey: true,
}));
//Create an new bree object for all Maintenance_Schedule_Template objects that have cron.is_one_time = false
if (queryError) {
console.error("Error while querying Maintenance_Schedule_Template objects", queryError);
return;
}
await addJobToBree(query);
}
async function addJobToBree(query) {
for (const schedule of query) {
const cron = schedule.get("cron");
const notifyBeforeDue = schedule.get("notifyBeforeDue");
// If the notifyBeforeDue field is set, we will schedule a job to run before the due date
if (notifyBeforeDue && notifyBeforeDue.value > 0) {
// Calculate the offset from the start date
const startdate = cron_1.default.calculateOffsetToExecution(schedule.get("cron").run_startdate, notifyBeforeDue);
//Add a job to bree to run at the calculated date
const newjob = {
name: cron_1.default.constructJobName(schedule.id, "Maintenance_Schedule_Notification_Before_Due"),
path: path_1.default.join(__dirname, "..", "jobs", "open_service_notifyBeforeSchedule.js"),
date: startdate,
interval: cron.is_one_time
? 0
: cron_1.default.createHumanReadableFormat(cron.run_cron),
hasSeconds: true,
worker: {
workerData: {
scheduleId: schedule.id,
ParseAppId: Parse.applicationId,
ParseJSKey: Parse.javaScriptKey,
ParseMasterKey: Parse.masterKey,
ParseServerURL: Parse.serverURL,
},
},
};
await cron_1.default.addJob(newjob);
}
try {
let newjob = undefined;
if (cron.is_one_time === false) {
if (cron.scheduletype === "human") {
newjob = {
name: cron_1.default.constructJobName(schedule.id, "Maintenance_Schedule"),
date: cron.run_startdate,
path: path_1.default.join(__dirname, "..", "jobs", "open_service_notifyOnSchedule.js"),
interval: cron_1.default.createHumanReadableFormat(cron.run_cron),
hasSeconds: true,
worker: {
workerData: {
scheduleId: schedule.id,
ParseAppId: Parse.applicationId,
ParseJSKey: Parse.javaScriptKey,
ParseMasterKey: Parse.masterKey,
ParseServerURL: Parse.serverURL,
},
},
};
}
else if (cron.scheduletype === "quartz") {
newjob = {
name: cron_1.default.constructJobName(schedule.id, "Maintenance_Schedule"),
cron: cron.run_cron,
path: path_1.default.join(__dirname, "..", "jobs", "open_service_notifyOnSchedule.js"),
timeout: 0,
interval: 0,
hasSeconds: true,
worker: {
workerData: {
scheduleId: schedule.id,
ParseAppId: Parse.applicationId,
ParseJSKey: Parse.javaScriptKey,
ParseMasterKey: Parse.masterKey,
ParseServerURL: Parse.serverURL,
},
},
};
}
}
else {
// If the job is a one time job, we will schedule it to run once
newjob = {
name: cron_1.default.constructJobName(schedule.id, "Maintenance_Schedule"),
date: cron.run_startdate,
path: path_1.default.join(__dirname, "..", "jobs", "open_service_notifyOnSchedule.js"),
interval: 0,
hasSeconds: true,
worker: {
workerData: {
scheduleId: schedule.id,
ParseAppId: Parse.applicationId,
ParseJSKey: Parse.javaScriptKey,
ParseMasterKey: Parse.masterKey,
ParseServerURL: Parse.serverURL,
},
},
};
}
await cron_1.default.addJob(newjob);
}
catch (error) {
console.warn(`Error while adding job for schedule ${schedule.id}`, error);
}
}
}