Class: BackgroundWorker::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Callbacks, Logging, State
Defined in:
lib/background_worker/base.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes included from State

#state

Class Method Summary collapse

Instance Method Summary collapse

Methods included from State

#report_failed, #report_minor_progress, #report_progress, #report_successful

Methods included from Logging

#log, #logger

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



16
17
18
19
20
21
22
# 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

Class Attribute Details

.queueObject (readonly)

Returns the value of attribute queue.



37
38
39
# File 'lib/background_worker/base.rb', line 37

def queue
  @queue
end

Instance Attribute Details

#job_idObject

Returns the value of attribute job_id.



12
13
14
# File 'lib/background_worker/base.rb', line 12

def job_id
  @job_id
end

#optionsObject

Returns the value of attribute options.



12
13
14
# File 'lib/background_worker/base.rb', line 12

def options
  @options
end

Class Method Details

.after_enqueue(*filters, &blk) ⇒ Object



75
76
77
# File 'lib/background_worker/base.rb', line 75

def after_enqueue(*filters, &blk)
  set_callback(:enqueue, :after, *filters, &blk)
end

.after_perform(*filters, &blk) ⇒ Object



67
68
69
# File 'lib/background_worker/base.rb', line 67

def after_perform(*filters, &blk)
  set_callback(:perform, :after, *filters, &blk)
end

.before_enqueue(*filters, &blk) ⇒ Object



71
72
73
# File 'lib/background_worker/base.rb', line 71

def before_enqueue(*filters, &blk)
  set_callback(:enqueue, :before, *filters, &blk)
end

.before_perform(*filters, &blk) ⇒ Object



63
64
65
# File 'lib/background_worker/base.rb', line 63

def before_perform(*filters, &blk)
  set_callback(:perform, :before, *filters, &blk)
end

.perform_later(options = {}) ⇒ Object

Public method to do in background…



40
41
42
43
44
45
# 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 = {}) ⇒ Object

This method is called by the job runner



48
49
50
51
52
53
54
55
56
57
# 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) ⇒ Object



59
60
61
# File 'lib/background_worker/base.rb', line 59

def queue_as(queue = nil)
  @queue = queue&.to_sym || :default
end

Instance Method Details

#enqueue(klass) ⇒ Object



30
31
32
33
34
# 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) ⇒ Object



24
25
26
27
28
# File 'lib/background_worker/base.rb', line 24

def perform_now(options)
  run_callbacks :perform do
    perform(options)
  end
end