Skip to content

Instantly share code, notes, and snippets.

@wrburgess
Created September 19, 2012 23:17
Show Gist options
  • Save wrburgess/3752973 to your computer and use it in GitHub Desktop.
Save wrburgess/3752973 to your computer and use it in GitHub Desktop.
Setting up Redis/Resque with Rails 3 on Local and Heroku

Setting Up Redis/Resque with Rails 3 on Local and Heroku

Refs

Add the gem

In your Gemfile

gem 'resque', :require => 'resque/server'

Run bundle

bundle install

Create a Connection Class

create lib/redis_connection.rb

class RedisConnection

  def self.close
    connection.quit
  end

  def self.connection
    @connection ||= new_connection
  end

  private

  def self.new_connection
    Redis.new(:host => Settings.redis.host,
              :port => Settings.redis.port,
              :password => Settings.redis.password,
              :thread_safe => true)
  end
end

Note: These settings are referenced from the settings.yml file via SettingsLogic gem

Set up Heroku Environment Variables

On the terminal

heroku config:set REDIS_HOST=ray.redistogo.com --remote stage
heroku config:set REDIS_PORT=9286 --remote stage
heroku config:set REDIS_PASSWORD=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx --remote stage

Set up Settings variables in settings.yml

staging:
<<: *defaults
  redis:
  host: <%= ENV["REDIS_HOST"] %>
  port: <%= ENV["REDIS_PORT"] %>
  password: <%= ENV["REDIS_PASSWORD"] %>

Autoload the lib directory

Add/edit this line in config/application.rb

config.autoload_paths += %W(#{config.root}/lib #{config.root}/lib/**/*)

Create a Rake Task for Resque

create a file of lib/tasks/resque.rake

require "resque/tasks"

task "resque:setup" => :environment do
  Resque.before_fork = Proc.new { ActiveRecord::Base.establish_connection }
end

Create an initializer file

create config/initializers/resque.rb

Resque.redis = RedisConnection.connection

Create an external system to host queue

create account at RedisToGo https://redistogo.com

get instance info at https://redistogo.com/instances

  • requirepass xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  • port 9286
  • host ray.redistogo.com

Create Procfiles for Local and Hosted Envs

create Procfile.local for testing, development on Local

redis: redis-server
worker: env QUEUE=* bundle exec rake environment resque:work

create Procfile for staging, production on Heroku

web: thin start -p $PORT
worker: env QUEUE=* bundle exec rake resque:work

Mount Resque on Rails 3 Routes

Add line to config/routes.rb

mount Resque::Server, :at => "/resque"

Turn on Heroku Worker

On the terminal

heroku ps:scale worker=1 --remote stage
@cisnky
Copy link

cisnky commented Feb 6, 2014

Best example I've seen online. Thanks!

@justin808
Copy link

Any differences for Rails 4?

@dmcwilliams
Copy link

Great tutorial! One suggestion to simplify, if you are not using the SettingsLogic gem and using Redis Cloud self.new_connection can be set up as:

host = URI.parse(ENV["REDISCLOUD_URL"]).try(:host)
port = URI.parse(ENV["REDISCLOUD_URL"]).try(:port)
password = URI.parse(ENV["REDISCLOUD_URL"]).try(:password)
Redis.new(:host => host, :port => port, :password => password)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment