Reputation: 121
I want to achieve a problem, where we manually go and check a webapp/server if it is up/down. I want to build a rails app which can automate this task.
Consider my app url is: HostName:PORT/Route?Params (may or may not have port in url)
I checked 'net/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
I am facing error at ,
response = http.request_get('/<route>?<params>')
when the app is down throwing 'Failed to open TCP connection to URL' which is correct.
Can you guys help me find some new solution or how can I improve the above implementation?
Upvotes: 0
Views: 883
Reputation: 37517
Just came across this old question. Net::HTTP methods get
and head
don't raise an exception. So use one of these instead.
def up?(site)
Net::HTTP.new(site).head('/').kind_of? Net::HTTPOK
end
up? 'www.google.com' #=> true
Upvotes: 0
Reputation: 4561
Since it's working as intended and you just need to handle the error that's returned when the app is down, wrap it in a rescue block.
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
Upvotes: 1