Class: Moleculer::Node

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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(options = {})
  @id       = options.fetch(:node_id)
  @local    = options.fetch(:local, false)
  @hostname = options.fetch(:hostname, Socket.gethostname)

  svcs = options.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

#idObject (readonly)

Returns the value of attribute id.



20
21
22
# File 'lib/moleculer/node.rb', line 20

def id
  @id
end

#servicesObject (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

#actionsHash

TODO: refactor this into a list object

Returns:

  • (Hash)

    returns a key, value mapping of available actions on this node



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

#beatObject

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

#eventsHash

TODO: refactor this into a list object

Returns:

  • (Hash)

    returns a key value mapping of events on this node



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_atTime

Returns the time of the last heartbeat, or Time#now if the node is local.

Returns:

  • (Time)

    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

Returns:

  • (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_hObject



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