Sha256: 88f64f7a0a50bf7909366399dd1295e8b592983622dde1ab0d42d70373213a40

Contents?: true

Size: 1.24 KB

Versions: 4

Compression:

Stored size: 1.24 KB

Contents

module Harvestdor
  ##
  # Harvest metrics tracker
  class Indexer::Metrics
    attr_accessor :error_count, :success_count, :logger

    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

    ##
    # 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.
    #
    # @param [Hash] options
    # @option options [#call] Callback that will receive any exception thrown by the block
    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

    ##
    # Record a successful run
    def success!
      @success_count += 1
    end

    ##
    # Record an error
    def error!
      @error_count += 1
    end

    ##
    # Total number of runs
    def total
      @success_count + @error_count
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
harvestdor-indexer-2.5.0 lib/harvestdor/indexer/metrics.rb
harvestdor-indexer-2.4.0 lib/harvestdor/indexer/metrics.rb
harvestdor-indexer-2.3.3 lib/harvestdor/indexer/metrics.rb
harvestdor-indexer-2.3.2 lib/harvestdor/indexer/metrics.rb