Class: Brigade::Monitor::Monitor

Inherits:
Object
  • Object
show all
Defined in:
lib/brigade/monitor/monitor.rb

Instance Method Summary collapse

Constructor Details

#initialize(key, miners, logger) ⇒ Monitor

Returns a new instance of Monitor.



8
9
10
11
12
13
14
15
# File 'lib/brigade/monitor/monitor.rb', line 8

def initialize(key, miners, logger)
  @key = key
  @miners = miners.map do |m|
    fetcher = Brigade::Monitor::Fetcher.new(m[:client], m[:name], logger)
    { fetcher: fetcher }.merge(m)
  end
  @log = logger
end

Instance Method Details

#runObject



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
63
64
65
66
67
# File 'lib/brigade/monitor/monitor.rb', line 17

def run
  api = Brigade::Monitor::API.new(@key, @log)

  @log.info("Monitoring #{@miners.length} miners")
  loop do
    updates = []

    @miners.each do |miner|
      @log.debug("Beginning miner: #{miner}")

      begin
        updates << miner[:fetcher].get_update
      rescue Timeout::Error => e
        @log.warn("Timeout::Error building update for #{miner[:name]} (#{e})")
        # XXX put something in the update to indicate it barfed
      rescue SystemCallError => e
        @log.error("SystemCallError building update for #{miner[:name]} (#{e})")
        # XXX put something in the update to indicate it barfed
      rescue Exception => e
        @log.error("Exception building update for #{miner[:name]} (#{e})")
        # XXX put something in the update to indicate it barfed
      end
    end

    if updates.empty?
      @log.info("Updates empty, not submitting")
    else
      @log.info("Updates available (#{updates.length}), submitting")
      begin
        tries ||= 3
        response = api.hosts(updates.to_json)
      rescue Exception => e
        @log.error("Exception submitting updates (#{e})")
        unless (tries -= 1).zero?
          @log.error('Retrying...')
          retry
        else
          @log.error('Giving up for this update...')
        end
      else
        if 401 == response.code
          @log.error('Unauthorized response submitting updates, check API key!')
          return
        end
        @log.info("Submitted updates (status: #{response.code})")
      end
    end

    sleep 60
  end
end