require 'resque'
require 'resque-lonely_job'

module JamRuby
  class BatchEmailJob
    extend Resque::Plugins::JamLonelyJob

    @@log = Logging.logger[BatchEmailJob]

    @queue = :batch_emails

    def self.perform(batch_id)
      if ebatch = EmailBatch.find_by_id(batch_id)
        ebatch.deliver_batch
      end
    end

    def self.enqueue(batch_id)
      begin
        Resque.enqueue(self, batch_id)
        true
      rescue
        # implies redis is down.  but since there is no retry logic with this, we should at least log a warn in case we've configured something wrong
        @@log.warn("unable to enqueue")
        false
      end
    end

  end
end
