Class: Smith::Agency

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/smith/application/agency.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

included

Constructor Details

#initialize(opts = {}) ⇒ Agency

Returns a new instance of Agency.



16
17
18
# File 'lib/smith/application/agency.rb', line 16

def initialize(opts={})
  @agent_processes = AgentCache.new
end

Instance Attribute Details

#agent_processesObject (readonly)

Returns the value of attribute agent_processes.



14
15
16
# File 'lib/smith/application/agency.rb', line 14

def agent_processes
  @agent_processes
end

#agentsObject (readonly)

Returns the value of attribute agents.



14
15
16
# File 'lib/smith/application/agency.rb', line 14

def agents
  @agents
end

Instance Method Details

#setup_queuesObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/smith/application/agency.rb', line 20

def setup_queues
  Messaging::Receiver.new(QueueDefinitions::Agency_control, :auto_ack => false) do |receiver|
    receiver.subscribe do |payload, responder|

      completion = EM::Completion.new.tap do |c|
        c.completion do |value|
          responder.ack
          responder.reply(Smith::ACL::AgencyCommandResponse.new(:response => value))
        end
      end

      begin
        Command.run(payload.command, payload.args, :agency => self,  :agents => @agent_processes, :responder => completion)
      rescue Command::UnknownCommandError => e
        responder.reply("Unknown command: #{payload.command}")
      end
    end
  end

  Messaging::Receiver.new(QueueDefinitions::Agent_lifecycle) do |receiver|
    receiver.subscribe do |payload, r|
      case payload
      when Smith::ACL::AgentDead
        dead(payload)
      when Smith::ACL::AgentAcknowledgeStart
        acknowledge_start(payload)
      when Smith::ACL::AgentAcknowledgeStop
        acknowledge_stop(payload)
      else
        logger.warn { "Unknown command received on #{QueueDefinitions::Agent_lifecycle.name} queue: #{payload.state}" }
      end
    end
  end

  Messaging::Receiver.new(QueueDefinitions::Agent_keepalive) do |receiver|
    receiver.subscribe do |payload, r|
      keep_alive(payload)
    end
  end
end

#start_monitoringObject



61
62
63
64
# File 'lib/smith/application/agency.rb', line 61

def start_monitoring
  # @agent_monitor = AgentMonitoring.new(@agent_processes)
  # @agent_monitor.start_monitoring
end

#stop(&blk) ⇒ Object

Stop the agency. This will wait for one second to ensure that any messages are flushed.



68
69
70
71
72
73
74
# File 'lib/smith/application/agency.rb', line 68

def stop(&blk)
  if blk
    Smith.stop(true, &blk)
  else
    Smith.stop(true)
  end
end