Module: Legion::Extensions::Catalog

Defined in:
lib/legion/extensions/catalog.rb,
lib/legion/extensions/catalog/registry.rb,
lib/legion/extensions/catalog/available.rb

Defined Under Namespace

Modules: Available, Registry

Constant Summary collapse

STATES =
%i[registered loaded starting running stopping stopped].freeze
STATE_ORDER =
STATES.each_with_index.to_h.freeze

Class Method Summary collapse

Class Method Details

.allObject



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

def all
  entries.dup
end

.entry(lex_name) ⇒ Object



39
40
41
# File 'lib/legion/extensions/catalog.rb', line 39

def entry(lex_name)
  entries[lex_name]
end

.flush_persisted_transitionsObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/legion/extensions/catalog.rb', line 65

def flush_persisted_transitions
  pending = nil
  @pending_persists_mutex ||= Mutex.new
  @pending_persists_mutex.synchronize do
    return if @pending_persists.nil? || @pending_persists.empty?

    pending = @pending_persists.dup
    @pending_persists.clear
  end

  return unless defined?(Legion::Data::Local) &&
                Legion::Data::Local.respond_to?(:connected?) &&
                Legion::Data::Local.connected?

  ensure_local_migration_registered!
  return warn_missing_extension_catalog_once unless extension_catalog_table_available?

  model = Legion::Data::Local.model(:extension_catalog)
  now = Time.now
  Legion::Data::Local.connection.transaction do
    pending.each do |lex_name, new_state|
      existing = model.where(lex_name: lex_name).first
      if existing
        existing.update(state: new_state.to_s, updated_at: now)
      else
        model.insert(lex_name: lex_name, state: new_state.to_s, created_at: now, updated_at: now)
      end
    end
  end
  Legion::Logging.info "Catalog persisted #{pending.size} transitions" if defined?(Legion::Logging)
rescue StandardError => e
  Legion::Logging.warn { "Catalog flush failed: #{e.class}: #{e.message}" } if defined?(Legion::Logging)
end

.loaded?(lex_name) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
# File 'lib/legion/extensions/catalog.rb', line 43

def loaded?(lex_name)
  s = state(lex_name)
  return false unless s

  STATE_ORDER[s] >= STATE_ORDER[:loaded]
end

.register(lex_name, state: :registered) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/legion/extensions/catalog.rb', line 13

def register(lex_name, state: :registered)
  return if @entries&.key?(lex_name)

  entries[lex_name] = {
    state:         state,
    registered_at: Time.now,
    started_at:    nil,
    stopped_at:    nil
  }
end

.reset!Object



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

def reset!
  @entries = {}
  @extension_catalog_available = nil
  @extension_catalog_connection_id = nil
  @warned_missing_extension_catalog = false
end

.running?(lex_name) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/legion/extensions/catalog.rb', line 50

def running?(lex_name)
  state(lex_name) == :running
end

.state(lex_name) ⇒ Object



35
36
37
# File 'lib/legion/extensions/catalog.rb', line 35

def state(lex_name)
  entries.dig(lex_name, :state)
end

.transition(lex_name, new_state) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/legion/extensions/catalog.rb', line 24

def transition(lex_name, new_state)
  return unless entries.key?(lex_name)

  entries[lex_name][:state] = new_state
  entries[lex_name][:started_at] = Time.now if new_state == :running
  entries[lex_name][:stopped_at] = Time.now if new_state == :stopped

  publish_transition(lex_name, new_state)
  persist_transition(lex_name, new_state)
end