Module: Legion::Readiness

Defined in:
lib/legion/readiness.rb

Constant Summary collapse

REQUIRED_COMPONENTS =
%i[settings crypt transport cache data extensions api].freeze
OPTIONAL_COMPONENTS =
%i[rbac llm apollo gaia identity].freeze
COMPONENTS =
(REQUIRED_COMPONENTS + OPTIONAL_COMPONENTS).freeze
DRAIN_TIMEOUT =
5

Class Method Summary collapse

Class Method Details

.mark_not_ready(component) ⇒ Object



22
23
24
25
# File 'lib/legion/readiness.rb', line 22

def mark_not_ready(component)
  status[component.to_sym] = false
  Legion::Logging.debug "[Readiness] #{component} is not ready" if defined?(Legion::Logging)
end

.mark_ready(component) ⇒ Object



17
18
19
20
# File 'lib/legion/readiness.rb', line 17

def mark_ready(component)
  status[component.to_sym] = true
  Legion::Logging.info "[Readiness] #{component} is ready" if defined?(Legion::Logging)
end

.mark_skipped(component) ⇒ Object



27
28
29
30
# File 'lib/legion/readiness.rb', line 27

def mark_skipped(component)
  status[component.to_sym] = :skipped
  Legion::Logging.debug "[Readiness] #{component} skipped (optional)" if defined?(Legion::Logging)
end

.ready?(component = nil) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
# File 'lib/legion/readiness.rb', line 32

def ready?(component = nil)
  if component
    result = [true, :skipped].include?(status[component.to_sym])
    Legion::Logging.warn "[Readiness] #{component} is not ready" if !result && defined?(Legion::Logging)
    return result
  end

  not_ready = COMPONENTS.reject { |c| [true, :skipped].include?(status[c]) }
  not_ready.each { |c| Legion::Logging.warn "[Readiness] #{c} is not ready" } if !not_ready.empty? && defined?(Legion::Logging)
  not_ready.empty?
end

.resetObject



54
55
56
# File 'lib/legion/readiness.rb', line 54

def reset
  @status = nil
end

.statusObject



13
14
15
# File 'lib/legion/readiness.rb', line 13

def status
  @status ||= Concurrent::Hash.new
end

.to_hObject



58
59
60
61
62
63
# File 'lib/legion/readiness.rb', line 58

def to_h
  COMPONENTS.to_h do |c|
    val = status[c]
    [c, [true, :skipped].include?(val)]
  end
end

.wait_until_not_ready(*components, timeout: DRAIN_TIMEOUT) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/legion/readiness.rb', line 44

def wait_until_not_ready(*components, timeout: DRAIN_TIMEOUT)
  deadline = Time.now + timeout
  loop do
    break if components.all? { |c| status[c] != true }
    break if Time.now > deadline

    sleep(0.1)
  end
end