Class: Schked::LivenessProbe

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:, logger:) ⇒ LivenessProbe

Returns a new instance of LivenessProbe.



90
91
92
93
94
95
96
97
98
# File 'lib/schked/liveness_probe.rb', line 90

def initialize(config:, logger:)
  @config = config
  @logger = logger
  @last_heartbeat_at = nil
  @shutting_down = false
  @server = nil
  @thread = nil
  @mutex = Mutex.new
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



88
89
90
# File 'lib/schked/liveness_probe.rb', line 88

def config
  @config
end

#loggerObject (readonly)

Returns the value of attribute logger.



88
89
90
# File 'lib/schked/liveness_probe.rb', line 88

def logger
  @logger
end

Instance Method Details

#healthy?Boolean

Returns:

  • (Boolean)


129
130
131
132
133
134
# File 'lib/schked/liveness_probe.rb', line 129

def healthy?
  return false if @shutting_down
  return false if @last_heartbeat_at.nil?

  Time.now - @last_heartbeat_at <= config.heartbeat_threshold
end

#heartbeatObject



112
113
114
# File 'lib/schked/liveness_probe.rb', line 112

def heartbeat
  @last_heartbeat_at = Time.now
end

#startObject



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/schked/liveness_probe.rb', line 100

def start
  return unless config.enabled

  @server = create_server
  logger.info("Schked liveness probe listening on #{config.bind}:#{config.port}#{config.path}")

  @thread = Thread.new {
    Thread.current.name = "schked-liveness-probe" if Thread.current.respond_to?(:name=)
    accept_loop
  }
end

#stopObject



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/schked/liveness_probe.rb', line 116

def stop
  @mutex.synchronize do
    return if @shutting_down

    @shutting_down = true
  end

  sleep 0.1
  @server&.close
  @thread&.join(5)
  logger.info("Schked liveness probe stopped")
end