Class: Smith::AgentCache

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/smith/agent_cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ AgentCache

Returns a new instance of AgentCache.



12
13
14
15
# File 'lib/smith/agent_cache.rb', line 12

def initialize(opts={})
  # @db = LevelDB::DB.make(Smith.cache_path.join('agent_state').to_s, :error_if_exists => false, :create_if_missing => true)
  @db = TDB.new(Smith.cache_path.join('agent_state.tdb').to_s)
end

Instance Attribute Details

#path ⇒ Object

Returns the value of attribute path.



10
11
12
# File 'lib/smith/agent_cache.rb', line 10

def path
  @path
end

Instance Method Details

#alive?(uuid) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/smith/agent_cache.rb', line 21

def alive?(uuid)
  (@db.include?(uuid)) ? instantiate(@db[uuid]).alive? : false
end

#create(name) ⇒ Object



17
18
19
# File 'lib/smith/agent_cache.rb', line 17

def create(name)
  AgentProcess.new(@db, :name => name, :uuid => SecureRandom.uuid)
end

#delete(uuid) ⇒ Object



46
47
48
# File 'lib/smith/agent_cache.rb', line 46

def delete(uuid)
  @db.delete(uuid)
end

#each(&blk) ⇒ Object



56
57
58
# File 'lib/smith/agent_cache.rb', line 56

def each(&blk)
  @db.each {|k,v| blk.call(instantiate(v)) }
end

#entry(uuid) ⇒ Object Also known as: []



50
51
52
# File 'lib/smith/agent_cache.rb', line 50

def entry(uuid)
  (uuid) ? instantiate(@db[uuid]) : nil
end

#exist?(uuid) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/smith/agent_cache.rb', line 25

def exist?(uuid)
  @db.include?(uuid)
end

#find_by_name(*names) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/smith/agent_cache.rb', line 29

def find_by_name(*names)
  inject([]) do |a, agent|
    a.tap do |acc|
      names.flatten.each do |name|
        acc << agent if name == agent.name
      end
    end
  end
end

#state(state) ⇒ Object

end



42
43
44
# File 'lib/smith/agent_cache.rb', line 42

def state(state)
  select {|a| a.state == state.to_s }
end