class BackgroundWorker::Base

Attributes

queue[R]
job_id[RW]
options[RW]

Public Class Methods

after_enqueue(*filters, &blk) click to toggle source
# File lib/background_worker/base.rb, line 75
def after_enqueue(*filters, &blk)
  set_callback(:enqueue, :after, *filters, &blk)
end
after_perform(*filters, &blk) click to toggle source
# File lib/background_worker/base.rb, line 67
def after_perform(*filters, &blk)
  set_callback(:perform, :after, *filters, &blk)
end
before_enqueue(*filters, &blk) click to toggle source
# File lib/background_worker/base.rb, line 71
def before_enqueue(*filters, &blk)
  set_callback(:enqueue, :before, *filters, &blk)
end
before_perform(*filters, &blk) click to toggle source
# File lib/background_worker/base.rb, line 63
def before_perform(*filters, &blk)
  set_callback(:perform, :before, *filters, &blk)
end
new(options = {}) click to toggle source
# File lib/background_worker/base.rb, line 16
def initialize(options = {})
  @options = options.symbolize_keys
  @job_id = @options[:job_id] || SecureRandom.uuid

  log("Created #{self.class}")
  log("Options are: #{@options.inspect}")
end
perform_later(options = {}) click to toggle source

Public method to do in background…

# File lib/background_worker/base.rb, line 40
def perform_later(options = {})
  worker = new(options)
  # Enqueue to the background queue
  worker.enqueue(self)
  worker
end
perform_now(options = {}) click to toggle source

This method is called by the job runner

# File lib/background_worker/base.rb, line 48
def perform_now(options = {})
  BackgroundWorker.verify_active_connections! if BackgroundWorker.config.backgrounded

  worker = new(options)
  execution = WorkerExecution.new(worker, options)
  execution.call
  worker
ensure
  BackgroundWorker.release_connections! if BackgroundWorker.config.backgrounded
end
queue_as(queue = nil) click to toggle source
# File lib/background_worker/base.rb, line 59
def queue_as(queue = nil)
  @queue = queue&.to_sym || :default
end

Public Instance Methods

enqueue(klass) click to toggle source
# File lib/background_worker/base.rb, line 30
def enqueue(klass)
  run_callbacks :enqueue do
    BackgroundWorker.enqueue(klass, options.merge(job_id: job_id))
  end
end
perform_now(options) click to toggle source
# File lib/background_worker/base.rb, line 24
def perform_now(options)
  run_callbacks :perform do
    perform(options)
  end
end