Search code examples
ruby-on-railshttpweblogicnet-http

Rails app to check the status of a server


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?


Solution

  • 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