Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import {Validator} from 'class-validator';
- import * as schedule from 'node-schedule';
- import * as Providers from './providers/index';
- /**
- * Instance to validations.
- */
- const validator = new Validator();
- /**
- * Instance to cron job rules.
- */
- const rule = new schedule.RecurrenceRule();
- /**
- * Class to execute cron jobs of the application. The Cron manager.
- */
- export class Job {
- /**
- * Stores Cron Jobs configurations.
- */
- private readonly config: Array<Object>;
- /**
- * Constructor.
- *
- * @param {Object} config Environment configuration object.
- */
- constructor(config) {
- if (validator.isDefined(config.cronJobs.providers)) {
- this.config = config.cronJobs.providers;
- }
- }
- /**
- * Gets class name of the service provider.
- *
- * @param {Object} config Cron Jobs configuration object.
- * @param {number} index Current position on config object iteration.
- *
- * @return string
- */
- private static getClassName(config: Object, index: number) : string {
- return validator.isDefined(Object.keys(config)[index])
- ? Object.keys(config)[index]
- : '';
- }
- /**
- * Gets instance of the service provider.
- *
- * @param {string} className The name of the class.
- *
- * @return Object
- */
- private static getProviderInstance(className: string) : Object {
- let instance: Object;
- if(validator.isDefined(Providers[className])
- && validator.isInstance(new Providers[className], Providers[className])) {
- instance = new Providers[className];
- }
- return instance;
- }
- /**
- * Gets the jobs configuration by service provider.
- *
- * @param {Object} providerConfig Configuration to the current service provider.
- * @param {string} className The class name to the service provider.
- *
- * @return Array
- */
- private static getJobsConfiguration(providerConfig: Object, className: string) : Array<Object> {
- let jobsConfig = [];
- if (providerConfig.hasOwnProperty(className)
- && validator.isDefined(providerConfig[className].jobs)) {
- jobsConfig = providerConfig[className].jobs.filter(item => item.active);
- }
- return jobsConfig;
- }
- /**
- * Creates the schedules of the jobs.
- *
- * @param {Object} jobConfiguration The Jobs configurations.
- * @param {Function} task Method of the provider instance to execute by scheduler.
- */
- private static buildScheduleJob(jobConfiguration: Object, task: Function) : void {
- if (jobConfiguration.hasOwnProperty('timezone')) {
- rule.tz = jobConfiguration['timezone'];
- }
- if (jobConfiguration.hasOwnProperty('time')) {
- for(let prop in jobConfiguration['time']) {
- if (jobConfiguration['time'].hasOwnProperty(prop)) {
- rule[prop] = jobConfiguration['time'][prop]
- }
- }
- }
- schedule.scheduleJob(rule, task);
- }
- /**
- * Creates the cron jobs.
- *
- * @param {Object} jobConfiguration The Jobs configurations.
- * @param {Object} instance Instance to the service provider.
- */
- public static createCronJobs(jobConfiguration: Array<Object>, instance: Object) : void {
- if (validator.isDefined(instance) && jobConfiguration.length > 0) {
- jobConfiguration.forEach(jobConfig => {
- if (jobConfig.hasOwnProperty('name')
- && typeof instance[jobConfig['name']] === 'function') {
- this.buildScheduleJob(jobConfig, instance[jobConfig['name']])
- }
- });
- }
- }
- /**
- * Runs the cron manager.
- */
- public run() : void {
- if (validator.isArray(this.config)) {
- let className: string;
- let providerInstance: Object;
- let jobConfiguration: Array<Object>;
- this.config.forEach((providerConfig, index) => {
- className = Job.getClassName(providerConfig, index);
- providerInstance = Job.getProviderInstance(className);
- jobConfiguration = Job.getJobsConfiguration(providerConfig, className);
- Job.createCronJobs(jobConfiguration, providerInstance);
- });
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement