Heroku上通过Ruby守护进程工作的Redis连接被拒绝。

3

一个运行在'web' Heroku进程上的Rails 3.2.6应用正在使用ENV["REDISTOGO_URL"]环境变量连接到Redis:

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

一种名为Ruby守护程序的“流式”进程作为次要工作者运行:
----- 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

然而,当流处理调用与Redis连接的主要Rails应用程序中的方法时,流处理过程会崩溃 - 即使流处理过程应该加载与“Web” Rails应用程序进程相同的redis.rb初始化程序。

----- /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个回答

1

在/initializer/redis.rb中从“Redis.current”切换到使用“$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在单独的线程上运行这些进程。 gem repo上的文档建议将gem更新到>= 3版本,以使Redis.current能够在多线程环境中运行:

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接