Module: Legion::API::Health

Defined in:
lib/legion/api/health.rb

Overview

Subsystem health assessment for GET /api/health.

A component degrades health ONLY if it is both:

1. enabled — Legion::Readiness.status[c] == true (Service marks a
 component ready only when its config flag is on AND setup succeeded).
 :skipped (disabled) and false/nil (never booted / still booting) are
 NOT enabled-and-previously-healthy, so they never fail health.
2. currently unhealthy — its live liveness check now returns false.

This prevents both false negatives (200 while transport can't process messages) and false positives (503 for a subsystem that was never configured or hasn't finished booting).

Constant Summary collapse

COMPONENTS =
%i[transport cache data].freeze

Class Method Summary collapse

Class Method Details

.assessObject

Returns { status:, components: { name => { enabled:, healthy:, detail? } } }



22
23
24
25
26
# File 'lib/legion/api/health.rb', line 22

def assess
  components = COMPONENTS.to_h { |name| [name, component_health(name)] }
  degraded = components.any? { |_name, c| c[:enabled] && c[:healthy] == false }
  { status: degraded ? 'degraded' : 'ok', components: components }
end

.cache_livenessObject



51
52
53
54
55
56
57
58
# File 'lib/legion/api/health.rb', line 51

def cache_liveness
  return [true, nil] unless defined?(Legion::Cache)

  connected = Legion::Cache.connected?
  [connected, connected ? nil : 'cache not connected']
rescue StandardError => e
  [false, e.message]
end

.component_health(name) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/legion/api/health.rb', line 28

def component_health(name)
  enabled = Legion::Readiness.status[name] == true
  return { enabled: false, healthy: nil } unless enabled

  healthy, detail = send("#{name}_liveness")
  info = { enabled: true, healthy: healthy }
  info[:detail] = detail if detail && !healthy
  info
end

.data_livenessObject



60
61
62
63
64
65
66
67
68
69
# File 'lib/legion/api/health.rb', line 60

def data_liveness
  connected = begin
    Legion::Settings[:data][:connected] == true
  rescue StandardError
    false
  end
  [connected, connected ? nil : 'data not connected']
rescue StandardError => e
  [false, e.message]
end

.transport_livenessObject

--- liveness checks: [healthy_boolean, detail_string_or_nil] ---



40
41
42
43
44
45
46
47
48
49
# File 'lib/legion/api/health.rb', line 40

def transport_liveness
  conn = Legion::Transport::Connection
  return [true, nil] if conn.respond_to?(:lite_mode?) && conn.lite_mode?

  session = conn.session
  open = session.respond_to?(:open?) && session.open?
  [open, open ? nil : 'session_open: false']
rescue StandardError => e
  [false, e.message]
end