RobB RobB - 11 months ago 53
MySQL Question

Rails: Display Maintenance Page if No Database Connection Available

I'm looking for a solution that will allow my rails app to render a user-friendly maintenance page when there is no Mysql server available to connect to.

Normally a

is thrown from the mysql connection adapter in active_record Something like:

/!\ FAILSAFE /!\ Wed May 26 11:40:14 -0700 2010
Status: 500 Internal Server Error
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock

Is there a low-overhead way to catch this error and render a maintenance page instead?

I'm assuming that since connections are actually made in the
mysql adapter the app never makes it to the controller stack before it throws the error, so you can't catch it in a controller.

Any input would be greatly appreciated.

Answer Source

You could create a view in whatever your root_path controller is:

map.root :controller => "foo", :action => "index"

Let's say you call this view "db_maintenance.html.erb". In your controller, do this:

def index
    @widgets = Widget.find(:all)
  rescue Exception => e
    # This will only happen if DB stuff fails
    redirect_to :action => "db_maintenance", :error => e.message


def db_maintenance
  @error = params[:error] # You might want to do something with this here or in the view
  # renders the app/views/foo/db_maintenance.html.erb view

In your view, you could put something like:

<h1>Sorry for the inconvenience</h1>
blah blah blah. This happened because of:
<pre><code><%= @error %></code></pre>

This, ofcourse, only helps if the user hits your site's main page, but you could easily extrapolate from there. You could add the "def db_maintenance" action to the application controller and manually specify what view it should render too. It's not perfect, but it should get the job done.