UNPKG

phx-node

Version:

PHX NODE

91 lines 5.25 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var PHXCloneCronjob_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.PHXCloneCronjob = void 0; const common_1 = require("@nestjs/common"); const schedule_1 = require("@nestjs/schedule"); const cron_1 = require("cron"); const sentry_monitor_1 = require("../../sentry-config/sentry-monitor"); let PHXCloneCronjob = PHXCloneCronjob_1 = class PHXCloneCronjob { constructor(schedulerRegistry) { this.schedulerRegistry = schedulerRegistry; this.logger = new common_1.Logger(PHXCloneCronjob_1.name); // queue promise dùng chung cho toàn bộ cron this.executionQueue = Promise.resolve(); } // Service này thực hiện nhân bản cronjob dựa trên các cấu hình được truyền vào, bao gồm: // cronName: Tên của cronjob. // schoolCode: Mã trường. // hostName: Dùng để truy vấn theo mô hình mới. // cronTime: Thời gian chạy cronjob. // taskService: Service nghiệp vụ thực thi. // monitorConfig: Cấu hình giám sát trên Sentry, bao gồm: // checkinMargin: Khoảng thời gian cho phép (tính bằng phút) sau thời gian dự kiến check-in mà hệ thống không coi việc check-in là bị bỏ lỡ. // maxRuntime: Thời gian tối đa (tính bằng phút) mà quá trình giám sát có thể ở trạng thái "đang tiến hành" trước khi bị coi là thất bại do hết thời gian chờ. // Mỗi cronjob được nhân bản sẽ có một monitor tương ứng được tạo trên Sentry. init(_a) { return __awaiter(this, arguments, void 0, function* ({ cronName, cronTime, task, }) { const jobKey = cronName; const job = new cron_1.CronJob(cronTime, () => { this.logger.log(`[${cronName}] cron job added to queue`); this.executionQueue = this.executionQueue .then(() => __awaiter(this, void 0, void 0, function* () { this.logger.log(`[${cronName}] cron job is running...`); yield task(); this.logger.log(`[${cronName}] cron job completed`); })) .catch((error) => { this.logger.error(`[${cronName}] cron job failed`, (error === null || error === void 0 ? void 0 : error.stack) || error); }); }); this.schedulerRegistry.addCronJob(jobKey, job); job.start(); }); } initV2(_a) { return __awaiter(this, arguments, void 0, function* ({ cronName, cronTime, task, }) { const jobKey = cronName; const job = new cron_1.CronJob(cronTime, task); this.schedulerRegistry.addCronJob(jobKey, job); job.start(); }); } checkInSentry(cronTime, taskService, monitorConfig, monitorSlug, hostName, logger) { return __awaiter(this, void 0, void 0, function* () { try { const cronTask = () => __awaiter(this, void 0, void 0, function* () { logger.log(`[${monitorSlug}] cron job is running...`); yield taskService.init(hostName); //Hàm chính của 1 service cron BẮT BUỘC phải đặt tên là init() }); yield (0, sentry_monitor_1.runWithSentryCheckIn)(monitorSlug, cronTime, cronTask, monitorConfig); } catch (err) { logger.error(err); } }); } }; exports.PHXCloneCronjob = PHXCloneCronjob; exports.PHXCloneCronjob = PHXCloneCronjob = PHXCloneCronjob_1 = __decorate([ (0, common_1.Injectable)(), __metadata("design:paramtypes", [schedule_1.SchedulerRegistry]) ], PHXCloneCronjob); //# sourceMappingURL=clone-cronjob.service.js.map