Class: ActiveJob::Status::Status
- Inherits:
-
Object
- Object
- ActiveJob::Status::Status
- Defined in:
- lib/activejob-status/status.rb
Instance Method Summary collapse
- #[]=(key, value) ⇒ Object
- #catch_exception(e) ⇒ Object
- #delete ⇒ Object
-
#initialize(job, options = {}) ⇒ Status
constructor
A new instance of Status.
- #job_id ⇒ Object
- #present? ⇒ Boolean
- #progress ⇒ Object
- #read ⇒ Object (also: #to_h)
- #status ⇒ Object
- #status_inquiry ⇒ Object
- #update(payload, options = {}) ⇒ Object
-
#update_defaults(status_key) ⇒ Object
Update default data.
Constructor Details
#initialize(job, options = {}) ⇒ Status
Returns a new instance of Status.
9 10 11 12 13 14 |
# File 'lib/activejob-status/status.rb', line 9 def initialize(job, = {}) = ActiveJob::Status..merge() @defaults = .fetch(:includes, []) @storage = ActiveJob::Status::Storage.new(.without(:includes)) @job = job end |
Instance Method Details
#[]=(key, value) ⇒ Object
16 17 18 |
# File 'lib/activejob-status/status.rb', line 16 def []=(key, value) update({key => value}, force: true) end |
#catch_exception(e) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/activejob-status/status.rb', line 71 def catch_exception(e) raise "cannot call #catch_exception when status is accessed from outside the job" if @job.is_a?(String) payload = {} payload[:status] = :failed if @defaults.include?(:status) payload[:serialized_job] = @job.serialize if @defaults.include?(:serialized_job) if @defaults.include?(:exception) = e. = e. if e.respond_to?(:original_message) payload[:exception] = {class: e.class.name, message: } end update(payload, force: true) end |
#delete ⇒ Object
34 35 36 |
# File 'lib/activejob-status/status.rb', line 34 def delete @storage.delete(@job) end |
#job_id ⇒ Object
38 39 40 |
# File 'lib/activejob-status/status.rb', line 38 def job_id @storage.job_id(@job) end |
#present? ⇒ Boolean
52 53 54 |
# File 'lib/activejob-status/status.rb', line 52 def present? read.present? end |
#progress ⇒ Object
46 47 48 49 50 |
# File 'lib/activejob-status/status.rb', line 46 def progress read.then do |hash| hash[:progress].to_f / hash[:total].to_f end end |
#read ⇒ Object Also known as: to_h
20 21 22 |
# File 'lib/activejob-status/status.rb', line 20 def read @storage.read(@job) end |
#status ⇒ Object
42 43 44 |
# File 'lib/activejob-status/status.rb', line 42 def status read[:status] end |
#status_inquiry ⇒ Object
56 57 58 |
# File 'lib/activejob-status/status.rb', line 56 def status_inquiry status.to_s.inquiry end |
#update(payload, options = {}) ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/activejob-status/status.rb', line 25 def update(payload, = {}) if @job.respond_to?(:progress) @job.progress.instance_variable_set(:@progress, payload[:progress]) if payload.include?(:progress) @job.progress.instance_variable_set(:@total, payload[:total]) if payload.include?(:total) end @storage.update(@job, payload, **) end |
#update_defaults(status_key) ⇒ Object
Update default data
62 63 64 65 66 67 68 69 |
# File 'lib/activejob-status/status.rb', line 62 def update_defaults(status_key) raise "cannot call #update_defaults when status is accessed from outside the job" if @job.is_a?(String) payload = {} payload[:status] = status_key if @defaults.include?(:status) payload[:serialized_job] = @job.serialize if @defaults.include?(:serialized_job) update(payload, force: true) end |