0

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?

mRbOneS
  • 121
  • 1
  • 14

2 Answers2

1

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
bkunzi01
  • 4,504
  • 1
  • 18
  • 25
  • 1
    Hi, Thanks for the info. I made use of rescue block and made few tweaks. Its working fine now. – mRbOneS Aug 07 '17 at 10:23
0

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
Mark Thomas
  • 37,131
  • 11
  • 74
  • 101