lib/poolparty/monitors/monitor_daemon.rb in auser-poolparty-1.2.11 vs lib/poolparty/monitors/monitor_daemon.rb in auser-poolparty-1.2.12

- old
+ new

@@ -1,45 +1,63 @@ require "open-uri" +require "json" +require "#{::File.dirname(__FILE__)}/base_monitor" module PoolParty - class MonitorDaemon + class MonitorDaemon < Monitors::BaseMonitor - attr_reader :should_daemonize, :pid_file, :log_file_path, :sleep_time + attr_reader :should_daemonize, :pid_file, :sleep_time def self.run(o={}) new(o).run end def initialize(o={}) @should_daemonize = o.delete(:daemonize) @pid_file = o.delete(:daemonize) || "/tmp/poolparty_monitor.pid" - temp_log_file_path = o.delete(:log_file_path) || "poolparty_monitor.log" - @log_file_path = temp_log_file_path - - unless ::File.file?(temp_log_file_path) - ::FileUtils.mkdir_p ::File.dirname(temp_log_file_path) unless ::File.directory?(::File.dirname(temp_log_file_path)) - ::File.open(temp_log_file_path, 'a+') - temp_log_file_path - end - @sleep_time = o.delete(:sleep_time) || 5 + @sleep_time = o.delete(:sleep_time) || 20 + + super end def pass_the_baton - %w(Memory neighborhood elections).each do |monitor| - out = open("http://localhost:8642/#{monitor}").read - log "#{monitor} / #{out.inspect}" + # Handle stats + my_nominations = JSON.parse(open("http://localhost:8642/stats/get_nominations").read) + unless my_nominations.empty? + running_nodes = my_cloud.nodes(:status => "running") + nominations = [] + running_nodes.each do |node| + timeout(10) do + log "Checking with #{node.internal_ip} for nominations: #{open("http://#{node.internal_ip}:8642/stats/get_nominations").read}" + nominations << begin + JSON.parse(open("http://#{node.internal_ip}:8642/stats/nominations").read) || "none" + rescue + log "Error when connecting to #{node.internal_ip}: #{e.inspect}" + "none" + end + end + end + log "Sending #{nominations.flatten.to_json} to #{server["/elections"].inspect}" + # put to "http://localhost:8642/elections/handle_election", data => nominations.to_json + server["/elections"].put(nominations.flatten.to_json) end - sleep sleep_time end def run if should_daemonize @should_daemonize = false daemonize else log "Starting MonitorDaemon" - loop {pass_the_baton} + loop { + begin + pass_the_baton + rescue Exception => e + log "There was an error with pass_the_baton: #{e}" + end + sleep sleep_time + } end end def daemonize(o={}) raise unless pid_file @@ -69,33 +87,11 @@ write_pid_file(pid) Process.detach(pid) end - - def log(msg) - log_file.flush - log_file << "[INFO] - #{Time.now} -- #{msg}\n" - end - - private - - def log_file - if @logfile - @logfile - else - begin - ::FileUtils.mkdir_p ::File.dirname(log_file_path) unless ::File.directory?(::File.dirname(log_file_path)) - @logfile ||= ::File.open(log_file_path, 'a+') - rescue Exception => e - puts "ERROR: #{e.inspect}" - @logfile = $stdout - end - end - end - def pid @pid ||= File.file?(pid_file) ? open(pid_file).read.to_i : nil end def stop! @@ -130,11 +126,11 @@ end rescue Errno::ESRCH # No such process puts "process not found!" nil end - + protected def remove_pid_file File.delete(pid_file) if pid_file && File.exists?(pid_file) end @@ -151,9 +147,22 @@ else remove_pid_file end end end - + + def server + if @server + @server + else + opts = { :content_type =>'application/json', + :accept => 'application/json', + :host => 'http://localhost', + :port => '8642' + } + @uri = "#{opts.delete(:host)}:#{opts.delete(:port)}" + @server = RestClient::Resource.new( @uri, opts) + end + end end end \ No newline at end of file