0

我想开发一个“健康检查”页面来显示数据库/redis/queue 是否正常工作。但是,如果 mysql 关闭,rails 似乎会以 500 错误导致请求失败。

Mysql2::Error (Can't connect to MySQL server on '127.0.0.1' (61)):
  mysql2 (0.3.11) lib/mysql2/client.rb:44:in `connect'
  mysql2 (0.3.11) lib/mysql2/client.rb:44:in `initialize'
  activerecord (3.2.8) lib/active_record/connection_adapters/mysql2_adapter.rb:16:in `new'
  activerecord (3.2.8) lib/active_record/connection_adapters/mysql2_adapter.rb:16:in `mysql2_connection'
  activerecord (3.2.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:309:in `new_connection'
  activerecord (3.2.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:319:in `checkout_new_connection'
  activerecord (3.2.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:241:in `block (2 levels) in checkout'
  activerecord (3.2.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:236:in `loop'
  activerecord (3.2.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:236:in `block in checkout'
  /Users/moshebergman/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
  activerecord (3.2.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:233:in `checkout'
  activerecord (3.2.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:96:in `block in connection'
  /Users/moshebergman/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
  activerecord (3.2.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:95:in `connection'
  activerecord (3.2.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:404:in `retrieve_connection'
  activerecord (3.2.8) lib/active_record/connection_adapters/abstract/connection_specification.rb:170:in `retrieve_connection'
  activerecord (3.2.8) lib/active_record/connection_adapters/abstract/connection_specification.rb:144:in `connection'
  activerecord (3.2.8) lib/active_record/query_cache.rb:67:in `rescue in call'
  activerecord (3.2.8) lib/active_record/query_cache.rb:61:in `call'
  activerecord (3.2.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:473:in `call'
  actionpack (3.2.8) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
  activesupport (3.2.8) lib/active_support/callbacks.rb:405:in `_run__4605244939803304128__call__1430909676671874052__callbacks'
  activesupport (3.2.8) lib/active_support/callbacks.rb:405:in `__run_callback'
  activesupport (3.2.8) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
  activesupport (3.2.8) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (3.2.8) lib/action_dispatch/middleware/callbacks.rb:27:in `call'

在这种情况下,有没有办法让铁轨不会完全失效?

4

1 回答 1

0

我通过猴子为某些 url 修补 QueryCache 找到了答案:

module ActiveRecord
  # = Active Record Query Cache
  class QueryCache
    def call(env)
      unless env['REQUEST_PATH'].starts_with?('/health_check')
        begin
          old = ActiveRecord::Base.connection.query_cache_enabled
          ActiveRecord::Base.connection.enable_query_cache!

          status, headers, body = @app.call(env)
          [status, headers, BodyProxy.new(old, body, ActiveRecord::Base.connection_id)]
        rescue Exception => e
          ActiveRecord::Base.connection.clear_query_cache
          unless old
            ActiveRecord::Base.connection.disable_query_cache!
          end
          raise e
        end
      else
        status, headers, body = @app.call(env)
      end
    end
  end
end

如果有人有更好的答案,请告诉我。

于 2012-10-26T22:49:36.257 回答