Class: Reacter::Core

Inherits:
EM::Connection
  • Object
show all
Defined in:
lib/reacter/core.rb

Instance Method Summary collapse

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_adaptersObject



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_agentsObject



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

#runObject



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 |messages|
    messages.each do |message|
      rv = message

    # 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 |messages|
        dispatch.call(messages)
      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

#stopObject



100
101
102
# File 'lib/reacter/core.rb', line 100

def stop()
  EM.stop_event_loop()
end