@topgroup/diginext
Version:
A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.
96 lines (95 loc) • 4.95 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CronjobService = void 0;
const dayjs_1 = __importDefault(require("dayjs"));
const Cronjob_1 = require("../entities/Cronjob");
const calculate_next_run_at_1 = require("../modules/cronjob/calculate-next-run-at");
const array_1 = require("../plugins/array");
const BaseService_1 = __importDefault(require("./BaseService"));
class CronjobService extends BaseService_1.default {
constructor(ownership) {
super(Cronjob_1.cronjobSchema, ownership);
}
create(data, options) {
// validate
if (data.repeat) {
if (!data.repeat.range)
throw new Error(`"repeat.range" is required.`);
if (!data.repeat.unit)
throw new Error(`Cronjob "repeat.unit" is required, one of: ${Cronjob_1.cronjobRepeatUnitList.join(", ")}`);
if (!data.repeat.range)
data.repeat.range = 1;
if (data.repeat.range <= 0)
throw new Error(`Cronjob "repeat.range" cannot be zero or negative.`);
}
if (!data.repeatCondition)
data.repeatCondition = {};
/**
* 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 (data.repeat.unit === "minute") data.repeatCondition.atMins = undefined;
// if (data.repeat.unit === "hour") data.repeatCondition.atHours = undefined;
// if (data.repeat.unit === "day") data.repeatCondition.atDays = undefined;
// if (data.repeat.unit === "month") data.repeatCondition.atMonths = undefined;
// validate repeat conditions
// repeat conditions should be unique arrays
if (data.repeatCondition.atMins)
data.repeatCondition.atMins = (0, array_1.filterUniqueItems)(data.repeatCondition.atMins)
.map((item) => {
// validate
if (item < 0 || item > 59)
throw new Error(`Values in "repeatCondition.atMins" array are invalid, should be in a range of [0-59].`);
return item;
})
.sort((a, b) => a - b); // <-- SORT
if (data.repeatCondition.atHours)
data.repeatCondition.atHours = (0, array_1.filterUniqueItems)(data.repeatCondition.atHours)
.map((item) => {
// validate
if (item < 0 || item > 23)
throw new Error(`Values in "repeatCondition.atHours" array are invalid, should be in a range of [0-23].`);
return item;
})
.sort((a, b) => a - b); // <-- SORT
if (data.repeatCondition.atDays)
data.repeatCondition.atDays = (0, array_1.filterUniqueItems)(data.repeatCondition.atDays)
.map((item) => {
// validate
if (item < 1 || item > 31)
throw new Error(`Values in "repeatCondition.atDays" array are invalid, should be in a range of [1-31].`);
return item;
})
.sort((a, b) => a - b); // <-- SORT
if (data.repeatCondition.atMonths)
data.repeatCondition.atMonths = (0, array_1.filterUniqueItems)(data.repeatCondition.atMonths)
.map((item) => {
// validate
if (item < 0 || item > 11)
throw new Error(`Values in "repeatCondition.atMonths" array are invalid, should be in a range of [0-11].`);
return item;
})
.sort((a, b) => a - b); // <-- SORT
if (data.repeatCondition.atWeekDays)
data.repeatCondition.atWeekDays = (0, array_1.sortedDaysOfWeek)((0, array_1.filterUniqueItems)(data.repeatCondition.atWeekDays).map((item) => {
// validate
if (!Cronjob_1.weekDays.includes(item))
throw new Error(`Values in "repeatCondition.atWeekDays" array are invalid, should be in a range of [sun-mon-...-fri-sat].`);
return item;
}));
// validate end date
if (data.endDate && (0, dayjs_1.default)(data.endDate).diff((0, dayjs_1.default)()) < 0)
throw new Error(`Value of "endDate" must be in the future.`);
// calculate next run schedule:
data.nextRunAt = (0, calculate_next_run_at_1.calculateNextRunAt)(data, { isDebugging: options === null || options === void 0 ? void 0 : options.isDebugging });
// return
return super.create(data, options);
}
}
exports.CronjobService = CronjobService;