Note that there are some explanatory texts on larger screens.

plurals
  1. POcode to ping websites works sometimes
    text
    copied!<p>I'm testing out a piece of code to ping a bunch of websites I own on a regular basis, to make sure they're up. </p> <p>I'm using rails and so far I have this hideous test action that I'm using to try it out (see below).<br> The problem though, is that sometimes it works, and other times it won't ... sometimes it runs through the code just fine, other times, it seems to completely ignore the begin/rescue block ...</p> <p>a. I need help figuring out what the problem is b. And refactoring this to make it look respectable.</p> <p>Your help is much appreciated.</p> <p>edit 1: Here is the updated code, sorry it took so long, pastie.org was down since yesterday <a href="http://pastie.org/927201" rel="nofollow noreferrer">http://pastie.org/927201</a></p> <p>Its still doing the same thing ... skipping the begin block (because it only updates up_check_time) ... however if one of the sites times out, it actually updates everything (check_msg, code etc) correctly ... confusing, yeah?</p> <pre><code>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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload