class Dboard::Collector
Attributes
sources[R]
Public Class Methods
new()
click to toggle source
# File lib/collector.rb, line 26 def initialize @sources = {} @after_update_callback = lambda {} @error_callback = lambda { |exception| } end
register_after_update_callback(callback)
click to toggle source
# 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)
click to toggle source
# File lib/collector.rb, line 18 def self.register_error_callback(callback) Collector.instance.register_error_callback(callback) end
register_source(key, instance)
click to toggle source
# File lib/collector.rb, line 10 def self.register_source(key, instance) Collector.instance.register_source(key, instance) end
start()
click to toggle source
# File lib/collector.rb, line 22 def self.start instance.start end
Public Instance Methods
register_after_update_callback(callback)
click to toggle source
# File lib/collector.rb, line 49 def register_after_update_callback(callback) @after_update_callback = callback end
register_error_callback(callback)
click to toggle source
# File lib/collector.rb, line 53 def register_error_callback(callback) @error_callback = callback end
register_source(key, instance)
click to toggle source
# File lib/collector.rb, line 45 def register_source(key, instance) @sources.merge!({ key => instance }) end
start()
click to toggle source
# 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)
click to toggle source
Public because the old tests depend on it
# 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
Private Instance Methods
publish_data(source, data)
click to toggle source
# File lib/collector.rb, line 87 def publish_data(source, data) Publisher.publish(source, data) end
update_in_thread(source, instance)
click to toggle source
# File lib/collector.rb, line 73 def update_in_thread(source, instance) time = Time.now puts "#{source} updating..." update_source(source, instance) elapsed_time = Time.now - time time_until_next_update = instance.update_interval - elapsed_time time_until_next_update = 0 if time_until_next_update < 0 puts "#{source} done in #{elapsed_time} seconds, will update again in #{time_until_next_update} seconds (interval: #{instance.update_interval})." sleep time_until_next_update rescue Exception => ex puts "Something failed outside the update_source method. #{ex.message}" puts ex.backtrace end
wait_a_little_bit_to_not_start_all_fetches_at_once()
click to toggle source
# File lib/collector.rb, line 91 def wait_a_little_bit_to_not_start_all_fetches_at_once sleep 10 * rand end