Class: Racecar::LivenessProbe

Inherits:
Object
  • Object
show all
Defined in:
lib/racecar/liveness_probe.rb

Instance Method Summary collapse

Constructor Details

#initialize(message_bus, file_path, max_interval) ⇒ LivenessProbe

Returns a new instance of LivenessProbe.



5
6
7
8
9
10
# File 'lib/racecar/liveness_probe.rb', line 5

def initialize(message_bus, file_path, max_interval)
  @message_bus = message_bus
  @file_path = file_path
  @max_interval = max_interval
  @subscribers = []
end

Instance Method Details

#check_liveness_within_interval!Object



15
16
17
18
19
20
# File 'lib/racecar/liveness_probe.rb', line 15

def check_liveness_within_interval!
  unless liveness_event_within_interval?
    $stderr.puts "Racecar healthcheck failed: No liveness within interval #{max_interval}s. Last liveness at #{last_liveness_event_at}, #{elapsed_since_liveness_event} seconds ago."
    Process.exit(1)
  end
end

#installObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/racecar/liveness_probe.rb', line 29

def install
  unless file_path && file_writeable?
    raise(
      "Liveness probe configuration error: `liveness_probe_file_path` must be set to a writable file path.\n" \
        "  Set `RACECAR_LIVENESS_PROBE_FILE_PATH` and `RACECAR_LIVENESS_MAX_INTERVAL` environment variables."
    )
  end

  subscribers << message_bus.subscribe("start_main_loop.racecar") do
    touch_liveness_file
  end

  subscribers = message_bus.subscribe("shut_down.racecar") do
    delete_liveness_file
  end

  nil
end

#liveness_event_within_interval?Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
# File 'lib/racecar/liveness_probe.rb', line 22

def liveness_event_within_interval?
  elapsed_since_liveness_event < max_interval
rescue Errno::ENOENT
  $stderr.puts "Racecar healthcheck failed: Liveness file not found `#{file_path}`"
  Process.exit(1)
end

#uninstallObject



48
49
50
# File 'lib/racecar/liveness_probe.rb', line 48

def uninstall
  subscribers.each { |s| message_bus.unsubscribe(s) }
end