Sha256: f9e809832f0af973fc264c05573b1daf095d17265fcd923f0f174e21306e4fb1
Contents?: true
Size: 1.64 KB
Versions: 16
Compression:
Stored size: 1.64 KB
Contents
require 'gorgon/originator_protocol' require 'gorgon/configuration' require 'gorgon/originator_logger' require 'gorgon/colors' require 'colorize' class PingService include Configuration TIMEOUT=4 def initialize @configuration = load_configuration_from_file("gorgon.json") @logger = OriginatorLogger.new @configuration[:originator_log_file] @protocol = OriginatorProtocol.new @logger @listeners = [] end def ping_listeners Signal.trap("INT") { disconnect } Signal.trap("TERM") { disconnect } EventMachine.run do @logger.log "Connecting..." @protocol.connect @configuration[:connection], :on_closed => proc {EM.stop} @logger.log "Pinging Listeners..." @protocol.send_message_to_listeners :ping EM.add_timer(TIMEOUT) { disconnect } @protocol.receive_payloads do |payload| @logger.log "Received #{payload}" handle_reply(Yajl::Parser.new(:symbolize_keys => true).parse(payload)) end end end private def disconnect @protocol.disconnect print_summary end def handle_reply payload if payload[:type] != "ping_response" puts "Unexpected message received: #{payload}" return end @listeners << payload hostname = payload[:hostname].colorize(Colors::HOST) puts "#{hostname} is running Listener version #{payload[:version]} and uses #{payload[:worker_slots]} workers" end def print_summary worker_slots = @listeners.inject(0) { |sum, l| sum + l[:worker_slots] } puts "\n#{@listeners.size} host(s) responded using a total of #{worker_slots} worker(s)." end def on_disconnect EventMachine.stop end end
Version data entries
16 entries across 16 versions & 1 rubygems