Sha256: 9ebc5607924bc3b4defce2805213482b6330c86d2deea3bbde04c838cc5516aa

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

require 'logger'

module BackgroundWorker
  class Config
    attr_reader :logger

    # Configuration includes following options:
    #   logger: what logger to user throughout
    #   enqueue_with: callback to execute the process
    #   after_exception: callback to handle exceptions (for example, error reporting service)
    #
    # eg:
    # Config.new(
    #   logger: Rails.logger,
    #   enqueue_with: -> klass, method_name, opts { Resque.enqueue(klass, method_name, opts) },
    #   after_exception: -> e { Airbrake.notify(e) }
    # )
    def initialize(attrs)
      @logger = attrs.fetch(:logger, ::Logger.new(STDOUT))
      @enqueue_with = attrs.fetch(:enqueue_with, method(:foreground_enqueue))
      @after_exception = attrs.fetch(:after_exception, method(:default_after_exception))
    end

    # Callback fired when an exception occurs
    def after_exception(e)
      @after_exception.call(e)
    end

    def foreground_enqueue(klass, method_name, opts)
      klass.perform(method_name, opts)
    end

    def default_after_exception(e)
      logger.error '** No after_exception handler installed **'
      logger.error "Exception: #{e}"
      logger.error "#{e.backtrace.join("\n")}"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
background_worker-0.0.4 lib/background_worker/config.rb