我正在尝试让我的 Rails 应用程序Resque
用于管理工人。但是,我想继续使用 ConnectionPool gem。
我在初始化程序中有这个:
puts ENV["REDISTOGO_URL"]
uri = (not ENV["REDISTOGO_URL"].nil?) ? URI.parse(ENV["REDISTOGO_URL"]) : nil
# at this point, debugger confirms $redis is nil
$redis = ConnectionPool::Wrapper.new(:size => 5, :timeout => 3) {
if uri.nil?
Redis.connect
else
Redis.connect(:host => uri.host, :port => uri.port, :password => uri.password)
end
}
$redis # just put this in here for the debugger
# At this point, $redis is #<Redis:0x007fb1b0036bf0>
# when it should be an instance of ConnectionPool::Wrapper
有谁知道为什么$redis
不作为实例返回ConnectionPool::Wrapper
?
我搜索了所有 gems 源代码,它没有设置$redis
. 在 ConnectionPool 的源代码中,我没有找到任何会返回实例Redis
而不是自身的东西。
这只发生在我从 DelayedJob 切换到 Resque 时。所以,这似乎是问题所在。然而,我不知所措。
我正在使用独角兽。这是config
.
worker_processes 2
timeout 30
preload_app true
before_fork do |server, worker|
# Replace with MongoDB or whatever
if defined?(ActiveRecord::Base)
ActiveRecord::Base.connection.disconnect!
Rails.logger.info('Disconnected from ActiveRecord')
end
# If you are using Redis but not Resque, change this
if defined?(Resque)
Resque.redis.quit
Rails.logger.info('Disconnected from Redis')
end
sleep 1
end
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)
# Yes, commented the Resque out for debugging, still get the same problem.
#Resque.redis = ENV['REDISTOGO_URL']
Rails.logger.info('Connected to Redis')
end
end
最后,Procfile:
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
worker: env TERM_CHILD=1 QUEUE=* bundle exec rake resque:work
我foreman
在我的开发环境中使用。
任何帮助是极大的赞赏。