Class: Powcloud::Insurance::AgentContainer

Inherits:
Object
  • Object
show all
Includes:
HttpService, Logger
Defined in:
lib/powcloud/insurance/agent_container.rb,
lib/powcloud/insurance/agent_container_http_service.rb

Instance Attribute Summary collapse

Attributes included from Logger

#logger

Instance Method Summary collapse

Methods included from HttpService

#create_http_service, included

Methods included from Logger

#init_child_logger, #init_logger, #log_exception, outputters, outputters=

Constructor Details

#initialize(agents, opts = {}) ⇒ AgentContainer

Returns a new instance of AgentContainer.



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/powcloud/insurance/agent_container.rb', line 20

def initialize(agents, opts = {})
  @options = {:host => '0.0.0.0', :port => 4567, :log_level => 'WARN'}.merge(opts)

  log_level = Log4r::LNAMES.include?(opts[:log_level]) ? Log4r::LNAMES.index(opts[:log_level]) : Log4r::WARN
  Log4r::Logger.root.level = log_level
  init_logger log_level

  @agents = ActiveSupport::OrderedHash.new
  @rack_map = {}

  load_agents agents
end

Instance Attribute Details

#rack_mapObject (readonly)

Returns the value of attribute rack_map.



18
19
20
# File 'lib/powcloud/insurance/agent_container.rb', line 18

def rack_map
  @rack_map
end

Instance Method Details

#load_agents(agents) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/powcloud/insurance/agent_container.rb', line 33

def load_agents(agents)
  agents.each do |agent_name|
    next if @agents.include? agent_name
    
    begin
      require agent_name
      agent_class = agent_name.classify.constantize
      logger.warn "Loaded #{agent_class.to_s}"
      @agents[agent_name] = agent = agent_class.new
      agent.init_logger(logger.level, agent_name) if agent.respond_to? :init_logger
    rescue Exception => ex
      log_exception ex
      exit 1 # Hard exit
    end
  end
end

#run!Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/powcloud/insurance/agent_container.rb', line 51

def run!
  EM.run do

    begin

      @agents.each_pair do |agent_name, agent|
        logger.warn "Starting aggent #{agent_name}" if logger
        agent.start_no_block # Must be non-blocking
      end

      rack_up

      make_traps
    rescue Exception => ex
      log_exception ex
      stop_and_exit 1
    end
  end
end