Class: Moleculer::Node
- Inherits:
-
Object
- Object
- Moleculer::Node
- Defined in:
- lib/moleculer/node.rb
Overview
Nodes are a representation of communicating apps within the same event bus. A node is something that emits/listens to events within the bus and communicates accordingly.
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#services ⇒ Object
readonly
Returns the value of attribute services.
Class Method Summary collapse
Instance Method Summary collapse
-
#actions ⇒ Hash
TODO: refactor this into a list object.
-
#beat ⇒ Object
Updates the last heartbeat to Time#now.
-
#events ⇒ Hash
TODO: refactor this into a list object.
-
#initialize(options = {}) ⇒ Node
constructor
A new instance of Node.
-
#last_heartbeat_at ⇒ Time
The time of the last heartbeat, or Time#now if the node is local.
- #local? ⇒ Boolean
- #register_service(service) ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Node
Returns a new instance of Node.
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/moleculer/node.rb', line 23 def initialize( = {}) @id = .fetch(:node_id) @local = .fetch(:local, false) @hostname = .fetch(:hostname, Socket.gethostname) svcs = .fetch(:services) # TODO: move this up to from_remote_info svcs.map! { |service| Service.from_remote_info(service, self) } if svcs.first.is_a? Hash @services = Hash[svcs.map { |s| [s.full_name, s] }] end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
20 21 22 |
# File 'lib/moleculer/node.rb', line 20 def id @id end |
#services ⇒ Object (readonly)
Returns the value of attribute services.
20 21 22 |
# File 'lib/moleculer/node.rb', line 20 def services @services end |
Class Method Details
.from_remote_info(info_packet) ⇒ Object
12 13 14 15 16 17 |
# File 'lib/moleculer/node.rb', line 12 def from_remote_info(info_packet) new( services: info_packet.services, node_id: info_packet.sender, ) end |
Instance Method Details
#actions ⇒ Hash
TODO: refactor this into a list object
41 42 43 44 45 46 47 48 |
# File 'lib/moleculer/node.rb', line 41 def actions unless @actions @actions = {} @services.each_value { |s| s.actions.each { |key, value| @actions[key] = value } } end @actions end |
#beat ⇒ Object
Updates the last heartbeat to Time#now
76 77 78 |
# File 'lib/moleculer/node.rb', line 76 def beat @last_heartbeat_at = Time.now end |
#events ⇒ Hash
TODO: refactor this into a list object
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/moleculer/node.rb', line 53 def events unless @events @events = {} @services.each_value do |s| s.events.each do |key, value| @events[key] ||= [] @events[key] << value end end end @events end |
#last_heartbeat_at ⇒ Time
Returns the time of the last heartbeat, or Time#now if the node is local.
68 69 70 71 72 |
# File 'lib/moleculer/node.rb', line 68 def last_heartbeat_at return Time.now if local? @last_heartbeat_at || Time.now end |
#local? ⇒ Boolean
80 81 82 |
# File 'lib/moleculer/node.rb', line 80 def local? @local end |
#register_service(service) ⇒ Object
34 35 36 |
# File 'lib/moleculer/node.rb', line 34 def register_service(service) @services[service.name] = service end |
#to_h ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/moleculer/node.rb', line 84 def to_h { sender: @id, config: {}, seq: 1, ipList: [], hostname: @hostname, services: @services.values.map(&:to_h), client: client_attrubutes, } end |