Resque me with a custom Redis host

If you’re working outside of Ruby on Rails and want to have Redis hosted non-locally than this is the command you’re looking for:

Resque.redis = '<non_localhost_address>:<port>'

To paint the full picture: You’ve got Redis running on Machine1.Hostname and your Resque workers on Machine2.Hostname and you need those two wired up. In your Rake file for Resque workers you should add the line of code from above.

One little caveat, if you’re told Resque is an uninitialized constant than you just simply need to get Resque into your environment. Here’s an example IRB session where I’m using Bundler/Gemfile:

ree-1.8.7-2011.03 :001 > require 'rubygems'
 => false 
ree-1.8.7-2011.03 :002 > require 'bundler/setup'
 => true 
ree-1.8.7-2011.03 :003 > Bundler.require
=> [<Bundler::Dependency type=:runtime name="SystemTimer" requirements=">= 0">, <Bundler::Dependency type=:runtime name="rake" requirements=">= 0">, <Bundler::Dependency type=:runtime name="redis" requirements=">= 0">, <Bundler::Dependency type=:runtime name="redis-namespace" requirements=">= 0">, <Bundler::Dependency type=:runtime name="yajl-ruby" requirements=">= 0">, <Bundler::Dependency type=:runtime name="resque" requirements=">= 0">, <Bundler::Dependency type=:runtime name="ruby-prof" requirements=">= 0">, <Bundler::Dependency type=:runtime name="json" requirements=">= 0">, <Bundler::Dependency type=:runtime name="nokogiri" requirements=">= 0">, <Bundler::Dependency type=:runtime name="fastercsv" requirements=">= 0">, <Bundler::Dependency type=:runtime name="yieldmanager" requirements=">= 0">]

Cool, now we’ve got our environment and can do what we want. It’s not necessary to use a Gemfile, you can manually install your gems and then require them in your Ruby file. But since I have multiple files doing many different things it makes sense to have one common section for inclusion of my packages. I also encourage creating a ruby include file with your Redis setups; this will prevent you from having that Resque.redis scattered in all of your files that access Redis.

Hope this saves someone a minute. More information is in Defunkt’s Resque package on Github.

Leave a Reply

Your email address will not be published. Required fields are marked *