Class: Nanite::Cluster
Instance Attribute Summary collapse
-
#agent_timeout ⇒ Object
readonly
Returns the value of attribute agent_timeout.
-
#amq ⇒ Object
readonly
Returns the value of attribute amq.
-
#identity ⇒ Object
readonly
Returns the value of attribute identity.
-
#log ⇒ Object
readonly
Returns the value of attribute log.
-
#nanites ⇒ Object
readonly
Returns the value of attribute nanites.
-
#reaper ⇒ Object
readonly
Returns the value of attribute reaper.
-
#serializer ⇒ Object
readonly
Returns the value of attribute serializer.
Instance Method Summary collapse
-
#initialize(amq, agent_timeout, identity, log, serializer) ⇒ Cluster
constructor
A new instance of Cluster.
- #publish(request, target) ⇒ Object
-
#register(reg) ⇒ Object
adds nanite to nanites map: key is nanite’s identity and value is a services/status pair implemented as a hash.
- #route(request, targets) ⇒ Object
-
#targets_for(request) ⇒ Object
determine which nanites should receive the given request.
Constructor Details
#initialize(amq, agent_timeout, identity, log, serializer) ⇒ Cluster
Returns a new instance of Cluster.
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/nanite/cluster.rb', line 5 def initialize(amq, agent_timeout, identity, log, serializer) @amq = amq @agent_timeout = agent_timeout @identity = identity @serializer = serializer @nanites = {} @reaper = Reaper.new(agent_timeout) @log = log setup_queues end |
Instance Attribute Details
#agent_timeout ⇒ Object (readonly)
Returns the value of attribute agent_timeout.
3 4 5 |
# File 'lib/nanite/cluster.rb', line 3 def agent_timeout @agent_timeout end |
#amq ⇒ Object (readonly)
Returns the value of attribute amq.
3 4 5 |
# File 'lib/nanite/cluster.rb', line 3 def amq @amq end |
#identity ⇒ Object (readonly)
Returns the value of attribute identity.
3 4 5 |
# File 'lib/nanite/cluster.rb', line 3 def identity @identity end |
#log ⇒ Object (readonly)
Returns the value of attribute log.
3 4 5 |
# File 'lib/nanite/cluster.rb', line 3 def log @log end |
#nanites ⇒ Object (readonly)
Returns the value of attribute nanites.
3 4 5 |
# File 'lib/nanite/cluster.rb', line 3 def nanites @nanites end |
#reaper ⇒ Object (readonly)
Returns the value of attribute reaper.
3 4 5 |
# File 'lib/nanite/cluster.rb', line 3 def reaper @reaper end |
#serializer ⇒ Object (readonly)
Returns the value of attribute serializer.
3 4 5 |
# File 'lib/nanite/cluster.rb', line 3 def serializer @serializer end |
Instance Method Details
#publish(request, target) ⇒ Object
35 36 37 |
# File 'lib/nanite/cluster.rb', line 35 def publish(request, target) amq.queue(target).publish(serializer.dump(request), :persistent => request.persistent) end |
#register(reg) ⇒ Object
adds nanite to nanites map: key is nanite’s identity and value is a services/status pair implemented as a hash
25 26 27 28 29 |
# File 'lib/nanite/cluster.rb', line 25 def register(reg) nanites[reg.identity] = { :services => reg.services, :status => reg.status } reaper.timeout(reg.identity, agent_timeout + 1) { nanites.delete(reg.identity) } log.info("registered: #{reg.identity}, #{nanites[reg.identity]}") end |
#route(request, targets) ⇒ Object
31 32 33 |
# File 'lib/nanite/cluster.rb', line 31 def route(request, targets) EM.next_tick { targets.map { |target| publish(request, target) } } end |
#targets_for(request) ⇒ Object
determine which nanites should receive the given request
17 18 19 20 |
# File 'lib/nanite/cluster.rb', line 17 def targets_for(request) return [request.target] if request.target __send__(request.selector, request.type).collect {|name, state| name } end |