Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {Validator} from 'class-validator';
  2. import * as schedule from 'node-schedule';
  3. import * as Providers from './providers/index';
  4.  
  5. /**
  6.  * Instance to validations.
  7.  */
  8. const validator = new Validator();
  9. /**
  10.  * Instance to cron job rules.
  11.  */
  12. const rule = new schedule.RecurrenceRule();
  13.  
  14. /**
  15.  * Class to execute cron jobs of the application. The Cron manager.
  16.  */
  17. export class Job {
  18.   /**
  19.    * Stores Cron Jobs configurations.
  20.    */
  21.   private readonly config: Array<Object>;
  22.  
  23.   /**
  24.    * Constructor.
  25.    *
  26.    * @param {Object} config Environment configuration object.
  27.    */
  28.   constructor(config) {
  29.     if (validator.isDefined(config.cronJobs.providers)) {
  30.       this.config = config.cronJobs.providers;
  31.     }
  32.   }
  33.  
  34.   /**
  35.    * Gets class name of the service provider.
  36.    *
  37.    * @param {Object} config Cron Jobs configuration object.
  38.    * @param {number} index Current position on config object iteration.
  39.    *
  40.    * @return string
  41.    */
  42.   private static getClassName(config: Object, index: number) : string {
  43.     return validator.isDefined(Object.keys(config)[index])
  44.       ? Object.keys(config)[index]
  45.       : '';
  46.   }
  47.  
  48.   /**
  49.    * Gets instance of the service provider.
  50.    *
  51.    * @param {string} className The name of the class.
  52.    *
  53.    * @return Object
  54.    */
  55.   private static getProviderInstance(className: string) : Object {
  56.     let instance: Object;
  57.  
  58.     if(validator.isDefined(Providers[className])
  59.       && validator.isInstance(new Providers[className], Providers[className])) {
  60.       instance = new Providers[className];
  61.     }
  62.  
  63.     return instance;
  64.   }
  65.  
  66.   /**
  67.    * Gets the jobs configuration by service provider.
  68.    *
  69.    * @param {Object} providerConfig Configuration to the current service provider.
  70.    * @param {string} className The class name to the service provider.
  71.    *
  72.    * @return Array
  73.    */
  74.   private static getJobsConfiguration(providerConfig: Object, className: string) : Array<Object> {
  75.     let jobsConfig = [];
  76.  
  77.     if (providerConfig.hasOwnProperty(className)
  78.       && validator.isDefined(providerConfig[className].jobs)) {
  79.       jobsConfig = providerConfig[className].jobs.filter(item => item.active);
  80.     }
  81.  
  82.     return jobsConfig;
  83.   }
  84.  
  85.   /**
  86.    * Creates the schedules of the jobs.
  87.    *
  88.    * @param {Object} jobConfiguration The Jobs configurations.
  89.    * @param {Function} task Method of the provider instance to execute by scheduler.
  90.    */
  91.   private static buildScheduleJob(jobConfiguration: Object, task: Function) : void {
  92.     if (jobConfiguration.hasOwnProperty('timezone')) {
  93.       rule.tz = jobConfiguration['timezone'];
  94.     }
  95.  
  96.     if (jobConfiguration.hasOwnProperty('time')) {
  97.       for(let prop in jobConfiguration['time']) {
  98.         if (jobConfiguration['time'].hasOwnProperty(prop)) {
  99.           rule[prop] = jobConfiguration['time'][prop]
  100.         }
  101.       }
  102.     }
  103.  
  104.     schedule.scheduleJob(rule, task);
  105.   }
  106.  
  107.   /**
  108.    * Creates the cron jobs.
  109.    *
  110.    * @param {Object} jobConfiguration The Jobs configurations.
  111.    * @param {Object} instance Instance to the service provider.
  112.    */
  113.   public static createCronJobs(jobConfiguration: Array<Object>, instance: Object) : void {
  114.     if (validator.isDefined(instance) && jobConfiguration.length > 0) {
  115.       jobConfiguration.forEach(jobConfig => {
  116.         if (jobConfig.hasOwnProperty('name')
  117.           && typeof instance[jobConfig['name']] === 'function') {
  118.           this.buildScheduleJob(jobConfig, instance[jobConfig['name']])
  119.         }
  120.       });
  121.     }
  122.   }
  123.  
  124.   /**
  125.    * Runs the cron manager.
  126.    */
  127.   public run() : void {
  128.     if (validator.isArray(this.config)) {
  129.       let className: string;
  130.       let providerInstance: Object;
  131.       let jobConfiguration: Array<Object>;
  132.  
  133.       this.config.forEach((providerConfig, index) => {
  134.         className = Job.getClassName(providerConfig, index);
  135.         providerInstance = Job.getProviderInstance(className);
  136.         jobConfiguration = Job.getJobsConfiguration(providerConfig, className);
  137.  
  138.         Job.createCronJobs(jobConfiguration, providerInstance);
  139.       });
  140.     }
  141.   }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement