超时::错误:在Rails中使用Redis时执行已过期

时间:2011-01-30 09:00:47

标签: ruby-on-rails ruby nosql redis

使用虚荣时,我经常会收到超时错误(Timeout::Error: execution expired),并且不断崩溃我的网站。这是发生的事情:

[GEM_ROOT]/gems/redis-2.1.1/lib/redis/connection.rb:19:in `connect'
[GEM_ROOT]/gems/redis-2.1.1/lib/redis/client.rb:134:in `block in connect_to'
[GEM_ROOT]/gems/redis-2.1.1/lib/redis/client.rb:197:in `with_timeout'
[GEM_ROOT]/gems/redis-2.1.1/lib/redis/client.rb:133:in `connect_to'
[GEM_ROOT]/gems/redis-2.1.1/lib/redis/client.rb:18:in `connect'
[GEM_ROOT]/gems/redis-2.1.1/lib/redis/client.rb:78:in `reconnect'
[GEM_ROOT]/gems/redis-2.1.1/lib/redis/client.rb:156:in `rescue in ensure_connected'
[GEM_ROOT]/gems/redis-2.1.1/lib/redis/client.rb:153:in `ensure_connected'
[GEM_ROOT]/gems/redis-2.1.1/lib/redis/client.rb:180:in `block in ensure_connected'
/home/avishai/.rvm/rubies/ruby-1.9.1-p378/lib/ruby/1.9.1/monitor.rb:190:in `mon_synchronize'
[GEM_ROOT]/gems/redis-2.1.1/lib/redis/client.rb:176:in `synchronize'
[GEM_ROOT]/gems/redis-2.1.1/lib/redis/client.rb:180:in `ensure_connected'
[GEM_ROOT]/gems/redis-2.1.1/lib/redis/client.rb:58:in `block in process'
[GEM_ROOT]/gems/redis-2.1.1/lib/redis/client.rb:118:in `logging'
[GEM_ROOT]/gems/redis-2.1.1/lib/redis/client.rb:57:in `process'
[GEM_ROOT]/gems/redis-2.1.1/lib/redis/client.rb:29:in `call'
[GEM_ROOT]/gems/redis-2.1.1/lib/redis.rb:510:in `incrby'
[GEM_ROOT]/gems/redis-namespace-0.10.0/lib/redis/namespace.rb:190:in `method_missing'
[GEM_ROOT]/gems/vanity-1.5.0/lib/vanity/adapters/redis_adapter.rb:65:in `block in metric_track'
[GEM_ROOT]/gems/vanity-1.5.0/lib/vanity/adapters/redis_adapter.rb:64:in `each'
[GEM_ROOT]/gems/vanity-1.5.0/lib/vanity/adapters/redis_adapter.rb:64:in `each_with_index'
[GEM_ROOT]/gems/vanity-1.5.0/lib/vanity/adapters/redis_adapter.rb:64:in `metric_track'
[GEM_ROOT]/gems/vanity-1.5.0/lib/vanity/metric/base.rb:140:in `track!'
[GEM_ROOT]/gems/vanity-1.5.0/lib/vanity/playground.rb:163:in `track!'
[GEM_ROOT]/gems/vanity-1.5.0/lib/vanity/helpers.rb:52:in `track!'
app/controllers/downloads_controller.rb:80:in `go'

在我的控制器中,我有:

  # GET /downloads/1/go
  def go
    @download = Download.find(params[:id])

    # Redirect & track the outbound click
    cookies[:subid] = { :value => "#{@download.to_param}", :expires => 30.days.from_now, :domain => ".example.com", :path => "/" }
    cookies[:download_type] = { :value => "#{@download.download_type}", :expires => 30.days.from_now, :domain => ".example.com", :path => "/" }    
    redirect_to @download.destination.strip, :status => :see_other
    track! :download_start
  end

我在Passenger上使用Ruby 1.9,运行Vanity。有谁知道如何解决这个问题?

谢谢!

更新有人建议将其添加到environment.rb以处理Passenger中的分叉,但仍然无法解决问题......

# monkey patch vanity to force new redis connection when Passenger forks
if defined?(PhusionPassenger) 

  class Vanity::Playground 
    def reconnect_redis 
      @redis = nil
      # Need to remove method or unable to force a reconnect because it
      # hits the method, not the variable.
      class << self ; self ; end.send(:remove_method, :redis)
      redis # Make a new connectiont to redis
    end 
  end

  PhusionPassenger.on_event(:starting_worker_process) do |forked| 
    if forked 
      # We’re in smart spawning mode.
      begin
        Vanity.playground.reconnect_redis 
      rescue Exception => e
        RAILS_DEFAULT_LOGGER.error "Error connecting to redis: #{e.to_s}" 
      end
    end 
  end 
end

0 个答案:

没有答案
相关问题