Class: Harvestdor::Indexer::Metrics

Inherits:
Object
  • Object
show all
Defined in:
lib/harvestdor/indexer/metrics.rb

Overview

Harvest metrics tracker

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Metrics

Returns a new instance of Metrics.



7
8
9
10
11
# File 'lib/harvestdor/indexer/metrics.rb', line 7

def initialize(options = {})
  @success_count = 0    # the number of objects successfully indexed
  @error_count = 0      # the number of objects that failed
  @logger = options[:logger] || Logger.new(STDERR)
end

Instance Attribute Details

#error_countObject

Returns the value of attribute error_count.



5
6
7
# File 'lib/harvestdor/indexer/metrics.rb', line 5

def error_count
  @error_count
end

#loggerObject

Returns the value of attribute logger.



5
6
7
# File 'lib/harvestdor/indexer/metrics.rb', line 5

def logger
  @logger
end

#success_countObject

Returns the value of attribute success_count.



5
6
7
# File 'lib/harvestdor/indexer/metrics.rb', line 5

def success_count
  @success_count
end

Instance Method Details

#error!Object

Record an error



40
41
42
# File 'lib/harvestdor/indexer/metrics.rb', line 40

def error!
  @error_count += 1
end

#success!Object

Record a successful run



34
35
36
# File 'lib/harvestdor/indexer/metrics.rb', line 34

def success!
  @success_count += 1
end

#tally(options = {}) ⇒ Object

Wrap an operation in tally block; if the block completes without throwing an exception, tally a success. If the block throws an exception, catch it and tally a failure.

Callers can provide an :on_error handler to receive the exception and process it appropriately.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • Callback (#call)

    that will receive any exception thrown by the block



23
24
25
26
27
28
29
30
# File 'lib/harvestdor/indexer/metrics.rb', line 23

def tally(options = {})
  yield
  success!
rescue => e
  error!
  logger.error "Failed to process: #{e.message}"
  options[:on_error].call e if options[:on_error]
end

#totalObject

Total number of runs



46
47
48
# File 'lib/harvestdor/indexer/metrics.rb', line 46

def total
  @success_count + @error_count
end