aboutsummaryrefslogtreecommitdiff
path: root/lib/longboat/raiders.rb
blob: e27ac0b6c182709c5b8de5d871bf72e7e048851f (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
50
51
52
53
54
55
56
57
58
59
60
61
62
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[reqname] = Kernel.const_get(cname).new(@collector, raider_config)
        end
      end
    end

    def raid!
      @raiders.each do |name, raider|
        start_time = Time.now
        raider.raid
        end_time = Time.now
        time_taken = end_time - start_time

        @collector.report!(
          "longboat_meta_raider_runtime",
          (time_taken.to_f * 1000).to_i,
          help: "Time taken by a raider whilst raiding in ms",
          type: "guage",
          labels: {raider: name}
        )
      end
    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