views:

144

answers:

2

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 Mysql::Error 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 active_record 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.

A: 

I think this is about your front-end configuration. For example, if you have Apache in front of some mongrels, you can configure Apache through ErrorDocument instructions to show a suitable file in case of error.

What is your front-end?

Stephan

Stephan Wehner
Stephan, thanks for your reply. In this case I'm using Nginx > Mongrel Cluster.
RobB
A: 

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
  begin
    @widgets = Widget.find(:all)
  rescue Exception => e
    # This will only happen if DB stuff fails
    redirect_to :action => "db_maintenance", :error => e.message
  end
end

...

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
end

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.

jgnagy