The Blog

Oct 27
2

Resque and Resque Unit in Rails 3

by David Czarnecki

Categories

Do you need to create background jobs, place those jobs on multiple queues, and process them later? And do you want to test your queue appropriately? Are you using Rails 3?

If your answer to those questions is yes, then you’re probably already looking at and using Resque and Resque Unit. I really just want to cover the nice part about using these libraries in Rails 3. Let’s cover the basic setup.

Gemfile

# Redis/resque

gem 'resque', '1.8.2'
gem 'redis', '2.0.12'
gem 'redis-namespace', '0.10.0'

group :test do
gem 'factory_girl_rails', '1.0'
gem 'mocha', '0.9.8'
gem 'resque_unit', '0.2.6'
end

config/initializers/resque.rb

require 'resque'

redis_config = YAML.load_file("#{Rails.root}/config/redis.yml")
Resque.redis = redis_config[Rails.env]
Resque.redis.namespace = "resque:company:namespace"

config/redis.yml

development: localhost:6379
test: localhost:6379
staging: localhost:6379
production: localhost:6379

Here’s the kicker. As we’re defining resque_unit in the “test” group in our Gemfile, it’s going to “provide a mock Resque for testing Rails code that depends on Resque”. Now we can make assertions in our tests about data
in Resque without having to have Resque running. Pretty simple right?

P.S. The answer to that last question is “yes”.

P.P.S. My code in the Gemfile does have indentation, the code formatter on our blog sucks.

  • Reddit
  • Digg
  • del.icio.us
  • Facebook
  • Tumblr
  • Twitter

Comments

  1. I believe that line #4 in config/initializers/resque.rb needs to say the following:

    Resque.redis = Redis.new(redis_config[Rails.env])

  2. If you look at the redis.rb initializer, https://github.com/defunkt/resque/blob/master/lib/resque.rb, it accepts multiple formats for creating a new instance. Both would work.

Leave a Reply

*