@topgroup/diginext
Version:
A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.
73 lines (72 loc) • 3.64 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.findAndRunCronjob = void 0;
const chalk_1 = __importDefault(require("chalk"));
const dayjs_1 = __importDefault(require("dayjs"));
const localizedFormat_1 = __importDefault(require("dayjs/plugin/localizedFormat"));
const log_1 = require("diginext-utils/dist/xconsole/log");
const Cronjob_1 = require("../../entities/Cronjob");
const run_job_1 = require("./run-job");
dayjs_1.default.extend(localizedFormat_1.default);
const findAndRunCronjob = async (workspaceId) => {
const { DB } = await Promise.resolve().then(() => __importStar(require("../../modules/api/DB")));
const now = (0, dayjs_1.default)().toDate();
const filter = { nextRunAt: { $lte: now } };
if (workspaceId)
filter.workspace = workspaceId;
const jobs = await DB.find("cronjob", filter);
if (jobs === null || jobs === void 0 ? void 0 : jobs.length) {
// find & execute jobs...
const foundJobs = jobs
.map((job) => {
if (job.repeatCondition) {
const { atDays, atHours, atMins, atMonths, atWeekDays } = job.repeatCondition;
if (atMins && atMins.length > 0 && !atMins.includes((0, dayjs_1.default)().minute()))
return;
if (atHours && atHours.length > 0 && !atHours.includes((0, dayjs_1.default)().hour()))
return;
if (atDays && atDays.length > 0 && !atDays.includes((0, dayjs_1.default)().date()))
return;
if (atMonths && atMonths.length > 0 && !atMonths.includes((0, dayjs_1.default)().month()))
return;
if (atWeekDays && atWeekDays.length > 0 && !atWeekDays.includes(Cronjob_1.weekDays[(0, dayjs_1.default)().day()]))
return;
}
(0, run_job_1.runCronjob)(job);
return job;
})
.filter((job) => typeof job !== "undefined");
if (foundJobs.length)
(0, log_1.logSuccess)(`[CRONJOB] Cronjob checking... \nFound & execute ${foundJobs.length} jobs: ${foundJobs.map((job, index) => chalk_1.default.magenta(`\n [${index}] ${job.name} (ID: ${job._id}) [Run at: ${(0, dayjs_1.default)(job.nextRunAt).format("llll")}]`))}`);
}
// else {
// if (IsDev()) log(`[CRONJOB] Cronjob checking: found ${jobs.length} to execute.`);
// }
};
exports.findAndRunCronjob = findAndRunCronjob;