Resque is not picking up Redis configuration settings

Michael Graff picture Michael Graff · Jun 6, 2012 · Viewed 11.1k times · Source

I am having unexpected and significant problems trying to get a Rails app, running under Unicorn, to connect to a password-protected Redis server.

Using bundle exec rails c production on the command line, I can issue commands through Resque.redis. However, it seems that my configuration is being lost when it's forked under Unicorn.

Using a non-password-protected Redis server Just Works. However, I intend to run workers on other servers than where the Redis server lives, so I need this to be password protected.

I have also had success in using a password protected (using the same technique) but using Passenger rather than Unicorn.

I have the following setup:

# config/resque.yml

development: localhost:6379
test: localhost:6379
production: redis://user:[email protected]:6379

.

# config/initializers/redis.rb

rails_root = ENV['RAILS_ROOT'] || File.dirname(__FILE__) + '/../..'
rails_env = ENV['RAILS_ENV'] || 'development'

$resque_config = YAML.load_file(rails_root + '/config/resque.yml')
uri = URI.parse($resque_config[rails_env])
Resque.redis = Redis.new(host: uri.host, port: uri.port, password: uri.password)

.

# unicorn.rb bootup file

preload_app true

before_fork do |server, worker|
  Redis.current.quit
end

after_fork do |server, worker|
  Redis.current.quit
end

.

Answer

reillyse picture reillyse · Sep 2, 2012

Ok, for the sake of other people who might be googling this problem, I've solved this for myself at least

Basic problem is calling Redis.new other places in the code ,e.g. in your geocoder setup or unicorn config file.

just make sure that every time you call initialize Redis you pass in the appropriate values e.g. something like

REDIS = Redis.connect(:url =>  ENV['REDISTOGO_URL'])

everywhere and you should never have

Redis.new 

as it will default to localhost and the default port