Class: BackgroundWorker::Base
- Inherits:
-
Object
- Object
- BackgroundWorker::Base
- Defined in:
- lib/background_worker/base.rb
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(method_name, options = {}) ⇒ Object
This method is called by the job runner.
-
.perform_in_background(method_name, options = {}) ⇒ Object
Public method to do in background…
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.
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/background_worker/base.rb', line 5 def initialize( = {}) Time.zone = Setting.time_zone @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: #{.pretty_inspect}") end |
Instance Attribute Details
#state ⇒ Object
Returns the value of attribute state.
3 4 5 |
# File 'lib/background_worker/base.rb', line 3 def state @state end |
#uid ⇒ Object
Returns the value of attribute uid.
3 4 5 |
# File 'lib/background_worker/base.rb', line 3 def uid @uid end |
Class Method Details
.get_state_of(worker_id) ⇒ Object
54 55 56 |
# File 'lib/background_worker/base.rb', line 54 def get_state_of(worker_id) BackgroundWorker::PersistentState.get_state_of(worker_id) end |
.perform(method_name, options = {}) ⇒ Object
This method is called by the job runner
It will just call your preferred method in the worker.
75 76 77 78 79 80 81 82 83 |
# File 'lib/background_worker/base.rb', line 75 def perform(method_name, = {}) BackgroundWorker.verify_active_connections! worker = new() execution = WorkerExecution.new(worker, method_name, ) execution.call end |
.perform_in_background(method_name, options = {}) ⇒ Object
Public method to do in background…
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/background_worker/base.rb', line 59 def perform_in_background(method_name, = {}) method_name = method_name.to_sym [:uid] ||= BackgroundWorker::Uid.new(to_s, method_name).generate # Store into shared-cache before kicking job off BackgroundWorker::PersistentState.new([:uid], .except(:uid)) # Enqueue to the background queue BackgroundWorker.enqueue(self, method_name, ) [:uid] end |
Instance Method Details
#log(message, options = {}) ⇒ Object
48 49 50 51 |
# File 'lib/background_worker/base.rb', line 48 def log(, = {}) severity = .fetch(:severity, :info) logger.send(severity, "uid=#{uid} #{}") end |
#logger ⇒ Object
44 45 46 |
# File 'lib/background_worker/base.rb', line 44 def logger BackgroundWorker.logger end |
#report_failed(message = 'Failed', detailed_message = nil) ⇒ Object
39 40 41 42 |
# File 'lib/background_worker/base.rb', line 39 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
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/background_worker/base.rb', line 23 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…
17 18 19 20 |
# File 'lib/background_worker/base.rb', line 17 def report_progress() state. = state.save end |
#report_successful(message = 'Finished successfully') ⇒ Object
35 36 37 |
# File 'lib/background_worker/base.rb', line 35 def report_successful( = 'Finished successfully') state.set_completed(, :successful) end |