Class: RubyZmqFramework::StateRegistry

Inherits:
Object
  • Object
show all
Includes:
FrameworkModule
Defined in:
lib/ruby_zmq_framework/state_registry.rb

Overview

A passive, in-memory cache of cluster-wide state. It listens to heartbeats and telemetry broadcast by other FrameworkModule nodes and, on request, replays its current snapshot back onto the bus. It never makes a blocking call and never crashes when a peer goes quiet — a silent node simply stops getting its active_nodes timestamp updated.

Constant Summary

Constants included from FrameworkModule

FrameworkModule::HEARTBEAT_INTERVAL

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FrameworkModule

#broadcast, included, #node_name, #stop_heartbeat

Constructor Details

#initialize(bus) ⇒ StateRegistry

Returns a new instance of StateRegistry.



12
13
14
15
# File 'lib/ruby_zmq_framework/state_registry.rb', line 12

def initialize(bus)
  @bus = bus
  @store = { active_nodes: {}, telemetry: {} }
end

Instance Attribute Details

#storeObject (readonly)

Returns the value of attribute store.



10
11
12
# File 'lib/ruby_zmq_framework/state_registry.rb', line 10

def store
  @store
end

Instance Method Details

#handle_message(topic, payload) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ruby_zmq_framework/state_registry.rb', line 17

def handle_message(topic, payload)
  case topic
  when :heartbeat
    @store[:active_nodes][payload[:node_name]] = {
      status: payload[:status],
      timestamp: payload[:timestamp]
    }
  when :request_global_state
    broadcast(:global_state_snapshot, @store)
  else
    @store[:telemetry][topic] = payload
  end
end