'use strict';
var Agenda = require('agenda');
var config = require('config');
var _ = require('lodash');
var ApplicationEvent = require('broker/lib/event_types/application_event');
var EventBroker = require('broker/lib/event_broker');
var BBPromise = require('bluebird');
var logger = require('hoist-logger');

function Runner() {
  _.bindAll(this);
}

Runner.prototype = {
  processEvents: function (job, done) {
    logger.info({
      job: job
    }, 'processing schedule job');
    var data = job.attrs.data;
    BBPromise.try(function () {
        return BBPromise.all(_.map(data.events, function (eventName) {
          return this.createEvent(data, eventName).catch(
            /* istanbul ignore next */
            function (err) {
              logger.info(err);
              logger.error(err);
              throw err;
            });
        }, this)).catch(
          /* istanbul ignore next */
          function (err) {
            logger.info(err);
            logger.error(err);
            throw err;
          });
      },[],this).catch(
        /* istanbul ignore next */
        function (err) {
          logger.info(err);
          logger.error(err);
          throw err;
        })
      .nodeify(done);
  },
  createEvent: function (data, eventName) {
    var applicationEvent = new ApplicationEvent({
      applicationId: data.application,
      environment: data.environment,
      eventName: eventName
    });
    logger.info({
      eventId: applicationEvent.messageId,
      applicationId: applicationEvent.applicationId,
      correlationId: applicationEvent.correlationId,
      eventName: eventName
    }, 'raising scheduled event');
    return EventBroker.publish(applicationEvent);
  },
  start: function () {

    this.agenda = new Agenda({
      db: {
        address: config.get('Hoist.mongo.db')
      }
    });
    this.agenda.define('create:event', this.processEvents);
    this.agenda.start();
    logger.info('waiting on schedule');
  },
  stop: function (cb) {
    this.agenda.stop(cb || /* istanbul ignore next */ function () {});
  }
};


module.exports = new Runner();

@@ -663,207 +663,47 @@
 ame)
-.catch(%0A            /* istanbul ignore next */%0A            function (err) %7B%0A              logger.info(err);%0A              logger.error(err);%0A              throw err;%0A            %7D);%0A        %7D
+;%0A        %7D, this));%0A      %7D, %5B%5D
 , this)
-)
 .cat
@@ -710,26 +710,24 @@
 ch(%0A        
- 
- 
 /* istanbul 
@@ -745,26 +745,24 @@
  */%0A        
-  
 function (er
@@ -758,34 +758,32 @@
 unction (err) %7B%0A
-  
           logger
@@ -796,34 +796,32 @@
 err);%0A          
-  
 logger.error(err
@@ -837,173 +837,142 @@
     
-  throw err;%0A          %7D);%0A      %7D,%5B%5D,this).catch(%0A        /* istanbul ignore next */%0A        function (err) %7B%0A          logger.info(err);%0A          logger.error(err
+var application;%0A          if (data) %7B%0A            application = data.application;%0A          %7D%0A          logger.alert(err, application
 );%0A 
@@ -1541,17 +1541,16 @@
 on () %7B%0A
-%0A
     this

add raygun alert