/
heartbeat.rb
44 lines (38 loc) · 1.12 KB
/
heartbeat.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
module Etcd
class Heartbeat
include Etcd::Loggable
attr_accessor :client
attr_accessor :freq
def initialize(client, freq)
@client = client
@freq = freq
end
# Initiates heartbeating the leader node in a background thread
# ensures, that observers are refreshed after leader re-election
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
# The command to check leader online status,
# runs in background and is resilient to failures
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
end
end