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

Block Resque Queue from processing

Stops workers from processing jobs in these queues, so you can e.g. restart indexing/mail/… servers safely

Usage

REDIS.sadd 'blocked-resque-queues', 'low'

Code

class Resque::Worker
  def queues_with_blocked
    blocked = REDIS.smembers('blocked-resque-queues') || []
    queues_without_blocked - blocked
  end
  alias_method_chain :queues, :blocked
end