Class: IbmAppconfigurationRubySdk::Watchdog

Inherits:
Object
  • Object
show all
Defined in:
lib/ibm_appconfiguration_ruby_sdk/websocket/watchdog.rb

Constant Summary collapse

WATCHDOG_CONFIG =
{
  check_interval: 60,
  heartbeat_timeout: 120
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Watchdog

Returns a new instance of Watchdog.



26
27
28
29
# File 'lib/ibm_appconfiguration_ruby_sdk/websocket/watchdog.rb', line 26

def initialize(client)
  @client = client
  @logger = IbmAppconfigurationRubySdk::Logger.instance
end

Instance Method Details

#startObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ibm_appconfiguration_ruby_sdk/websocket/watchdog.rb', line 31

def start
  if @client.connected?
    @logger.log("Watchdog timer started - monitoring heartbeat every #{WATCHDOG_CONFIG[:check_interval]}s")
  else
    @logger.log(Constants::WATCHDOG_NOT_RUNNING)
    return Thread.new {} # no-op thread
  end

  Thread.new do
    loop do
      sleep WATCHDOG_CONFIG[:check_interval]

      break unless @client.connected?

      heartbeat_age =
        Time.now - @client.last_heartbeat_at

      next unless heartbeat_age >
                  WATCHDOG_CONFIG[:heartbeat_timeout]

      @logger.log(Constants::WATCHDOG_STOPPING)
      @logger.warning(
        "Watchdog detected stale connection - no heartbeat received for " \
        "#{heartbeat_age.round(1)}s (threshold: #{WATCHDOG_CONFIG[:heartbeat_timeout]}s)"
      )

      @client.handle_disconnect("Heartbeat timeout")

      break
    end
  end
end