UNPKG

@topgroup/diginext

Version:

A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.

98 lines (97 loc) 6.76 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.calculateNextRunAt = void 0; const chalk_1 = __importDefault(require("chalk")); const dayjs_1 = __importDefault(require("dayjs")); const Cronjob_1 = require("../../entities/Cronjob"); const array_1 = require("../../plugins/array"); const calculateNextRunAt = (job, options) => { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5; let nextRunAt; if (options === null || options === void 0 ? void 0 : options.isDebugging) { console.log("------------------------------------"); console.log("Calculating next run :>> current date:"); console.log(`- Year: ${(0, dayjs_1.default)().year()}`); console.log(`- Month: ${(0, dayjs_1.default)().month()}`); console.log(`- Date: ${(0, dayjs_1.default)().date()}`); console.log(`- Hour: ${(0, dayjs_1.default)().hour()}`); console.log(`- Minute: ${(0, dayjs_1.default)().minute()}`); console.log(`- In mins: [${((_b = (_a = job.repeatCondition) === null || _a === void 0 ? void 0 : _a.atMins) === null || _b === void 0 ? void 0 : _b.join(", ")) || " "}]`); console.log(`- In hours: [${((_d = (_c = job.repeatCondition) === null || _c === void 0 ? void 0 : _c.atHours) === null || _d === void 0 ? void 0 : _d.join(", ")) || " "}]`); console.log(`- In days: [${((_f = (_e = job.repeatCondition) === null || _e === void 0 ? void 0 : _e.atDays) === null || _f === void 0 ? void 0 : _f.join(", ")) || " "}]`); console.log(`- In months: [${((_h = (_g = job.repeatCondition) === null || _g === void 0 ? void 0 : _g.atMonths) === null || _h === void 0 ? void 0 : _h.join(", ")) || " "}]`); console.log(`- In weekdays: [${((_k = (_j = job.repeatCondition) === null || _j === void 0 ? void 0 : _j.atWeekDays) === null || _k === void 0 ? void 0 : _k.join(", ")) || " "}]`); } let _nextDate = (0, dayjs_1.default)().add(job.repeat.range, job.repeat.unit); if (job.repeat && job.repeat.range && job.repeat.unit) { // repeat condition: /** * Examples: * ✓ Every month (monthly), at [weekdays: "mon", "tue", "fri"] * ✓ Every day (daily), at [weekdays: "mon", "tue", "fri"] * ✓ Every 5 minutes, at [hours: 13,14,18] * ✓ Every 2 hours, at [minutes: 5,10] and [weekdays: mon,wed,thu,sat] */ if (((_m = (_l = job.repeatCondition) === null || _l === void 0 ? void 0 : _l.atMins) === null || _m === void 0 ? void 0 : _m.length) > 0 && !((_o = job.repeatCondition) === null || _o === void 0 ? void 0 : _o.atMins.includes(_nextDate.minute()))) { const validMins = (_p = job.repeatCondition) === null || _p === void 0 ? void 0 : _p.atMins.sort((a, b) => a - b); const nextMin = validMins.filter((min) => min > _nextDate.minute())[0]; if (nextMin) { _nextDate = _nextDate.set("minute", nextMin); } else { _nextDate = _nextDate.add(1, "hour").set("minute", validMins[0]); } } if (((_r = (_q = job.repeatCondition) === null || _q === void 0 ? void 0 : _q.atHours) === null || _r === void 0 ? void 0 : _r.length) > 0 && !((_s = job.repeatCondition) === null || _s === void 0 ? void 0 : _s.atHours.includes(_nextDate.hour()))) { const validHours = (_t = job.repeatCondition) === null || _t === void 0 ? void 0 : _t.atHours.sort((a, b) => a - b); const nextHour = validHours.filter((hour) => hour > _nextDate.hour())[0]; if (nextHour) { _nextDate = _nextDate.set("hour", nextHour); } else { _nextDate = _nextDate.add(1, "day").set("hour", validHours[0]); } } if (((_v = (_u = job.repeatCondition) === null || _u === void 0 ? void 0 : _u.atDays) === null || _v === void 0 ? void 0 : _v.length) > 0 && !((_w = job.repeatCondition) === null || _w === void 0 ? void 0 : _w.atDays.includes(_nextDate.date()))) { const validDays = (_x = job.repeatCondition) === null || _x === void 0 ? void 0 : _x.atDays.sort((a, b) => a - b); const nextDay = validDays.filter((day) => day > _nextDate.date())[0]; if (nextDay) { _nextDate = _nextDate.set("date", nextDay); } else { _nextDate = _nextDate.add(1, "month").set("date", validDays[0]); } } if (((_z = (_y = job.repeatCondition) === null || _y === void 0 ? void 0 : _y.atMonths) === null || _z === void 0 ? void 0 : _z.length) > 0 && !((_0 = job.repeatCondition) === null || _0 === void 0 ? void 0 : _0.atMonths.includes(_nextDate.month()))) { const validMonths = (_1 = job.repeatCondition) === null || _1 === void 0 ? void 0 : _1.atMonths.sort((a, b) => a - b); const nextMonth = validMonths.filter((month) => month > _nextDate.month())[0]; if (nextMonth) { _nextDate = _nextDate.set("month", nextMonth); } else { _nextDate = _nextDate.add(1, "year").set("month", validMonths[0]); } } if (((_3 = (_2 = job.repeatCondition) === null || _2 === void 0 ? void 0 : _2.atWeekDays) === null || _3 === void 0 ? void 0 : _3.length) > 0 && !((_4 = job.repeatCondition) === null || _4 === void 0 ? void 0 : _4.atWeekDays.includes(Cronjob_1.weekDays[_nextDate.day()]))) { const validWeekdays = (0, array_1.sortedDaysOfWeek)((_5 = job.repeatCondition) === null || _5 === void 0 ? void 0 : _5.atWeekDays); const nextWeekday = validWeekdays.filter((weekday, index) => index > validWeekdays.indexOf(Cronjob_1.weekDays[_nextDate.day()]))[0]; if (nextWeekday) { _nextDate = _nextDate.set("day", Cronjob_1.weekDays.indexOf(nextWeekday)); } else { _nextDate = _nextDate.add(1, "week").set("day", Cronjob_1.weekDays.indexOf(validWeekdays[0])); } } // check end date nextRunAt = job.endDate && _nextDate.diff((0, dayjs_1.default)(job.endDate)) > 0 ? undefined : _nextDate.toDate(); } if (options === null || options === void 0 ? void 0 : options.isDebugging) { console.log(`=> NEXT RUN AT: ${chalk_1.default.yellow(_nextDate.format("llll"))}`); console.log("------------------------------------"); } return nextRunAt; }; exports.calculateNextRunAt = calculateNextRunAt;