Class: GRI::Scheduler

Inherits:
Object show all
Defined in:
lib/gri/scheduler.rb

Direct Known Subclasses

UScheduler

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(loop, metrics) ⇒ Scheduler

Returns a new instance of Scheduler.



5
6
7
8
9
10
# File 'lib/gri/scheduler.rb', line 5

def initialize loop, metrics
  @loop = loop
  @metrics = metrics
  @loop.on_detach {process_queue}
  @writers = []
end

Instance Attribute Details

#fake_descr_hashObject

Returns the value of attribute fake_descr_hash.



3
4
5
# File 'lib/gri/scheduler.rb', line 3

def fake_descr_hash
  @fake_descr_hash
end

#queueObject

Returns the value of attribute queue.



3
4
5
# File 'lib/gri/scheduler.rb', line 3

def queue
  @queue
end

#writersObject

Returns the value of attribute writers.



3
4
5
# File 'lib/gri/scheduler.rb', line 3

def writers
  @writers
end

Instance Method Details

#finalizeObject



58
59
60
# File 'lib/gri/scheduler.rb', line 58

def finalize
  @writers.each {|w| w.finalize if w.respond_to? :finalize}
end

#process1(col_type, host, options) ⇒ Object



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
# File 'lib/gri/scheduler.rb', line 26

def process1 col_type, host, options
  return if Config['nop']
  return if col_type == 'fluentd'
  collector = Collector.create(col_type, host, options,
                               @fake_descr_hash) {|records|
    for writer in @writers
      puts "  writer #{writer.class}" if $debug
      writer.write records
    end
    @metrics[:record_count] += records.size
  }
  if collector
    #puts "#{collector.class} (#{col_type}): #{host}" if $debug
    interval = (options['interval'] || 300).to_i
    collector.interval = interval
    timeout = (options['timeout'] || Config['timeout'] || 90).to_i
    collector.timeout = [timeout, interval].min
    collector.on_error {@metrics[:error_count] += 1}
    collector.on_retry {@metrics[:retry_count] += 1}
    Log.info "[#{$$}] #{host}: collect #{col_type}"
    begin
      @loop.run if collector.sync?
      @loop.attach collector
    rescue SystemCallError
      Log.error "#{host}: ERROR: #{$!}"
      puts "#{host}: ERROR: #{$!}" if $debug
      @loop.detach collector
    end
    @metrics[:run_count] += 1
  end
end

#process_queueObject



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/gri/scheduler.rb', line 12

def process_queue
  while @loop.collectors.size < 5
    host, options = queue.shift
    break unless host
    next if host =~ /^GRIMETRICS/

    ts = options['type']
    col_types = ts ? ts.split(',') : ['snmp']
    for col_type in col_types
      process1 col_type, host, options
    end
  end
end