Class: Etcd::Heartbeat

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/etcd/heartbeat.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#logger, #reset_logger!

Constructor Details

#initialize(client, freq) ⇒ Heartbeat


7
8
9
10
# File 'lib/etcd/heartbeat.rb', line 7

def initialize(client, freq)
  @client = client
  @freq   = freq
end

Instance Attribute Details

#clientObject

Returns the value of attribute client


5
6
7
# File 'lib/etcd/heartbeat.rb', line 5

def client
  @client
end

#freqObject

Returns the value of attribute freq


6
7
8
# File 'lib/etcd/heartbeat.rb', line 6

def freq
  @freq
end

Instance Method Details

#heartbeat_commandObject

The command to check leader online status, runs in background and is resilient to failures


29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/etcd/heartbeat.rb', line 29

def heartbeat_command
  logger.debug("heartbeat_command: enter ")
  logger.debug(client.observers_overview.join(", "))
  begin
    client.refresh_observers_if_needed
    client.update_cluster if client.status == :down
    client.get("foo")
  rescue Exception => e
    client.status = :down
    logger.debug "heartbeat - #{e.message} #{e.backtrace}"
  end
  sleep freq
end

#start_heartbeat_if_neededObject

Initiates heartbeating the leader node in a background thread ensures, that observers are refreshed after leader re-election


15
16
17
18
19
20
21
22
23
24
# File 'lib/etcd/heartbeat.rb', line 15

def start_heartbeat_if_needed
  logger.debug ("start_heartbeat_if_needed - enter")
  return if freq == 0
  return if @heartbeat_thread
  @heartbeat_thread = Thread.new do
    while true do
      heartbeat_command
    end
  end
end