 _          = require 'lodash'
-async       = require 'async'
+async      = require 'async'
+debug      = require('debug')('interval-service')
 cronParser = require 'cron-parser'
-debug      = require('debug')('interval-service')
 
 class KueWorker
   constructor: (dependencies={})->
+    @INTERVAL_TTL       = process.env.INTERVAL_TTL ? 10000
+    @INTERVAL_JOBS      = process.env.INTERVAL_JOBS ? 1000
+    @INTERVAL_ATTEMPTS  = process.env.INTERVAL_ATTEMPTS ? 999
     @INTERVAL_PROMOTION = process.env.INTERVAL_PROMOTION ? 100
-    @INTERVAL_JOBS      = process.env.INTERVAL_JOBS ? 1000
 
     @kue = dependencies.kue ? require 'kue'
     IORedis = dependencies.IORedis ? require 'ioredis'

        debug 'creating a new job!'
        @meshbluMessage.message [job.data.targetId], timestamp: Date.now()

        try
          intervalTime = @calculateNextCronInterval cronString, jobStartTime if cronString?
        catch error
          console.error error
          done()

        @setCronInterval intervalTime, cronString
        done()

  getTargetJobs: (job, callback=->) =>
    @redis.srem "interval/job/#{job.data.targetId}", job.id

    parser = cronParser.parseExpression cronString, currentDate: currentDate

    while timeDiff <= 0
      nextDate = parser.next()
      nextDate.setMilliseconds 0
      timeDiff = nextDate - currentDate
      debug 'this is the next time', timeDiff, nextDate.getTime()

    return timeDiff

  createJob: (job, intervalTime)=>
    return queue.create('interval', job.data).
      delay(intervalTime).
      removeOnComplete(true).
      attempts(@INTERVAL_ATTEMPTS).
      ttl(@INTERVAL_TTL)

module.exports = KueWorker
