2012-07-10 48 views
3

一個Rails 3.2.6應用程序中運行爲 '網絡' 的Heroku過程連接使用ENV到Redis的[ 「REDISTOGO_URL」]環境變量:Redis的連接通過紅寶石守護程序工人拒絕在Heroku

irb(main):002:0> Redis.current 
=> #<Redis client v2.2.2 connected to redis://xxx.redistogo.com:1234/0 (Redis v2.4.11)> 

- --- /initializers/redis.rb

if Rails.env.development? 
    Redis.current = Redis.new 
elsif Rails.env.test? 
    Redis.current = Redis.new 
elsif Rails.env.production? 
    uri = URI.parse(ENV["REDISTOGO_URL"]) 
    Redis.current = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password) 
end 

紅寶石守護程序 '流式' 進程運行作爲輔助工人:

----- Procfile

web: bundle exec rails server thin -p $PORT -e $RACK_ENV 
worker: bundle exec rake jobs:work 
streaming: RAILS_ENV=production ruby bin/streaming.rb start 

然而,流式傳輸過程中崩潰時,它調用主Rails應用程序,與Redis的連接方法 - 即使流過程應該被加載相同redis.rb初始化的「網絡」的Rails應用程序。

----- /bin/streaming_ctl.rb

# encoding: UTF-8 

require "rubygems" 
require "bundler/setup" 
require "daemons" 

Daemons.run(File.expand_path("../streaming.rb", __FILE__)) 

----- /bin/streaming.rb

# encoding: UTF-8 

# TODO: set rails env in init script 
ENV["RAILS_ENV"] ||= "production" 

# load rails environment 
require File.expand_path('../../config/environment', __FILE__) 

logger = ActiveSupport::BufferedLogger.new(File.expand_path("./../../log/streaming.log", __FILE__)) 

Streaming.start 
logger.info("\nStarting streaming in #{Rails.env.to_s} mode.") 

爲什麼使用默認流處理/工人Redis主機和端口?

[streaming.1]: /app/vendor/bundle/ruby/1.9.1/gems/redis-2.2.2/lib/redis/client.rb:236:in `rescue in establish_connection': Connection refused - Unable to connect to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED) 

回答

1

從「Redis.current」在/initializer/redis.rb切換和使用「$ redis的」可變慣例代替(如這裏http://jimneath.org/2011/03/24/using-redis-with-ruby-on-rails.html示出)固定在連接問題。

該應用當時使用的是redis gem 2.2.2版本。看起來好像這個gem版本中的Redis.current在'web'和'worker'進程中不一致,因爲Heroku在不同的線程上運行這些進程。在寶石回購的文檔建議更新到寶石> = 3版將使Redis.current到在多線程環境中運行:

https://github.com/redis/redis-rb/blob/master/CHANGELOG.md#300