问题
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?
回答1:
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
来源:https://stackoverflow.com/questions/12256476/resque-resque-server-on-redistogo-with-heroku