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.
You could create a view in whatever your root_path controller is:
Let’s say you call this view “db_maintenance.html.erb”. In your controller, do this:
In your view, you could put something like:
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.