Class: Dboard::Collector

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCollector

Returns a new instance of Collector.



26
27
28
29
30
# File 'lib/collector.rb', line 26

def initialize
  @sources = {}
  @after_update_callback = lambda {}
  @error_callback = lambda { |exception| }
end

Instance Attribute Details

#sourcesObject (readonly)

Returns the value of attribute sources.



8
9
10
# File 'lib/collector.rb', line 8

def sources
  @sources
end

Class Method Details

.register_after_update_callback(callback) ⇒ Object



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

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

.register_error_callback(callback) ⇒ Object



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

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

.register_source(key, instance) ⇒ Object



10
11
12
# File 'lib/collector.rb', line 10

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

.startObject



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

def self.start
  instance.start
end

Instance Method Details

#register_after_update_callback(callback) ⇒ Object



49
50
51
# File 'lib/collector.rb', line 49

def register_after_update_callback(callback)
  @after_update_callback = callback
end

#register_error_callback(callback) ⇒ Object



53
54
55
# File 'lib/collector.rb', line 53

def register_error_callback(callback)
  @error_callback = callback
end

#register_source(key, instance) ⇒ Object



45
46
47
# File 'lib/collector.rb', line 45

def register_source(key, instance)
  @sources.merge!({ key => instance })
end

#startObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/collector.rb', line 32

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

      loop do
        update_in_thread(source, instance)
      end
    end
  end
  loop { sleep 1 }
end

#update_source(source, instance) ⇒ Object

Public because the old tests depend on it



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/collector.rb', line 58

def update_source(source, instance)
  begin
    data = instance.fetch
    publish_data(source, data)
  ensure
    @after_update_callback.call
  end
rescue Exception => ex
  puts "Failed to update #{source}: #{ex.message}"
  puts ex.backtrace
  @error_callback.call(ex)
end