Class: Reacter::Core
- Inherits:
-
EM::Connection
- Object
- EM::Connection
- Reacter::Core
- Defined in:
- lib/reacter/core.rb
Instance Method Summary collapse
-
#initialize(*args) ⇒ Core
constructor
A new instance of Core.
- #load_adapters ⇒ Object
- #load_agents ⇒ Object
- #run ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(*args) ⇒ Core
Returns a new instance of Core.
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/reacter/core.rb', line 12 def initialize(*args) super @_dispatch_queue = EM::Queue.new @_adapters = [] @_agents = [] Reacter.load_config(args.first || {}) load_adapters() load_agents() end |
Instance Method Details
#load_adapters ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/reacter/core.rb', line 25 def load_adapters() adapter_config = Reacter.get('global.adapter', nil) if adapter_config adapter_config = [adapter_config] if adapter_config.is_a?(Hash) adapter_config.each do |adapter| type = adapter.get('type') if (instance = Adapter.create(type, adapter)) instance.connect() @_adapters << instance else raise "Adapter '#{type}' not found, exiting" end end else Util.fatal("No adapters specified, exiting") exit 1 end end |
#load_agents ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/reacter/core.rb', line 47 def load_agents() Reacter.get('global.agents.enabled', []).each do |agent| agent = Agent.create(agent) @_agents << agent if agent end if @_agents.empty? Util.fatal("No agents enabled, exiting") exit 1 end end |
#run ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/reacter/core.rb', line 59 def run() @_adapters.each do |adapter| Util.info("Start listening using #{adapter.type} adapter...") end # agent message dispatch subprocess dispatch = proc do || .each do || rv = # send this message to all agents @_agents.each do |agent| next if rv === false rv = agent.received(rv) end end end # enter polling loop @_adapters.each do |adapter| next unless adapter.enabled? poller = proc do adapter.poll do || dispatch.call() end end EM.defer(poller) end EM.add_periodic_timer(1) do # exit if all adapters are disabled if @_adapters.select{|i| i.enabled? }.empty? Util.info("All adapters disabled, exiting") stop() end end end |
#stop ⇒ Object
100 101 102 |
# File 'lib/reacter/core.rb', line 100 def stop() EM.stop_event_loop() end |