I've been trying to figure what the issue is for couple days now but no luck, I'm running Rails 3.1.3 on Heroku Cedar with Unicorn and using Resque 1.20 for background jobs.
Redis add-on as been added and REDISTOGO_URL set, I have resque.rb initializer as
require 'resque'
Resque.after_fork = Proc.new { ActiveRecord::Base.establish_connection }
uri = URI.parse(ENV["REDISTOGO_URL"])
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
Resque.redis = REDIS
I also tried
Resque.redis = Redis.connect(:url => ENV['REDISTOGO_URL'])
Also this, from the official RedisToGo site
ENV["REDISTOGO_URL"] ||= "redis://username:password@host:1234/"
uri = URI.parse(ENV["REDISTOGO_URL"])
Resque.redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password, :thread_safe => true)
Heroku worker as:
bundle exec rake resque:work QUEUE='*'
Everything works fine locally and in Heroku console; but when I try to queue from Rails
Resque.enqueue(EmailQueue, id)
I get
NoMethodError (undefined method `sadd' for nil:NilClass):
Please any help will be appreciated, thank you.
Figured it out, since I was on Unicorn; I had to set config/unicorn.rb as below to connect or Redis.
after_fork do |server, worker|
# Replace with MongoDB or whatever
if defined?(ActiveRecord::Base)
ActiveRecord::Base.establish_connection
Rails.logger.info('Connected to ActiveRecord')
end
# If you are using Redis but not Resque, change this
if defined?(Resque)
Resque.redis = ENV['REDISTOGO_URL']
Rails.logger.info('Connected to Redis')
end
end
Hopes this helps someone else.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.