Class: BackgroundWorker::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/background_worker/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ Config

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) }

)



18
19
20
21
22
23
# File 'lib/background_worker/config.rb', line 18

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))
  @backgrounded = attrs.fetch(:backgrounded, true)
end

Instance Attribute Details

#backgroundedObject (readonly)

Returns the value of attribute backgrounded.



5
6
7
# File 'lib/background_worker/config.rb', line 5

def backgrounded
  @backgrounded
end

#enqueue_withObject (readonly)

Returns the value of attribute enqueue_with.



5
6
7
# File 'lib/background_worker/config.rb', line 5

def enqueue_with
  @enqueue_with
end

#loggerObject (readonly)

Returns the value of attribute logger.



5
6
7
# File 'lib/background_worker/config.rb', line 5

def logger
  @logger
end

Instance Method Details

#after_exception(e) ⇒ Object

Callback fired when an exception occurs



26
27
28
# File 'lib/background_worker/config.rb', line 26

def after_exception(e)
  @after_exception.call(e)
end

#default_after_exception(e) ⇒ Object



34
35
36
37
38
# File 'lib/background_worker/config.rb', line 34

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

#foreground_enqueue(klass, opts) ⇒ Object



30
31
32
# File 'lib/background_worker/config.rb', line 30

def foreground_enqueue(klass, opts)
  klass.perform_now(opts)
end