Class: Nanite::Cluster

Inherits:
Object show all
Defined in:
lib/nanite/cluster.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_timeoutObject (readonly)

Returns the value of attribute agent_timeout.



3
4
5
# File 'lib/nanite/cluster.rb', line 3

def agent_timeout
  @agent_timeout
end

#amqObject (readonly)

Returns the value of attribute amq.



3
4
5
# File 'lib/nanite/cluster.rb', line 3

def amq
  @amq
end

#identityObject (readonly)

Returns the value of attribute identity.



3
4
5
# File 'lib/nanite/cluster.rb', line 3

def identity
  @identity
end

#logObject (readonly)

Returns the value of attribute log.



3
4
5
# File 'lib/nanite/cluster.rb', line 3

def log
  @log
end

#nanitesObject (readonly)

Returns the value of attribute nanites.



3
4
5
# File 'lib/nanite/cluster.rb', line 3

def nanites
  @nanites
end

#reaperObject (readonly)

Returns the value of attribute reaper.



3
4
5
# File 'lib/nanite/cluster.rb', line 3

def reaper
  @reaper
end

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