首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >redis:在heroku上使用redis-rails时的时间错误

redis:在heroku上使用redis-rails时的时间错误
EN

Stack Overflow用户
提问于 2013-06-03 21:20:13
回答 3查看 3.5K关注 0票数 2

更新后包括Redis/Resque版本和堆栈跟踪(如下):

代码语言:javascript
复制
redis (3.0.4)
redis-namespace (1.3.0)
  redis (~> 3.0.0)
redis-store (1.1.2)
  redis (>= 2.2.0)
resque (1.24.1)
  mono_logger (~> 1.0)
  multi_json (~> 1.0)
  redis-namespace (~> 1.2)
  sinatra (>= 0.9.2)
  vegas (~> 0.1.2)
resque-scheduler (2.0.1)
  redis (>= 2.0.1)
  resque (>= 1.20.0)
  rufus-scheduler

我在heroku上看到间歇性的Redis::TimeoutError: Connection timed out,同时使用Rails.cache.fetch命令将一个中等大小的数组(大约200个Fixnum)写入Redis商店。

我也在用Resque。

我看到这里::Client可以接收一个超时值选项,但我不知道应该将初始化选项传递给Redis。

我使用的是标准的heroku resque.rb

代码语言:javascript
复制
rails_root        = ENV['RAILS_ROOT'] || File.dirname(__FILE__) + '/../..'
rails_env         = ENV['RAILS_ENV'] || 'development'

resque_config     = YAML.load_file(rails_root + '/config/resque.yml')
ENV['REDIS_URI']  = resque_config[rails_env]
Resque.redis      = resque_config[rails_env]
Resque.inline     = rails_env == 'test'

require 'resque_scheduler'
require 'resque/scheduler'
require 'resque_scheduler/server'

Resque.schedule   = YAML.load_file(rails_root + '/config/resque-schedule.yml')

Resque.before_fork do
  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!
end

Resque.after_fork do
  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.establish_connection
end

我假设这里实例化了Redis客户端。这是一个不同于在production.rb中实例化的客户机吗?

代码语言:javascript
复制
rails_root          = ENV['RAILS_ROOT'] || File.dirname(__FILE__) + '/../..'
rails_env           = ENV['RAILS_ENV'] || 'development'
resque_config = YAML.load_file(rails_root + '/config/resque.yml')
config.cache_store = :redis_store, resque_config[rails_env], { expires_in: 14.days }

据我所知,Rails.cache选项的选项散列。这里实例化了一个新客户端吗?我怎么才能把选项传递给这个呢?

更新后将此实验包含在heroku控制台中,这意味着它们是不同的客户端实例:

代码语言:javascript
复制
irb(main):002:0> Rails.cache
=> #<ActiveSupport::Cache::RedisStore:0x00000003860e18 @data=#<Redis client v3.0.4 for redis://spinyfin.redistogo.com:9485/0>, @options={:expires_in=>14 days}>
irb(main):003:0> Resque.redis.redis
=> #<Redis client v3.0.4 for redis://spinyfin.redistogo.com:9485/0>
irb(main):004:0> Rails.cache.instance_variable_get(:@data).object_id == Resque.redis.redis.object_id
=> false

堆栈跟踪:

代码语言:javascript
复制
Redis::TimeoutError: Connection timed out
    from /app/vendor/bundle/ruby/1.9.1/gems/redis-3.0.4/lib/redis/client.rb:208:in `rescue in io'
    from /app/vendor/bundle/ruby/1.9.1/gems/redis-3.0.4/lib/redis/client.rb:206:in `io'
    from /app/vendor/bundle/ruby/1.9.1/gems/redis-3.0.4/lib/redis/client.rb:214:in `read'
    from /app/vendor/bundle/ruby/1.9.1/gems/redis-3.0.4/lib/redis/client.rb:84:in `block in call'
    from /app/vendor/bundle/ruby/1.9.1/gems/redis-3.0.4/lib/redis/client.rb:187:in `block (2 levels) in process'
    from /app/vendor/bundle/ruby/1.9.1/gems/redis-3.0.4/lib/redis/client.rb:295:in `ensure_connected'
    from /app/vendor/bundle/ruby/1.9.1/gems/redis-3.0.4/lib/redis/client.rb:177:in `block in process'
    from /app/vendor/bundle/ruby/1.9.1/gems/redis-3.0.4/lib/redis/client.rb:256:in `logging'
    from /app/vendor/bundle/ruby/1.9.1/gems/redis-3.0.4/lib/redis/client.rb:176:in `process'
    from /app/vendor/bundle/ruby/1.9.1/gems/redis-3.0.4/lib/redis/client.rb:84:in `call'
    from /app/vendor/bundle/ruby/1.9.1/gems/redis-3.0.4/lib/redis.rb:644:in `block in setex'
    from /app/vendor/bundle/ruby/1.9.1/gems/redis-3.0.4/lib/redis.rb:36:in `block in synchronize'
    from /app/vendor/ruby-1.9.3/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
    from /app/vendor/bundle/ruby/1.9.1/gems/redis-3.0.4/lib/redis.rb:36:in `synchronize'
    from /app/vendor/bundle/ruby/1.9.1/gems/redis-3.0.4/lib/redis.rb:643:in `setex'
    from /app/vendor/bundle/ruby/1.9.1/gems/redis-store-1.1.2/lib/redis/store/interface.rb:17:in `setex'
... 11 levels...
    from /app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/cache.rb:299:in `fetch'
    ...SNIP...
    ...my code...
    ...SNIP...
    from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/relation/delegation.rb:6:in `each'
    from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/relation/delegation.rb:6:in `each'
    ...SNIP...
    ...my code...
    ...SNIP...
    from (irb):5
    from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
    from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
    from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'irb(main):006:0>  !    Heroku client internal error.
 !    Search for help at: https://help.heroku.com
 !    Or report a bug at: https://github.com/heroku/heroku/issues/new

    Error:       Operation timed out (Errno::ETIMEDOUT)
    Backtrace:   /usr/local/heroku/ruby/lib/ruby/1.9.1/openssl/buffering.rb:121:in `sysread'
                 /usr/local/heroku/ruby/lib/ruby/1.9.1/openssl/buffering.rb:121:in `readpartial'
                 /Users/me/.heroku/client/lib/heroku/client/rendezvous.rb:69:in `block in start'
                 /Users/me/.heroku/client/lib/heroku/client/rendezvous.rb:53:in `loop'
                 /Users/me/.heroku/client/lib/heroku/client/rendezvous.rb:53:in `start'
                 /Users/me/.heroku/client/lib/heroku/command/run.rb:132:in `rendezvous_session'
                 /Users/me/.heroku/client/lib/heroku/command/run.rb:119:in `run_attached'
                 /Users/me/.heroku/client/lib/heroku/command/run.rb:24:in `index'
                 /Users/me/.heroku/client/lib/heroku/command.rb:206:in `run'
                 /Users/me/.heroku/client/lib/heroku/cli.rb:28:in `start'
                 /usr/local/heroku/bin/heroku:24:in `<main>'

    Command:     heroku run rails c
    Plugins:     heroku-redis-cli
    Version:     heroku-toolbelt/2.39.4 (x86_64-darwin10.8.0) ruby/1.9.3
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-06-21 20:13:27

这不是一个很好的答案,但在一位同事的推荐下,我从RedisToGo转到了openredis,这些问题马上就消失了。

票数 1
EN

Stack Overflow用户

发布于 2013-06-03 22:52:12

“连接超时”消息意味着redis-rb无法启动到您的Redis服务器的连接。通常,您只想在应用程序启动时初始化Redis连接一次。但是,由于Resque分叉(而不是使用Sidekiq之类的线程),它必须为每个作业初始化一个新的Redis连接。

通常,这不是一个问题,但是Heroku在创建新的Redis连接时有间歇性的问题。我在语言/客户端库/ Redis主机上看到了这个问题,除了减少您创建的Redis连接的数量之外,唯一的缓解问题的方法是在Resque.after_fork块中自动重试连接到Redis。(例如。捕获超时错误,如果您尝试了3次,可以重试或重新引发异常)

票数 1
EN

Stack Overflow用户

发布于 2013-07-04 01:31:38

您不能同时将URL和选项哈希传递给config.cache_store,因为这只虫子

代码语言:javascript
复制
config.cache_store = :redis_store, resque_config[rails_env], { expires_in: 14.days }

如果您传递一个URL和一个选项散列,它将忽略URL并默认为localhost,这可能就是您看到超时的原因(我刚刚遇到了同样的问题)。

相反,可以这样做:

代码语言:javascript
复制
redis_uri = URI.parse(ENV["REDISTOGO_URL"])
config.cache_store = :redis_store, {
  host: redis_uri.host,
  port: redis_uri.port,
  password: redis_uri.password,
  namespace: "cache",
  expires_in: 7.days
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16906025

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档