aboutsummaryrefslogtreecommitdiff
path: root/lib/longboat/raiders.rb
blob: 103cac5a41c3b262c9957004e849634d81c544ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
module Longboat
  class Raiders
    def initialize(collector, config)
      @collector = collector
      @raiders = []
      @config = config

      @config[:raiders_path].each do |dir|
        next unless Dir.exist?(dir)

        Dir.entries(dir).each do |file|
          next if file =~ /^\./

          reqname = File.basename(file, ".rb")
          cname = reqname.split('_').map(&:capitalize).join

          require "raiders/#{reqname}"
          @raiders << Kernel.const_get(cname).new(@collector, raider_config)
        end
      end
    end

    def raid!
      @raiders.each(&:raid)
    end

    def raid_every(time = @config[:raid_every], async = true)
      if async
        Thread.new do
          loop do
            raid!
            sleep(time)
          end
        end
      else
        loop do
          raid!
          sleep(time)
        end
      end
    end

    private

    def raider_config
      @config.slice(:raid_every, :metric_prefix)
    end
  end
end