Method: Nanite::State#initialize

Defined in:
lib/nanite/state.rb

#initialize(redis) ⇒ State

this class encapsulates the state of a nanite system using redis as the data store. here is the schema, for each agent we store a number of items, for a nanite with the identity: nanite-foobar we store the following things:

nanite-foobar: 0.72 # load average or ‘status’ s-nanite-foobar: { /foo/bar, /foo/nik } # a SET of the provided services tg-nanite-foobar: { foo-42, customer-12 } # a SET of the tags for this agent t-nanite-foobar: 123456789 # unix timestamp of the last state update

also we do an inverted index for quick lookup of agents providing a certain service, so for each service the agent provides, we add the nanite to a SET of all the nanites that provide said service:

foo/bar: { nanite-foobar, nanite-nickelbag, nanite-another } # redis SET

we do that same thing for tags: some-tag: { nanite-foobar, nanite-nickelbag, nanite-another } # redis SET

This way we can do a lookup of what nanites provide a set of services and tags based on redis SET intersection:

nanites_for(‘/gems/list’, ‘some-tag’)

> returns a nested array of nanites and their state that provide the intersection

of these two service tags



32
33
34
35
36
37
38
# File 'lib/nanite/state.rb', line 32

def initialize(redis)
  Nanite::Log.info("[setup] initializing redis state: #{redis}")
  host, port = redis.split(':')
  host ||= '127.0.0.1'
  port ||= '6379'
  @redis = Redis.new :host => host, :port => port
end