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
-
#state ⇒ Object
Returns the value of attribute state.
-
#uid ⇒ Object
Returns the value of attribute uid.
Class Method Summary collapse
- .get_state_of(worker_id) ⇒ Object
-
.perform_later(method_name, options = {}) ⇒ Object
Public method to do in background…
-
.perform_now(method_name, options = {}) ⇒ Object
This method is called by the job runner.
- .queue_as(queue = nil) ⇒ Object
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Base
constructor
A new instance of Base.
- #log(message, options = {}) ⇒ Object
- #logger ⇒ Object
- #report_failed(message = 'Failed', detailed_message = nil) ⇒ Object
-
#report_minor_progress(message) ⇒ Object
Report a minor progress – may get called a lot, so don’t save it so often.
-
#report_progress(message) ⇒ Object
Report progress…
- #report_successful(message = 'Finished successfully') ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Base
Returns a new instance of Base.
9 10 11 12 13 14 15 16 |
# File 'lib/background_worker/base.rb', line 9 def initialize( = {}) @uid = [:uid] # Store state persistently, to enable status checkups & progress reporting @state = BackgroundWorker::PersistentState.new(@uid, .except(:uid)) log("Created #{self.class}") log("Options are: #{options.inspect}") end |
Class Attribute Details
.queue ⇒ Object (readonly)
Returns the value of attribute queue.
56 57 58 |
# File 'lib/background_worker/base.rb', line 56 def queue @queue end |
Instance Attribute Details
#state ⇒ Object
Returns the value of attribute state.
7 8 9 |
# File 'lib/background_worker/base.rb', line 7 def state @state end |
#uid ⇒ Object
Returns the value of attribute uid.
7 8 9 |
# File 'lib/background_worker/base.rb', line 7 def uid @uid end |
Class Method Details
.get_state_of(worker_id) ⇒ Object
57 58 59 |
# File 'lib/background_worker/base.rb', line 57 def get_state_of(worker_id) BackgroundWorker::PersistentState.get_state_of(worker_id) end |
.perform_later(method_name, options = {}) ⇒ Object
Public method to do in background…
62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/background_worker/base.rb', line 62 def perform_later(method_name, = {}) opts = .symbolize_keys method_name = method_name.to_sym opts[:uid] ||= BackgroundWorker::Uid.new(to_s, method_name).generate # Store into shared-cache before kicking job off BackgroundWorker::PersistentState.new(opts[:uid], opts.except(:uid)) # Enqueue to the background queue BackgroundWorker.enqueue(self, method_name, opts) opts[:uid] end |
.perform_now(method_name, options = {}) ⇒ Object
This method is called by the job runner
It will just call your preferred method in the worker.
80 81 82 83 84 85 86 87 88 |
# File 'lib/background_worker/base.rb', line 80 def perform_now(method_name, = {}) BackgroundWorker.verify_active_connections! if BackgroundWorker.config.backgrounded worker = new() execution = WorkerExecution.new(worker, method_name, ) execution.call ensure BackgroundWorker.release_connections! if BackgroundWorker.config.backgrounded end |
.queue_as(queue = nil) ⇒ Object
90 91 92 |
# File 'lib/background_worker/base.rb', line 90 def queue_as(queue = nil) @queue = queue&.to_sym || :default end |
Instance Method Details
#log(message, options = {}) ⇒ Object
50 51 52 53 |
# File 'lib/background_worker/base.rb', line 50 def log(, = {}) severity = .fetch(:severity, :info) logger.send(severity, "uid=#{uid} #{message}") end |
#logger ⇒ Object
46 47 48 |
# File 'lib/background_worker/base.rb', line 46 def logger BackgroundWorker.logger end |
#report_failed(message = 'Failed', detailed_message = nil) ⇒ Object
41 42 43 44 |
# File 'lib/background_worker/base.rb', line 41 def report_failed( = 'Failed', = nil) state. = state.set_completed(, :failed) end |
#report_minor_progress(message) ⇒ Object
Report a minor progress – may get called a lot, so don’t save it so often
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/background_worker/base.rb', line 25 def report_minor_progress() state. = # Only report minor events once per second @last_report ||= Time.now - 2 time_elapsed = Time.now - @last_report return unless time_elapsed > 1 @last_report = Time.now state.save end |
#report_progress(message) ⇒ Object
Report progress…
19 20 21 22 |
# File 'lib/background_worker/base.rb', line 19 def report_progress() state. = state.save end |
#report_successful(message = 'Finished successfully') ⇒ Object
37 38 39 |
# File 'lib/background_worker/base.rb', line 37 def report_successful( = 'Finished successfully') state.set_completed(, :successful) end |