Automatically handle missing database connection in ActiveRecord?

With the launch of Amazon Relational Database Service today and their β€œforced” maintenance windows, I wondered if anyone had any solutions to handle the missing database connection in Rails.

Ideally, I would like to be able to automatically show the service page to visitors if the connection to the database disappears (i.e. Amazon is doing its maintenance). Has anyone ever done anything like this?

Arfon Greetings

+3
source share
1 answer

Rack:

class RescueFromNoDB < Struct.new(:app)
  def call(env)
    app.call(env)
  rescue Mysql::Error => e
    if e.message =~ /Can't connect to/
      [500, {"Content-Type" => "text/plain"}, ["Can't get to the DB server right now."]]
    else
      raise
    end
  end
end

, , e.message =~ /Can't connect to/ , SQL ActionController::Dispatcher.

+4

Source: https://habr.com/ru/post/1721237/


All Articles