Class: Spotlight::JobTracker

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/spotlight/job_tracker.rb

Overview

Associate background jobs with records

Instance Method Summary collapse

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: type, exhibit: exhibit, data: args)
rescue StandardError => e
  Rails.logger.error("Unable to create log entry for job tracker #{id}: #{e}")
end

#completed?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'app/models/spotlight/job_tracker.rb', line 86

def completed?
  status == 'completed'
end

#enqueued?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'app/models/spotlight/job_tracker.rb', line 78

def enqueued?
  status == 'enqueued'
end

#failed?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'app/models/spotlight/job_tracker.rb', line 90

def failed?
  status == 'failed'
end

#in_progress?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'app/models/spotlight/job_tracker.rb', line 82

def in_progress?
  status == 'in_progress'
end

#job_statusObject



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

#labelObject



29
30
31
# File 'app/models/spotlight/job_tracker.rb', line 29

def label
  "[#{job_class.titleize}] #{resource_label}"
end

#percentObject



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

#progressObject



52
53
54
# File 'app/models/spotlight/job_tracker.rb', line 52

def progress
  data[:progress] || job_status[:progress] || 0
end

#progress_labelObject



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_labelObject



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

#statusObject



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_trackerObject



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

Returns:

  • (Boolean)


60
61
62
# File 'app/models/spotlight/job_tracker.rb', line 60

def total?
  total(default: 0).positive?
end