Resque, Resque Server, on RedisToGo with Heroku
Asked Answered
S

1

8

I've been trying to get Resque (with Resque server) & RedisToGo working on heroku (cedar) for awhile now, but I keep running into this error:

Redis::CannotConnectError (Error connecting to Redis on 127.0.0.1:6379 (ECONNREFUSED)):

Its working locally, and I can access redis just fine in Heroku's console for my app.

My Procfile has:

web: bundle exec thin start -p $PORT -e $RACK_ENV
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
resque: env TERM_CHILD=1 RESQUE_TERM_TIMEOUT=10 bundle exec rake resque:work

My Gemfile has:

gem 'redis'

#Background queue
gem 'resque', '~> 1.22.0', :require => "resque/server"

lib/tasks/resque.rake:

require 'resque/tasks'

task "resque:setup" => :environment do
  ENV['QUEUE'] = '*'
end

desc "Alias for resque:work (To run workers on Heroku)"
task "jobs:work" => "resque:work"

routes.rb:

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

initializers: redis.rb:

uri = URI.parse(ENV["REDISTOGO_URL"])
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
Resque.redis = REDIS

resque.rb:

Dir["#{Rails.root}/app/workers/*.rb"].each { |file| require file }
Resque.after_fork = Proc.new { ActiveRecord::Base.establish_connection }

then in my app/workers directory I have something like myjob.rb

I feel like I'm going in circles here, any ideas?

Schilit answered 4/9, 2012 at 3:38 Comment(2)
Can you confirm that you installed the RedisToGo addon. Check the output of heroku config and ensure you have an entry for REDISTOGO_URLOcieock
Did you figure this out? I'm having the same issue.Frog
M
9

I think your Procfile has a typo. Why do you have two web processes? I'd stick with one and use unicorn.

web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb

When using unicorn with resque, you have to define the resque redis connection each time unicorn forks. Here are the relevant files.

config/initializers/redis.rb

uri = URI.parse(ENV["REDIS_WORKER"])
REDIS_WORKER = Redis.new(host: uri.host, port: uri.port, password: uri.password)

config/initializers/resque.rb

Resque.redis = REDIS_WORKER

config/unicorn.rb

before_fork do |server, worker|
  if defined?(Resque)
    Resque.redis.quit
    Rails.logger.info("Disconnected from Redis")
  end
end

after_fork do |server, worker|
  if defined?(Resque)
    Resque.redis = REDIS_WORKER
    Rails.logger.info("Connected to Redis")
  end
end

See this gist for the complete unicorn.rb

Mousseline answered 31/1, 2013 at 22:5 Comment(2)
In this scenario, there's one web and one worker dyno? And this config tracks the IP address of the worker dyno? Will this account for all the communication that needs to happen without another service in between the two?Freaky
@Freaky One web and one worker dyno. REDIS_WORKER is the worker dyno Redis connection. You don't need another service.Mousseline

© 2022 - 2024 — McMap. All rights reserved.