Class: Gorgon::PingService

Inherits:
Object
  • Object
show all
Includes:
Configuration
Defined in:
lib/gorgon/ping_service.rb

Constant Summary collapse

TIMEOUT =
4

Instance Method Summary collapse

Methods included from Configuration

#load_configuration_from_file

Constructor Details

#initializePingService

Returns a new instance of PingService.



14
15
16
17
18
19
# File 'lib/gorgon/ping_service.rb', line 14

def initialize
  @configuration = load_configuration_from_file("gorgon.json", merge: "gorgon_secret.json")
  @logger = OriginatorLogger.new @configuration[:originator_log_file]
  @protocol = OriginatorProtocol.new @logger
  @listeners = []
end

Instance Method Details

#ping_listenersObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gorgon/ping_service.rb', line 21

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