Class: Dboard::Collector

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/collector.rb

Constant Summary collapse

DEFAULT_MIN_INTERVAL =

seconds

30

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCollector

Returns a new instance of Collector.



37
38
39
40
41
42
43
44
45
46
# File 'lib/collector.rb', line 37

def initialize
  @sources = {}
  @after_update_callback = lambda {}
  @error_callback = lambda { |exception| }
  @mutex = Mutex.new
  @last_update_at = {}
  @active = {}
  @pending = {}
  @refresh_locks = {}
end

Instance Attribute Details

#sourcesObject (readonly)

Returns the value of attribute sources.



15
16
17
# File 'lib/collector.rb', line 15

def sources
  @sources
end

Class Method Details

.register_after_update_callback(callback) ⇒ Object



21
22
23
# File 'lib/collector.rb', line 21

def self.register_after_update_callback(callback)
  instance.register_after_update_callback(callback)
end

.register_error_callback(callback) ⇒ Object



25
26
27
# File 'lib/collector.rb', line 25

def self.register_error_callback(callback)
  instance.register_error_callback(callback)
end

.register_source(key, source_instance) ⇒ Object



17
18
19
# File 'lib/collector.rb', line 17

def self.register_source(key, source_instance)
  instance.register_source(key, source_instance)
end

.request_update(key, arg = nil) ⇒ Object



29
30
31
# File 'lib/collector.rb', line 29

def self.request_update(key, arg = nil)
  instance.request_update(key, arg)
end

.startObject



33
34
35
# File 'lib/collector.rb', line 33

def self.start
  instance.start
end

Instance Method Details

#register_after_update_callback(callback) ⇒ Object



67
68
69
# File 'lib/collector.rb', line 67

def register_after_update_callback(callback)
  @after_update_callback = callback
end

#register_error_callback(callback) ⇒ Object



71
72
73
# File 'lib/collector.rb', line 71

def register_error_callback(callback)
  @error_callback = callback
end

#register_source(key, instance) ⇒ Object



62
63
64
65
# File 'lib/collector.rb', line 62

def register_source(key, instance)
  @sources.merge!({ key => instance })
  @refresh_locks[key] = Mutex.new
end

#request_update(key, arg = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/collector.rb', line 75

def request_update(key, arg = nil)
  instance = @sources.fetch(key)
  floor = min_interval_for(instance)
  entry = arg.nil? ? FULL : arg
  leading = nil
  delay = @mutex.synchronize {
    now = monotonic_now
    (@pending[key] ||= []) << entry
    case decide_request(@active[key], @last_update_at[key], floor, now)
    when :coalesce
      return
    when :refresh_now
      @active[key] = true
      leading = @pending[key]
      @pending[key] = []
      0
    when :schedule
      @active[key] = true
      floor - (now - @last_update_at[key])
    end
  }
  spawn { run_worker(key, instance, floor, delay, leading) }
end

#startObject



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/collector.rb', line 48

def start
  @sources.each do |source, instance|
    Thread.new do
      wait_a_little_bit_to_not_start_all_fetches_at_once

      loop do
        request_update(source)
        sleep instance.update_interval
      end
    end
  end
  loop { sleep 1 }
end

#update_source(source, instance, batch = nil) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/collector.rb', line 99

def update_source(source, instance, batch = nil)
  begin
    data = fetch_source(instance, batch)
    publish_data(source, data)
  ensure
    @after_update_callback.call
  end
rescue Exception => ex
  Dboard.logger.error("Failed to update #{source}: #{ex.message}")
  Dboard.logger.error(ex.backtrace.join("\n"))
  @error_callback.call(ex)
end