2010-05-26 37 views
1

我正在尋找一種解決方案,以便在沒有可用於連接的Mysql服務器時,允許我的Rails應用程序呈現用戶友好的維護頁面。Rails:如果沒有可用的數據庫連接,顯示維護頁面

通常一個Mysql::ErrorMySQL連接適配器像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適配器中創建的,所以應用程序在它引發錯誤之前從未將其發送到控制器堆棧,因此您無法在控制器中捕獲它。

任何輸入將不勝感激。

回答

1

您可以創建一個視圖,無論你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」動作添加到應用程序控制器,並手動指定它應該呈現的視圖。這並不完美,但它應該完成工作。

0

我認爲這是關於您的前端配置。例如,如果Apache在某些mongrels前面,則可以通過ErrorDocument指令來配置Apache,以便在發生錯誤時顯示合適的文件。

你的前端是什麼?

斯蒂芬

+0

Stephan,謝謝你的回覆。在這種情況下,我使用Nginx> Mongrel Cluster。 – RobB 2010-05-27 17:31:21