lib/deputy.rb in deputy-0.1.48 vs lib/deputy.rb in deputy-0.1.49
- old
+ new
@@ -14,11 +14,12 @@
class Scout
class Plugin
OPTIONS = {}.to_yaml
def self.clean_class_name
- to_s.split('::')[1..-1].join('::')
+ parts = to_s.split('::')
+ parts.size == 1 ? parts.first : parts[1..-1].join('::')
end
protected
def report(metrics)
@@ -82,40 +83,39 @@
constants.detect{|c| c.instance_methods.map{|m| m.to_s}.include?('build_report') }
end
end
module Deputy
- START_MINUTE = (Time.now.to_i + 30) / 60 # we could start at 58..02 seconds -> always in middle of minute
VERSION = File.read( File.join(File.dirname(__FILE__),'..','VERSION') ).strip
DEFAULT_VALUE = 'OK'
def self.install_cron
executable = `which deputy`.strip
- unless (`crontab -l`).include?(executable)
+ unless (`crontab -l`).include?(executable)
`crontab -l | { cat; echo "* * * * * #{executable} --run-plugins >> /tmp/deputy.log 2>&1"; } | crontab -`
if executable !~ %r{^/usr/}
puts "make deputy globally available! or e.g. calls from inside cronjobs do not know deputy"
puts "sudo ln -s #{executable} /usr/bin/deputy"
end
end
end
def self.run_plugins(options={})
+ start_time = Time.now.to_i
sleep_random_interval unless options[:no_wait]
content = get("/plugins.rb")
exceptions = []
Scout.plugins(content).each do |interval, plugin|
- run_every_n_minutes = interval/60
- minutes_to_wait = run_every_n_minutes - (START_MINUTE % run_every_n_minutes)
- if minutes_to_wait == run_every_n_minutes
+ wait = minutes_to_wait(start_time, interval)
+ if wait == 0
puts "#{plugin.clean_class_name}: running"
begin
plugin.new.build_report
rescue Object => e # catch and report plugin-specific errors
- e.message[0..0] = plugin.to_s.split('::')[1..-1].join
+ e.message[0..0] = plugin.clean_class_name
puts e
exceptions << e
end
else
puts "#{plugin.clean_class_name}: waiting another #{minutes_to_wait} minutes"
@@ -129,19 +129,18 @@
raise e
end
def self.send_report(group, value, options = {})
raise "separate #{group} with a ." unless group.split('.',2).size == 2
- host = options[:host] || Socket.gethostname
-
- get "/notify?group=#{CGI.escape group}&value=#{CGI.escape value.to_s}&hostname=#{host}#{'&forced_host=true' if options[:host]}"
+ get "/notify?group=#{CGI.escape group}&value=#{CGI.escape value.to_s}", options
end
def self.get(path, options = {})
url = "#{sheriff_url}#{path}"
url = "http://#{url}" unless url =~ %r{://}
options[:http_basic_authentication] = extract_auth_from_url!(url)
+ url = add_host_to_url(url, options.delete(:host))
Timeout.timeout(config['timeout']||10) do
open(url, options).read
end
rescue Exception => e
@@ -159,10 +158,16 @@
return YAML.load(File.read(file)) if File.exist?(file)
end
raise "No deputy.yml found in /etc or #{home}"
end
+ def self.minutes_to_wait(start_time, interval)
+ start_minute = start_time / 60
+ run_every_n_minutes = interval / 60
+ start_minute % run_every_n_minutes
+ end
+
def self.sleep_random_interval
if max = config['max_random_start_delay']
constant_number = Socket.gethostname.sum{|x| x[0]}
sleep seeded_random(max, constant_number)
end
@@ -199,7 +204,13 @@
def self.extract_auth_from_url!(url)
url.sub!(%r{//(.*?):(.*?)@}, '//')
auth = [$1, $2].compact
auth.empty? ? nil : auth
+ end
+
+ def self.add_host_to_url(url, host=nil)
+ query = "hostname=#{host || Socket.gethostname}#{'&forced_host=true' if host}"
+ separator = (url.include?('?') ? "&" : "?")
+ url + separator + query
end
end