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
source
share