2017-08-06 104 views
0

我想要實現一個問題,我們手動去檢查一個webapp /服務器,如果它是上/下。我想構建一個可以自動執行此任務的rails應用程序。Rails應用程序來檢查服務器的狀態

考慮我的應用程序的網址是:主機名:PORT /路由PARAMS(可能會或可能不會在URL端口)

我檢查 '網/ HTTP'

def check_status() 
     @url='host' 
     uri = URI(@url) 
     http = Net::HTTP.new(@url,port) 
     response = http.request_get('/<route>?<params>') 
     if response == Net::HTTPSuccess 
     @result='Running' 
     else 
     @result='Not Running' 
     end 
    end 

我面對錯誤的,

response = http.request_get('/<route>?<params>') 

當應用程序被向下拋「無法打開TCP連接到URL」,這是正確的。

你們可以幫我找到一些新的解決方案,或者我該如何改進上述實施?

回答

1

由於它按預期工作,您只需處理應用程序關閉時返回的錯誤,將其包裝在救援塊中。

def check_status() 
     @url='host' 
     uri = URI(@url) 
     http = Net::HTTP.new(@url,port) 
     begin 
     response = http.request_get('/<route>?<params>') 
     rescue TheClassNameOfThisErrorWhenSiteIsDown 
      @result = 'Not Running' 
     end 
     if response == Net::HTTPSuccess 
      @result='Running' 
     else 
      @result='Not Running' 
     end 
     end 
    end 
+1

嗨,感謝您的信息。我利用了救援塊並做了一些調整。它的工作現在很好。 – mRbOneS

相關問題