The code for ping websites works sometimes

I am testing a piece of code to type a bunch of websites that I own on a regular basis to make sure they are up.

I use rails and so far I have this terrible test action that I use to try it (see below).
The problem is that sometimes it works, and in other cases it will not ... sometimes it goes through the code just fine, in other cases it seems to completely ignore the start / save block ...

and. I need help finding a problem. b. And refactoring so that it looks respectable.

Your help is greatly appreciated.

edit 1: Here is the updated code, sorry for so long, pastie.org has not been since yesterday http://pastie.org/927201

It still does the same thing ... skips the start block (because it only updates up_check_time) ... however, if one of the sites expires, it actually updates everything (check_msg, code, etc.) correctly .. confusing, right?

require 'net/http'
require 'uri'

def ping
    @sites = NewsSource.all

    @sites.each do |site|
        if site.uri and !site.uri.empty?
            uri = URI.parse(site.uri)
            response = nil
            path = uri.path.blank? ? '/' : uri.path
            path = uri.query.blank? ? path : "#{path}?#{uri.query}"

            begin
                Net::HTTP.start(uri.host, uri.port) {|http|
                http.open_timeout = 30
                http.read_timeout = 30
                response = http.head(path)
                }

                if response.code.eql?('200') or response.code.eql?('301') or response.code.eql?('302')
                site.up = true
                else
                site.up = false
                end

                site.up_check_msg = response.message
                site.up_check_code = response.code
            rescue Errno::EBADF
            rescue Timeout::Error
                site.up = false
                site.up_check_msg = 'timeout'
                site.up_check_code = '408'
            end
            site.up_check_time = 0.seconds.ago
            site.save
        end
    end
end
+5
source share
4 answers

You currently have an empty block rescuefor Errno::EBADF, so if this exception is raised, you will not install site.upon false.

In addition, several other minor improvements:

Instead if site.uri and !site.uri.empty?you can use:

next if site.uri.nil? or site.uri.empty?

to skip this loop iteration eachand avoid code indentation at an extra level.

and

if response.code.eql?('200') or response.code.eql?('301') or response.code.eql?('302')
  site.up = true
else
  site.up = false
end

you can write more briefly:

site.up = ['200', '301', '302'].include? response.code

, .

+3

, , :

urls.each_with_index do |url, idx|
  print "Processing URL #%04d: " % (idx+1)
  uri = URI.parse(url)
  response = nil

  begin
    Net::HTTP.start(uri.host, uri.port) do |http|
      response = http.head(uri.path.size > 0 ? uri.path : "/")
    end
  rescue => e
    puts "#{e.message} - #{url}"
    next
  end

  # handle redirects
  if response.is_a?(Net::HTTPRedirection)
    new_uri = URI.parse(response['location'])
    puts "URI redirects to #{new_uri}"
    next
  end

  puts case response.code
    when '200' then ...
    when '404' then ...
    else ...
  end
end
+1

, , , - . Errno:: EBADF, Timeout:: Error, , . , , Errno:: EBADF, Timeout:: Error ,

logger.info(">>Exception was: "+$!)

, , .

0

All Articles