var Promise = require('bluebird');

var Timer = require('node-distributed-timer');

var log4js = require('log4js');
var logger = log4js.getLogger('api/services/JobService');

var timer = new Timer({
  connection: {
    host: sails.config.globals.redisHost
  },
  tasks: {
    expire: function(id) {
      Job.findOne({
        id: id
      })
      .populate('notifications')
      .then(job => {
        logger.info('Processing ' + job.id + ' (' + job.name + ')');
        job.expired = true;
        return Promise.all([job.save(), ExportService.process(job, job.notifications)]);
      });
    }
  }
});

module.exports = {
  process: function(key) {
    return Job.findOne({ key: key }).populate('notifications').then((job) => {
      logger.debug('Resetting ' + job.id + ' (' + job.name + ')');
      this.clear(job.id);
      this.kickoff(job);
      job.lastActive = new Date();
      var promises = [];
      if (job.expired) {
        job.expired = false;
        promises.push(ExportService.process(job, job.notifications));
      }
      promises.push(job.save());
      return Promise.all(promises).then(() => {
        return 200;
      });
    });
  },

  kickoff: function(job) {
    logger.debug('Kicking off job ' + job.id + ' (' + job.name + ')');
    timer.clear('expire', job.id);
    timer.schedule('expire', job.id, job.timeout * 1000);
  },

  clear: function(id) {
    logger.debug('Clearing job ' + id);
    timer.clear('expire', id);
  }
};

@@ -515,16 +515,115 @@
 se.all(%5B
+%0A          Event.create(%7B%0A            job: job,%0A            user: job.user%0A          %7D),%0A          
 job.save
@@ -625,16 +625,26 @@
 .save(),
+%0A         
  ExportS
@@ -681,16 +681,25 @@
 cations)
+%0A        
 %5D);%0A    

Create Event when a timer expires