Class: ActiveJobTrackerRecord

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
app/models/active_job_tracker_record.rb

Constant Summary collapse

@@mutex =

Class-level mutex for thread-safety

Mutex.new

Instance Method Summary collapse

Instance Method Details

#cache_thresholdObject



15
16
17
# File 'app/models/active_job_tracker_record.rb', line 15

def cache_threshold
  @cache_threshold ||= ActiveJobTracker.configuration.cache_threshold
end

#cache_threshold=(value) ⇒ Object



11
12
13
# File 'app/models/active_job_tracker_record.rb', line 11

def cache_threshold=(value)
  @cache_threshold = value || ActiveJobTracker.configuration.cache_threshold
end

#durationObject



35
36
37
38
39
# File 'app/models/active_job_tracker_record.rb', line 35

def duration
  return nil unless started_at
  end_time = completed_at || failed_at || Time.current
  (end_time - started_at).to_f
end

#flush_progress_cacheObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/models/active_job_tracker_record.rb', line 71

def flush_progress_cache
  key = progress_cache_key

  cache_value = 0
  @@mutex.synchronize do
    cache_value = Rails.cache.read(key).to_i
    Rails.cache.delete(key)
  end

  if cache_value > 0
    with_lock do
      self.current += cache_value
      save!
    end
  end
end

#progress(use_cache: true, increment_by: 1) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/models/active_job_tracker_record.rb', line 46

def progress(use_cache: true, increment_by: 1)
  if use_cache
    key = progress_cache_key
    should_flush = false

    @@mutex.synchronize do
      current_value = Rails.cache.fetch(key, expires_in: 1.week) { 0 }.to_i
      new_value = incremented_value(current_value: current_value, increment_by: increment_by)

      Rails.cache.write(key, new_value, expires_in: 1.week)

      should_flush = new_value >= self.cache_threshold
    end

    # Flush outside the mutex to avoid deadlocks
    flush_progress_cache if should_flush
  else
    with_lock do
      self.current = incremented_value(current_value: current, increment_by: increment_by)
      save!
    end
  end
  self
end

#progress_cacheObject



19
20
21
# File 'app/models/active_job_tracker_record.rb', line 19

def progress_cache
  Rails.cache.fetch(progress_cache_key, expires_in: 1.week) { 0 }.to_i
end

#progress_cache=(value) ⇒ Object



23
24
25
# File 'app/models/active_job_tracker_record.rb', line 23

def progress_cache=(value)
  Rails.cache.write(progress_cache_key, value, expires_in: 1.week)
end

#progress_cache_keyObject



27
28
29
# File 'app/models/active_job_tracker_record.rb', line 27

def progress_cache_key
  "active_job_tracker:#{self.id}:progress_cache"
end

#progress_percentageObject



31
32
33
# File 'app/models/active_job_tracker_record.rb', line 31

def progress_percentage
  (progress_ratio * 100).to_i
end

#progress_ratioObject



41
42
43
44
# File 'app/models/active_job_tracker_record.rb', line 41

def progress_ratio
  return 0.0 if target.to_i.zero?
  [ current.to_f / target.to_f, 1.0 ].min
end