Class: BackgroundWorker::Base
- Inherits:
-
Object
- Object
- BackgroundWorker::Base
- Defined in:
- lib/background_worker/base.rb
Class Attribute Summary collapse
-
.queue ⇒ Object
readonly
Returns the value of attribute queue.
Instance Attribute Summary collapse
-
#job_id ⇒ Object
Returns the value of attribute job_id.
-
#options ⇒ Object
Returns the value of attribute options.
Attributes included from State
Class Method Summary collapse
- .after_enqueue(*filters, &blk) ⇒ Object
- .after_perform(*filters, &blk) ⇒ Object
- .before_enqueue(*filters, &blk) ⇒ Object
- .before_perform(*filters, &blk) ⇒ Object
-
.perform_later(options = {}) ⇒ Object
Public method to do in background…
-
.perform_now(options = {}) ⇒ Object
This method is called by the job runner.
- .queue_as(queue = nil) ⇒ Object
Instance Method Summary collapse
- #enqueue(klass) ⇒ Object
-
#initialize(options = {}) ⇒ Base
constructor
A new instance of Base.
- #perform_now(options) ⇒ Object
Methods included from State
#report_failed, #report_minor_progress, #report_progress, #report_successful
Methods included from Logging
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 = .symbolize_keys @job_id = @options[:job_id] || SecureRandom.uuid log("Created #{self.class}") log("Options are: #{@options.inspect}") end |
Class Attribute Details
.queue ⇒ Object (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_id ⇒ Object
Returns the value of attribute job_id.
12 13 14 |
# File 'lib/background_worker/base.rb', line 12 def job_id @job_id end |
#options ⇒ Object
Returns the value of attribute options.
12 13 14 |
# File 'lib/background_worker/base.rb', line 12 def @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( = {}) worker = new() # 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( = {}) BackgroundWorker.verify_active_connections! if BackgroundWorker.config.backgrounded worker = new() execution = WorkerExecution.new(worker, ) 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, .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() run_callbacks :perform do perform() end end |