Module: RubyZmqFramework::FrameworkModule

Included in:
CanBridge, StateRegistry
Defined in:
lib/ruby_zmq_framework/framework.rb

Defined Under Namespace

Modules: Heartbeat

Constant Summary collapse

HEARTBEAT_INTERVAL =

seconds

5

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



38
39
40
41
42
# File 'lib/ruby_zmq_framework/framework.rb', line 38

def self.included(base)
  base.include(StrictContract)
  base.requires_methods(:handle_message)
  base.prepend(Heartbeat)
end

Instance Method Details

#broadcast(topic, payload = {}) ⇒ Object



53
54
55
# File 'lib/ruby_zmq_framework/framework.rb', line 53

def broadcast(topic, payload = {})
  @bus.publish(topic, payload)
end

#node_nameObject

Identity used in heartbeats. Precedence: @node_name set by the node itself, then NODE_NAME from the environment (how bin/flowctl names the processes it spawns), then the class name. Distinct names matter when several instances of one class run — otherwise they overwrite each other in any StateRegistry's active_nodes.



62
63
64
# File 'lib/ruby_zmq_framework/framework.rb', line 62

def node_name
  @node_name || ENV['NODE_NAME'] || self.class.name || 'AnonymousNode'
end

#stop_heartbeatObject

Gracefully stops the heartbeat thread. Call this before closing the bus the node broadcasts on. Wakes the thread out of its interval wait rather than killing it, so an in-flight broadcast always completes.



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ruby_zmq_framework/framework.rb', line 69

def stop_heartbeat
  thread = @heartbeat_thread
  return unless thread

  @heartbeat_mutex.synchronize do
    @heartbeat_running = false
    @heartbeat_wakeup.signal
  end
  thread.join(HEARTBEAT_INTERVAL + 1)
  @heartbeat_thread = nil
end