我正在寻找一个解决方案,将允许我的Rails应用程序呈现一个用户友好的维护页面时,没有可用的MySQL服务器连接。
通常,从Mysql::Error中的MySQL连接适配器抛出一个active_record,类似于:
/!\ 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是否有一种低开销的方法来捕获此错误并呈现一个维护页面?
我假设,由于连接实际上是在active_record MySQL适配器中建立的,所以应用程序从未在抛出错误之前到达控制器堆栈,因此您无法在控制器中捕获它。
发布于 2010-07-13 05:11:59
您可以在任何root_path控制器中创建视图:
map.root :controller => "foo", :action => "index"假设您将此视图称为"db_maintenance.html.erb“。在您的控制器中,执行以下操作:
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在你看来,你可以这样说:
<h1>Sorry for the inconvenience</h1>
blah blah blah. This happened because of:
<pre><code><%= @error %></code></pre>当然,只有当用户访问站点的主页时,这才会有所帮助,但您可以很容易地从中推断出来。您可以将"def db_maintenance“操作添加到应用程序控制器中,并手动指定它应该呈现的视图。这并不完美,但它应该能完成任务。
发布于 2010-05-27 01:16:52
我想这是关于你的前端配置的。例如,如果前面有Apache,则可以通过ErrorDocument指令配置Apache,以便在出现错误时显示合适的文件。
你的前端是什么?
斯蒂芬恩
https://stackoverflow.com/questions/2915792
复制相似问题