UNPKG

phx-node

Version:

PHX NODE

194 lines 11 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 PHXCronjobManagerService_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.PHXCronjobManagerService = void 0; const common_1 = require("@nestjs/common"); const clone_cronjob_service_1 = require("./clone-cronjob.service"); // import { PHXHostnameService } from "../hostname/hostname.service"; const config_by_hostname_service_1 = require("./config-by-hostname.service"); const schoollist_service_1 = require("./schoollist.service"); const query_1 = require("./query"); const rxjs_1 = require("rxjs"); const axios_1 = require("@nestjs/axios"); const config_1 = require("@nestjs/config"); let PHXCronjobManagerService = PHXCronjobManagerService_1 = class PHXCronjobManagerService { constructor(cloneCronjob, // private readonly hostnameService: PHXHostnameService, configByHostNameService, schoolListService, httpService, configService) { this.cloneCronjob = cloneCronjob; this.configByHostNameService = configByHostNameService; this.schoolListService = schoolListService; this.httpService = httpService; this.configService = configService; this.logger = new common_1.Logger(PHXCronjobManagerService_1.name); } init(listService) { return __awaiter(this, void 0, void 0, function* () { this.logger.log("Running get list school"); let listHostName = []; let listConfigCronjobByHostname = []; try { [listHostName, listConfigCronjobByHostname] = yield Promise.all([ this.schoolListService.getAllSchool(), this.configByHostNameService.getCronConfig(), ]); } catch (error) { this.logger.error("Fail to get config cron", error); } if (listHostName.length === 0 || listConfigCronjobByHostname.length === 0) { this.logger.warn("Config cronjob is empty!"); } for (const { cronName, cronTime, taskService } of listService) { yield this.cloneCronjob.init({ cronName, cronTime, task: () => __awaiter(this, void 0, void 0, function* () { var _a, _b; if (listHostName.length === 0 || listConfigCronjobByHostname.length === 0) { return; } for (const { hostname, school_id, isTenantMode } of listHostName) { // hostname get config mapping const hostnameConfig = isTenantMode ? query_1.HOSTNAME_MULTI_TESTING : hostname; // hostname call grpc const hostnameRun = isTenantMode ? `${query_1.HOSTNAME_MULTI_TESTING}?tenant_id=${school_id}` : hostname; const configItem = listConfigCronjobByHostname.find((item) => (item === null || item === void 0 ? void 0 : item.school.hostname) === hostnameConfig); if (!configItem) { this.logger.error(`Chưa cấu hình cronjob cho hostname: ${hostnameConfig}`); continue; } const runType = configItem.cronjob_config_type_run.code; const listCronName = (_b = (_a = configItem.metadata) === null || _a === void 0 ? void 0 : _a.list_cron_name) !== null && _b !== void 0 ? _b : []; const isCronJobEnable = this.configByHostNameService.checkCronjobEnable({ runType, cronName, listCronName, }); if (!isCronJobEnable) continue; try { this.logger.log(`[${hostnameRun}, ${cronName}] start processing`); yield taskService.init(hostnameRun); this.logger.log(`[${hostnameRun}, ${cronName}] completed`); } catch (error) { this.logger.error(`[${hostnameRun}, ${cronName}] failed`, (error === null || error === void 0 ? void 0 : error.stack) || error); } } }), }); } }); } initV2(listService) { return __awaiter(this, void 0, void 0, function* () { this.logger.log("Running get list school"); let listHostName = []; let listConfigCronjobByHostname = []; try { [listHostName, listConfigCronjobByHostname] = yield Promise.all([ this.schoolListService.getAllSchool(), this.configByHostNameService.getCronConfig(), ]); } catch (error) { this.logger.error("Fail to get config cron", error); } if (listHostName.length === 0 || listConfigCronjobByHostname.length === 0) { this.logger.warn("Config cronjob is empty!"); } for (const { cronName, cronTime, eventPattern } of listService) { yield this.cloneCronjob.initV2({ cronName, cronTime, task: () => __awaiter(this, void 0, void 0, function* () { var _a, _b; if (listHostName.length === 0 || listConfigCronjobByHostname.length === 0) { return; } for (const { hostname, school_id, isTenantMode } of listHostName) { // hostname get config mapping const hostnameConfig = isTenantMode ? query_1.HOSTNAME_MULTI_TESTING : hostname; // hostname call grpc const hostnameRun = isTenantMode ? `${query_1.HOSTNAME_MULTI_TESTING}?tenant_id=${school_id}` : hostname; const configItem = listConfigCronjobByHostname.find((item) => (item === null || item === void 0 ? void 0 : item.school.hostname) === hostnameConfig); if (!configItem) { this.logger.error(`Chưa cấu hình cronjob cho hostname: ${hostnameConfig}`); continue; } const runType = configItem.cronjob_config_type_run.code; const listCronName = (_b = (_a = configItem.metadata) === null || _a === void 0 ? void 0 : _a.list_cron_name) !== null && _b !== void 0 ? _b : []; const isCronJobEnable = this.configByHostNameService.checkCronjobEnable({ runType, cronName, listCronName, }); if (!isCronJobEnable) continue; try { this.logger.log(`[${hostnameRun}, ${cronName}] start processing`); const producerApi = this.configService.get("PRODUCER_API"); if (!producerApi) { const errorMessage = `${hostname}: PRODUCER_API is not configured`; this.logger.debug(errorMessage); throw new Error(errorMessage); } yield (0, rxjs_1.lastValueFrom)(this.httpService.post(`${producerApi}/${eventPattern}`, { hostname: hostnameRun, cronName, }, { headers: { hostname, }, })); this.logger.log(`[${hostnameRun}, ${cronName}] enqueue completed`); } catch (error) { this.logger.error(`[${hostnameRun}, ${cronName}] failed`, (error === null || error === void 0 ? void 0 : error.stack) || error); } } }), }); } }); } }; exports.PHXCronjobManagerService = PHXCronjobManagerService; exports.PHXCronjobManagerService = PHXCronjobManagerService = PHXCronjobManagerService_1 = __decorate([ (0, common_1.Injectable)(), __metadata("design:paramtypes", [clone_cronjob_service_1.PHXCloneCronjob, config_by_hostname_service_1.PHXConfigByHostNameService, schoollist_service_1.SchoolListService, axios_1.HttpService, config_1.ConfigService]) ], PHXCronjobManagerService); //# sourceMappingURL=cronjob-manager.service.js.map