Class: Spotlight::JobTracker
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Spotlight::JobTracker
- Defined in:
- app/models/spotlight/job_tracker.rb
Overview
Associate background jobs with records
Instance Method Summary collapse
- #append_log_entry(type:, exhibit: nil, **args) ⇒ Object
- #completed? ⇒ Boolean
- #enqueued? ⇒ Boolean
- #failed? ⇒ Boolean
- #in_progress? ⇒ Boolean
- #job_status ⇒ Object
- #label ⇒ Object
- #percent ⇒ Object
- #progress ⇒ Object
- #progress_label ⇒ Object
- #resource_label ⇒ Object
- #status ⇒ Object
- #top_level_job_tracker ⇒ Object
- #total(default: progress) ⇒ Object
- #total? ⇒ Boolean
Instance Method Details
#append_log_entry(type:, exhibit: nil, **args) ⇒ Object
94 95 96 97 98 |
# File 'app/models/spotlight/job_tracker.rb', line 94 def append_log_entry(type:, exhibit: nil, **args) events.create(type:, exhibit:, data: args) rescue StandardError => e Rails.logger.error("Unable to create log entry for job tracker #{id}: #{e}") end |
#completed? ⇒ Boolean
86 87 88 |
# File 'app/models/spotlight/job_tracker.rb', line 86 def completed? status == 'completed' end |
#enqueued? ⇒ Boolean
78 79 80 |
# File 'app/models/spotlight/job_tracker.rb', line 78 def enqueued? status == 'enqueued' end |
#failed? ⇒ Boolean
90 91 92 |
# File 'app/models/spotlight/job_tracker.rb', line 90 def failed? status == 'failed' end |
#in_progress? ⇒ Boolean
82 83 84 |
# File 'app/models/spotlight/job_tracker.rb', line 82 def in_progress? status == 'in_progress' end |
#job_status ⇒ Object
40 41 42 43 44 |
# File 'app/models/spotlight/job_tracker.rb', line 40 def job_status return {} unless job_id @job_status ||= ActiveJob::Status.get(job_id) end |
#label ⇒ Object
29 30 31 |
# File 'app/models/spotlight/job_tracker.rb', line 29 def label "[#{job_class.titleize}] #{resource_label}" end |
#percent ⇒ Object
64 65 66 67 68 |
# File 'app/models/spotlight/job_tracker.rb', line 64 def percent return nil unless total? (100.0 * progress) / total end |
#progress ⇒ Object
52 53 54 |
# File 'app/models/spotlight/job_tracker.rb', line 52 def progress data[:progress] || job_status[:progress] || 0 end |
#progress_label ⇒ Object
46 47 48 49 50 |
# File 'app/models/spotlight/job_tracker.rb', line 46 def progress_label return number_with_delimiter(progress) unless total? "#{number_with_delimiter(progress)} / #{number_with_delimiter(total)}" end |
#resource_label ⇒ Object
33 34 35 36 37 38 |
# File 'app/models/spotlight/job_tracker.rb', line 33 def resource_label return resource.filename if resource.is_a? ActiveStorage::Blob return resource.name if resource.is_a? Upload resource_id end |
#status ⇒ Object
70 71 72 73 74 75 76 |
# File 'app/models/spotlight/job_tracker.rb', line 70 def status @status ||= super @status ||= 'missing' @status = 'in_progress' if @status == 'completed' && job_trackers.any? { |t| t.in_progress? || t.enqueued? } @status end |
#top_level_job_tracker ⇒ Object
100 101 102 103 104 105 106 |
# File 'app/models/spotlight/job_tracker.rb', line 100 def top_level_job_tracker if on.is_a?(Spotlight::JobTracker) on.top_level_job_tracker else self end end |
#total(default: progress) ⇒ Object
56 57 58 |
# File 'app/models/spotlight/job_tracker.rb', line 56 def total(default: progress) [progress, data[:total] || job_status[:total] || default].max end |
#total? ⇒ Boolean
60 61 62 |
# File 'app/models/spotlight/job_tracker.rb', line 60 def total? total(default: 0).positive? end |