<commit_before>'use strict';

var async = require('async');

module.exports = {
  startPriority: 900,
  loadPriority:  699,
  initialize: function(api, next){

    api.tasks = {

      tasks: {},
      jobs: {},
      middleware: {},
      globalMiddleware: [],

      loadFile: function(fullFilePath, reload){
        var self = this;
        if(!reload){ reload = false; }

        var loadMessage = function(loadedTaskName){
          api.log(['task %sloaded: %s, %s', (reload ? '(re)' : ''), loadedTaskName, fullFilePath], 'debug');
        };

        api.watchFileAndAct(fullFilePath, function(){
          self.loadFile(fullFilePath, true);
        });

        var task;
        try{
          var collection = require(fullFilePath);
          for(var i in collection){
            task = collection[i];
            api.tasks.tasks[task.name] = task;
            self.validateTask(api.tasks.tasks[task.name]);
            api.tasks.jobs[task.name] = self.jobWrapper(task.name);
            loadMessage(task.name);
          }
        }catch(error){
          api.exceptionHandlers.loader(fullFilePath, error);
          delete api.tasks.tasks[task.name];
          delete api.tasks.jobs[task.name];
        }
      },

      jobWrapper: function(taskName){
        var self = this;
        var task = api.tasks.tasks[taskName];
        var middleware = task.middleware || [];
        var plugins = task.plugins || [];
        var pluginOptions = task.pluginOptions || [];
        if(task.frequency > 0){
          if(plugins.indexOf('jobLock') < 0){ plugins.push('jobLock'); }
          if(plugins.indexOf('queueLock') < 0){ plugins.push('queueLock'); }
          if(plugins.indexOf('delayQueueLock') < 0){ plugins.push('delayQueueLock'); }
        }
        //load middleware into plugins
        function processMiddleware(m){
          if(api.tasks.middleware[m]){ //Ignore middleware until it has been loaded.
            var plugin = function(worker, func, queue, job, args, options){
              var self = this;
              self.name = m;
              self.worker = worker;
              self.queue = queue;
              self.func = func;
              self.job = job;
              self.args = args;
              self.options = options;
              self.api = api;

              if(self.worker.queueObject){
                self.queueObject = self.worker.queueObject;
              }else{
                self.queueObject = self.worker;
              }
            };

            if(api.tasks.middleware[m].preProcessor){ plugin.prototype.before_perform = api.tasks.middleware[m].preProcessor; }
            if(api.tasks.middleware[m].postProcessor){ plugin.prototype.after_perform = api.tasks.middleware[m].postProcessor; }

            plugins.push(plugin);
          }<commit_msg>Adds hooks for before_enqueue and after_enqueue to task middleware.<commit_after>'use strict';

var async = require('async');

module.exports = {
  startPriority: 900,
  loadPriority:  699,
  initialize: function(api, next){

    api.tasks = {

      tasks: {},
      jobs: {},
      middleware: {},
      globalMiddleware: [],

      loadFile: function(fullFilePath, reload){
        var self = this;
        if(!reload){ reload = false; }

        var loadMessage = function(loadedTaskName){
          api.log(['task %sloaded: %s, %s', (reload ? '(re)' : ''), loadedTaskName, fullFilePath], 'debug');
        };

        api.watchFileAndAct(fullFilePath, function(){
          self.loadFile(fullFilePath, true);
        });

        var task;
        try{
          var collection = require(fullFilePath);
          for(var i in collection){
            task = collection[i];
            api.tasks.tasks[task.name] = task;
            self.validateTask(api.tasks.tasks[task.name]);
            api.tasks.jobs[task.name] = self.jobWrapper(task.name);
            loadMessage(task.name);
          }
        }catch(error){
          api.exceptionHandlers.loader(fullFilePath, error);
          delete api.tasks.tasks[task.name];
          delete api.tasks.jobs[task.name];
        }
      },

      jobWrapper: function(taskName){
        var self = this;
        var task = api.tasks.tasks[taskName];
        var middleware = task.middleware || [];
        var plugins = task.plugins || [];
        var pluginOptions = task.pluginOptions || [];
        if(task.frequency > 0){
          if(plugins.indexOf('jobLock') < 0){ plugins.push('jobLock'); }
          if(plugins.indexOf('queueLock') < 0){ plugins.push('queueLock'); }
          if(plugins.indexOf('delayQueueLock') < 0){ plugins.push('delayQueueLock'); }
        }
        //load middleware into plugins
        function processMiddleware(m){
          if(api.tasks.middleware[m]){ //Ignore middleware until it has been loaded.
            var plugin = function(worker, func, queue, job, args, options){
              var self = this;
              self.name = m;
              self.worker = worker;
              self.queue = queue;
              self.func = func;
              self.job = job;
              self.args = args;
              self.options = options;
              self.api = api;

              if(self.worker.queueObject){
                self.queueObject = self.worker.queueObject;
              }else{
                self.queueObject = self.worker;
              }
            };

            if(api.tasks.middleware[m].preProcessor){ plugin.prototype.before_perform = api.tasks.middleware[m].preProcessor; }
            if(api.tasks.middleware[m].postProcessor){ plugin.prototype.after_perform = api.tasks.middleware[m].postProcessor; }
            if(api.tasks.middleware[m].preEnqueue){ plugin.prototype.before_enqueue = api.tasks.middleware[m].preEnqueue; }
            if(api.tasks.middleware[m].postEnqueue){ plugin.prototype.after_enqueue = api.tasks.middleware[m].postEnqueue; }

            plugins.push(plugin);
          }
{'fraction_non_alphanumeric': 0.13219652203275367, 'fraction_numerical': 0.0033766672294445383, 'mean_word_length': 2.004056795131846, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5792424', 'n_tokens_mistral': 1659, 'n_tokens_neox': 1611, 'n_words': 314}