UNPKG

@topgroup/diginext

Version:

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

77 lines (76 loc) 3.62 kB
"use strict"; 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.runCronjob = void 0; const axios_1 = __importDefault(require("axios")); const dayjs_1 = __importDefault(require("dayjs")); const log_1 = require("diginext-utils/dist/xconsole/log"); const calculate_next_run_at_1 = require("./calculate-next-run-at"); const runCronjob = async (job) => { const { DB } = await Promise.resolve().then(() => __importStar(require("../../modules/api/DB"))); // call api request of the cronjob: (0, axios_1.default)({ url: `${job.url}`, params: job.params, headers: job.headers, data: job.body || {}, method: job.method || "GET", }) .then(async ({ data: responseData, status: responseStatus }) => { (0, log_1.logSuccess)(`[CRONJOB] Job "${job.name}" (${job._id}) has been executed successfully:`, responseData); // add to cronjob's history: const cronjobHistory = { runAt: new Date(), status: "success", responseStatus, message: "Ok", }; const updatedJob = await DB.updateOne("cronjob", { _id: job._id }, { $push: { history: cronjobHistory } }, { raw: true }); }) .catch(async (e) => { var _a, _b; (0, log_1.logError)(`[CRONJOB] Job "${job.name}" (${job._id}) failed:`, e); // add to cronjob's history: const cronjobHistory = { runAt: new Date(), status: "failed", responseStatus: ((_a = e.data) === null || _a === void 0 ? void 0 : _a.status) || ((_b = e.response) === null || _b === void 0 ? void 0 : _b.status) || e.status, message: e.toString(), }; const updatedJob = await DB.updateOne("cronjob", { _id: job._id }, { $push: { history: cronjobHistory } }, { raw: true }); }); // schedule a next run: const nextRunAt = (0, calculate_next_run_at_1.calculateNextRunAt)(job); const updateData = nextRunAt ? { nextRunAt } : { $unset: { nextRunAt: 1 } }; const updatedJob = await DB.updateOne("cronjob", { _id: job._id }, updateData, { raw: true }); if (!updatedJob) (0, log_1.logWarn)(`[CRONJOB] Job "${job.name}" (${job._id}) > Unable to set next schedule.`); if (nextRunAt) (0, log_1.log)(`[CRONJOB] Job "${job.name}" (${job._id}) > Next schedule: ${(0, dayjs_1.default)(nextRunAt).format("llll")}`); }; exports.runCronjob = runCronjob;