Resque test helpers, e.g. process all jobs

When testing resque jobs, the simples solution is to set Resque.inline = true, which just executes the jobs imediatly, but if you want to make sure that they e.g. have been scheduled for the right time or simulate non-parallel execution, you might find these useful.

ResqueScheduler
module ResqueScheduler
  def all_scheduled_jobs_count
    total_jobs = Hash.new(0)
    Array(redis.zrange(:delayed_queue_schedule, 0, -1)).each do |timestamp|
      total_jobs[timestamp.to_i] += redis.llen("delayed:#{timestamp}").to_i
    end
    total_jobs
  end
end

Resque
module Resque
  def self.perform_enqueue_and_scheduled(queue)
    while timestamp = Resque.next_delayed_timestamp
      Resque::Scheduler.enqueue_delayed_items_for_timestamp(timestamp)
    end
    Resque.perform_all(queue)
  end

  def self.perform_all(queue_name)
    until size(queue_name) == 0
      reserve(queue_name).perform
    end
  end
end

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s