"Is my site offline?" how

What is the best way to create "Is my site inaccessible?" in ruby? How to do this to verify this using HTTP (s) and Ping?

Thank.

+3
source share
2 answers

Basically, just use the http library to see if you can (in fact, HEADing will be better) on the page they point to. If you get a response, then the server will stop, otherwise (it does not respond or the timeout expires) it does not work, and you warn the user accordingly.

This is not the cleanest way to do this, but basically:

require 'net/http'
require 'uri'

def isUp( url )
    uri = URI.parse( url )

    begin
        Timeout::timeout(5) {
            Net::HTTP.start( uri.host, uri.port ) { |http|
                 http.head( uri.path )
            }
        }
    rescue Timeout::Error
        return false
    end

    return true 
end

, / , , , .

+3

()

require 'uri'
require 'open-uri'
site = "http://stackoverflow.com/"
open("http://downforeveryoneorjustme.com/#{URI.parse(site).host}"){|f|f.read}["It not just you!"]
+2

Source: https://habr.com/ru/post/1773150/


All Articles