UNPKG

@ose4g/cron-manager

Version:

Npm package for ease of working with cron jobs

31 lines (30 loc) 1.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.cronJob = exports.JOB_SYMBOL = void 0; require("reflect-metadata"); var cron = require("node-cron"); exports.JOB_SYMBOL = Symbol('job'); /** * * Decorator for the cron job method * @param cronExpression : Expression defining how often the function should run * @param handlerTag : tag to uniquely identify the handler so the handler can be started or stopped using the tag * @returns : MethodDecorator */ function cronJob(cronExpression, handlerTag) { return function (target, propertyKey, descriptor) { var isValid = cron.validate(cronExpression); if (!isValid) throw new Error("".concat(cronExpression, " is not a valid cron expression")); var allHandlers = Reflect.getMetadata(exports.JOB_SYMBOL, target.constructor) || []; allHandlers.push({ handlerTag: handlerTag, cronExpression: cronExpression, func: propertyKey, constructor: target.constructor, className: target.constructor.name, }); Reflect.defineMetadata(exports.JOB_SYMBOL, allHandlers, target.constructor); }; } exports.cronJob = cronJob;