UNPKG

node-cron-management

Version:
165 lines (164 loc) 6.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CronManager = void 0; const node_cron_1 = require("node-cron"); const decorators_1 = require("./decorators"); class CronManager { constructor() { this.instanceMap = new Map(); this.handlerTagCronJobMap = new Map(); this.groupTagCronJobMap = new Map(); this.allJobs = []; this.initialized = false; const groups = Reflect.getMetadata(decorators_1.GROUP_SYMBOL, Reflect) || []; //initially instantiate each class name instance to null groups.forEach((value) => { const { className } = value; if (this.instanceMap.has(className)) throw new Error(`duplicate key for ${className}`); this.instanceMap.set(className, null); }); } /** * Registers an instance to a class * @param Class : The Class * @param instance : Instance of the class given */ register(Class, instance) { if (!this.instanceMap.has(Class.name)) throw new Error(`class ${Class.name} has not been decorated with @cronGroup`); if (Class.name !== instance.constructor.name) throw new Error(`instance is not of type ${Class.name}`); this.instanceMap.set(Class.name, instance); } /** * gets all the handlers and adds them to the various Maps and lists in the appropriate manner * @returns void */ init() { if (this.initialized) return; const groups = Reflect.getMetadata(decorators_1.GROUP_SYMBOL, Reflect) || []; //get the handlers for each group and then add their handlers to the right places groups.forEach((value) => { const { groupTag, constructor } = value; const handlers = Reflect.getMetadata(decorators_1.JOB_SYMBOL, constructor) || []; //for each handler create the scheduled task and add it into all jobs first handlers.forEach((handler) => { const { func, cronExpression, className, handlerTag, timezone } = handler; if (!this.instanceMap.has(className)) throw new Error(`class ${className} was not decorated with cronGroup tag`); const instance = this.instanceMap.get(className); if (!instance) throw new Error(`class ${className} has not been registered`); const options = { scheduled: false }; if (timezone && timezone !== "") { options.timezone = timezone; } const job = (0, node_cron_1.schedule)(cronExpression, () => { instance[func](); }, options); this.allJobs.push(job); //add the job to the global list of jobs. //add to the group tags list. if (groupTag) { const jobs = this.groupTagCronJobMap.get(groupTag) || []; jobs.push(job); this.groupTagCronJobMap.set(groupTag, jobs); } //set the job for that specific handler tag; if (handlerTag) { if (this.handlerTagCronJobMap.has(handlerTag)) throw new Error(`tag ${handlerTag} is already in use`); this.handlerTagCronJobMap.set(handlerTag, job); } }); }); this.initialized = true; } /** * starts all cron jobs */ startAll() { this.init(); this.allJobs.forEach((job) => { job.start(); }); } /** * stops all cron jobs */ stopAll() { this.init(); this.allJobs.forEach((job) => { job.stop(); }); } /** * starts or stops all jobs defined in a group with groupTag = groupTag * @param groupTag : unique tag of the group * @param start : set to true to start the job. Set to false to stop the job. it defaults to true if not set. */ startGroupLogic(groupTag, start = true) { this.init(); if (!this.groupTagCronJobMap.has(groupTag)) throw new Error(`group tag ${groupTag} not found`); const jobs = this.groupTagCronJobMap.get(groupTag) || []; jobs === null || jobs === void 0 ? void 0 : jobs.forEach((job) => { if (start) job.start(); else job.stop(); }); } /** * * @param handlerTag unique tag of the handler * @param start set to true to start the job. Set to false to stop the job. it defaults to true if not set. */ startHandlerLogic(handlerTag, start = true) { this.init(); if (!this.handlerTagCronJobMap.has(handlerTag)) throw new Error(`handler tag ${handlerTag} not found`); const job = this.handlerTagCronJobMap.get(handlerTag); if (start) job === null || job === void 0 ? void 0 : job.start(); else job === null || job === void 0 ? void 0 : job.stop(); } /** * Starts all jobs with groupTag * @param groupTag */ startGroup(groupTag) { this.startGroupLogic(groupTag); } /** * Stops all jobs with group groupTag * @param groupTag */ stopGroup(groupTag) { this.startGroupLogic(groupTag, false); } /** * Starts the job with handlerTag * @param handlerTag */ startHandler(handlerTag) { this.startHandlerLogic(handlerTag); } /** * stops the job with handlerTag * @param handlerTag */ stopHandler(handlerTag) { this.startHandlerLogic(handlerTag, false); } getGroups() { const groups = Array.from(this.groupTagCronJobMap.keys()); return groups; } getHandlers() { return Array.from(this.handlerTagCronJobMap.keys()); } } exports.CronManager = CronManager;