0

データベース/redis/キューが正しく機能しているかどうかを示す「ヘルスチェック」ページを開発したいと考えています。ただし、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 に答える