[英]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. 我正在寻找一种解决方案,当没有可用的Mysql服务器连接时,它将允许我的rails应用程序呈现用户友好的维护页面。
Normally a Mysql::Error
is thrown from the mysql connection adapter in active_record Something like: 通常,从active_record中的mysql连接适配器抛出
Mysql::Error
类的东西:
/!\ 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. 我假设由于连接实际上是在
active_record
mysql适配器中建立的,因此应用程序在引发错误之前永远不会将其连接到控制器堆栈,因此您无法将其捕获到控制器中。
Any input would be greatly appreciated. 任何投入将不胜感激。
You could create a view in whatever your root_path controller is: 您可以在任何root_path控制器中创建一个视图:
map.root :controller => "foo", :action => "index"
Let's say you call this view "db_maintenance.html.erb". 假设您将此视图称为“ 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.
您可以将“ def db_maintenance”操作添加到应用程序控制器,并手动指定它也应呈现的视图。 It's not perfect, but it should get the job done.
这不是完美的,但应该可以完成工作。
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.
例如,如果在某些杂乱无章的人面前有Apache,则可以通过ErrorDocument指令配置Apache以在发生错误时显示合适的文件。
What is your front-end? 你的前端是什么?
Stephan 史蒂芬
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.