lib/deputy.rb in deputy-0.1.19 vs lib/deputy.rb in deputy-0.1.20
- old
+ new
@@ -113,11 +113,13 @@
get "/report/#{CGI.escape metric}?value=#{CGI.escape value.to_s}"
end
def self.get(path)
url = "#{sheriff_url}#{path}"
- open(url).read
+ Timeout.timeout(1) do
+ open(url).read
+ end
rescue => e
e.message << url
raise e
end
@@ -129,7 +131,29 @@
home = File.expand_path('~')
["#{home}/.deputy.yml", '/etc/deputy.yml'].each do |file|
return YAML.load(File.read(file)) if File.exist?(file)
end
raise "No deputy.yml found in /etc or #{home}"
+ end
+
+ # stolen from klarlack -- http://github.com/schoefmax/klarlack
+ # to get an reliable timeout that wont fail on other platforms
+ # or if sytem_timer is missing
+ Timeout = begin
+ # Try to use the SystemTimer gem instead of Ruby's timeout library
+ # when running on something that looks like Ruby 1.8.x. See:
+ # http://ph7spot.com/articles/system_timer
+ # We don't want to bother trying to load SystemTimer on jruby and
+ # ruby 1.9+.
+ if RUBY_VERSION =~ /^1\.8\./ and RUBY_PLATFORM !~ /java/
+ require 'system_timer'
+ SystemTimer
+ else
+ require 'timeout'
+ Timeout
+ end
+ rescue LoadError => e
+ $stderr.puts "Could not load SystemTimer gem, falling back to Ruby's slower/unsafe timeout library: #{e.message}"
+ require 'timeout'
+ Timeout
end
end
\ No newline at end of file