Class: Nanite::State

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/nanite/state.rb

Instance Method Summary collapse

Constructor Details

#initialize(redis, tag_store = nil) ⇒ State

This class encapsulates the state of a nanite system using redis as the data store and a provided tag store. For a nanite with the identity ‘nanite-foobar’ we store the following:

nanite-foobar: 0.72 # load average or ‘status’ t-nanite-foobar: 123456789 # unix timestamp of the last state update

The tag store is used to store the associated services and tags.

A tag store should provide the following methods:

- initialize(redis): Initialize tag store, may use provided redis handle
- services(nanite): Retrieve services implemented by given agent
- tags(nanite): Retrieve tags implemented by given agent
- all_services: Retrieve all services implemented by all agents
- all_tags: Retrieve all tags exposed by all agents
- store(nanite, services, tags): Store agent's services and tags
- update(name, new_tags,obsolete_tags): Update agent's tags
- delete(nanite): Delete all entries associated with given agent
- nanites_for(service, tags): Retrieve agents implementing given service
                              and exposing given tags

The default implementation for the tag store reuses Redis.



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

def initialize(redis, tag_store=nil)
  host, port, tag_store_type = redis.split(':')
  host ||= '127.0.0.1'
  port ||= '6379'
  tag_store||= 'Nanite::RedisTagStore'
  @redis = Redis.new(:host => host, :port => port)
  @tag_store = tag_store.to_const.new(@redis)
  Nanite::Log.info("[setup] Initializing redis state using host '#{host}', port '#{port}' and tag store #{tag_store}")
end

Instance Method Details

#[](nanite) ⇒ Object

Retrieve given agent services, tags, status and timestamp



42
43
44
45
46
47
48
49
50
51
# File 'lib/nanite/state.rb', line 42

def [](nanite)
  log_redis_error do
    status    = @redis[nanite]
    timestamp = @redis["t-#{nanite}"]
    services  = @tag_store.services(nanite)
    tags      = @tag_store.tags(nanite)
    return nil unless status && timestamp && services
    {:services => services, :status => status, :timestamp => timestamp.to_i, :tags => tags}
  end
end

#[]=(nanite, attributes) ⇒ Object

Set given attributes for given agent Attributes may include services, tags and status



55
56
57
58
# File 'lib/nanite/state.rb', line 55

def []=(nanite, attributes)
  @tag_store.store(nanite, attributes[:services], attributes[:tags])
  update_status(nanite, attributes[:status])
end

#delete(nanite) ⇒ Object

Delete all information related to given agent



61
62
63
64
65
66
67
# File 'lib/nanite/state.rb', line 61

def delete(nanite)
  @tag_store.delete(nanite)
  log_redis_error do
    @redis.delete(nanite)
    @redis.delete("t-#{nanite}")
  end
end

#eachObject

Iterate through all agents, yielding services, tags status and timestamp keyed by agent name



96
97
98
99
100
# File 'lib/nanite/state.rb', line 96

def each
  list_nanites.each do |nan|
    yield nan, self[nan]
  end
end

#list_nanitesObject

Return all registered agents



83
84
85
86
87
# File 'lib/nanite/state.rb', line 83

def list_nanites
  log_redis_error do
    @redis.keys("nanite-*")
  end
end

#nanites_for(request) ⇒ Object

Return agents that implement given service and expose all given tags



104
105
106
107
108
109
110
111
112
# File 'lib/nanite/state.rb', line 104

def nanites_for(request)
  res = {}
  @tag_store.nanites_ids_for(request).each do |nanite_id|
    if nanite = self[nanite_id]
      res[nanite_id] = nanite
    end
  end
  res
end

#sizeObject

Number of registered agents



90
91
92
# File 'lib/nanite/state.rb', line 90

def size
  list_nanites.size
end

#update_status(name, status) ⇒ Object

Update status and timestamp for given agent



70
71
72
73
74
75
# File 'lib/nanite/state.rb', line 70

def update_status(name, status)
  log_redis_error do
    @redis[name] = status
    @redis["t-#{name}"] = Time.now.utc.to_i
  end
end

#update_tags(name, new_tags, obsolete_tags) ⇒ Object

Update tags for given agent



78
79
80
# File 'lib/nanite/state.rb', line 78

def update_tags(name, new_tags, obsolete_tags)
  @tag_store.update(name, new_tags, obsolete_tags)
end