Question

With the launch of Amazon's Relational Database Service today and their 'enforced' maintenance windows I wondered if anyone has any solutions for handling a missing database connection in Rails.

Ideally I'd like to be able to automatically present a maintenance page to visitors if the database connection disappears (i.e. Amazon are doing their maintenance) - has anyone ever done anything like this?

Cheers Arfon

Was it helpful?

Solution

You can do this with a Rack Middleware:

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

Obviously you can customize the error message, and the e.message =~ /Can't connect to/ bit may just be paranoia, almost all other SQL errors should be caught inside ActionController::Dispatcher.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top