Class: GorgonBunny::HeartbeatSender

Inherits:
Object
  • Object
show all
Defined in:
lib/gorgon_bunny/lib/gorgon_bunny/heartbeat_sender.rb

Overview

Periodically sends heartbeats, keeping track of the last publishing activity.

Instance Method Summary collapse

Constructor Details

#initialize(transport, logger) ⇒ HeartbeatSender

API



15
16
17
18
19
20
21
# File 'lib/gorgon_bunny/lib/gorgon_bunny/heartbeat_sender.rb', line 15

def initialize(transport, logger)
  @transport = transport
  @logger    = logger
  @mutex     = Monitor.new

  @last_activity_time = Time.now
end

Instance Method Details

#signal_activity!Object



39
40
41
# File 'lib/gorgon_bunny/lib/gorgon_bunny/heartbeat_sender.rb', line 39

def signal_activity!
  @last_activity_time = Time.now
end

#start(period = 30) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/gorgon_bunny/lib/gorgon_bunny/heartbeat_sender.rb', line 23

def start(period = 30)
  @mutex.synchronize do
    # calculate interval as half the given period plus
    # some compensation for Ruby's implementation inaccuracy
    # (we cannot get at the nanos level the Java client uses, and
    # our approach is simplistic). MK.
    @interval = [(period / 2) - 1, 0.4].max

    @thread = Thread.new(&method(:run))
  end
end

#stopObject



35
36
37
# File 'lib/gorgon_bunny/lib/gorgon_bunny/heartbeat_sender.rb', line 35

def stop
  @mutex.synchronize { @thread.exit }
end