Class: GoodJob::CleanupTracker

Inherits:
Object
  • Object
show all
Defined in:
lib/good_job/cleanup_tracker.rb

Overview

Tracks thresholds for cleaning up old jobs.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cleanup_interval_seconds: false, cleanup_interval_jobs: false) ⇒ CleanupTracker

Returns a new instance of CleanupTracker.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
# File 'lib/good_job/cleanup_tracker.rb', line 11

def initialize(cleanup_interval_seconds: false, cleanup_interval_jobs: false)
  raise ArgumentError, "Do not use `0` for cleanup intervals. Use `false` to disable, or -1 to always run" if cleanup_interval_seconds == 0 || cleanup_interval_jobs == 0 # rubocop:disable Style/NumericPredicate

  self.cleanup_interval_seconds = cleanup_interval_seconds
  self.cleanup_interval_jobs = cleanup_interval_jobs

  reset
end

Instance Attribute Details

#cleanup_interval_jobsObject

Returns the value of attribute cleanup_interval_jobs.



6
7
8
# File 'lib/good_job/cleanup_tracker.rb', line 6

def cleanup_interval_jobs
  @cleanup_interval_jobs
end

#cleanup_interval_secondsObject

Returns the value of attribute cleanup_interval_seconds.



6
7
8
# File 'lib/good_job/cleanup_tracker.rb', line 6

def cleanup_interval_seconds
  @cleanup_interval_seconds
end

#job_countObject

Returns the value of attribute job_count.



6
7
8
# File 'lib/good_job/cleanup_tracker.rb', line 6

def job_count
  @job_count
end

#last_atObject

Returns the value of attribute last_at.



6
7
8
# File 'lib/good_job/cleanup_tracker.rb', line 6

def last_at
  @last_at
end

Instance Method Details

#cleanup?Boolean

Whether a cleanup should be run.

Returns:

  • (Boolean)


28
29
30
31
32
# File 'lib/good_job/cleanup_tracker.rb', line 28

def cleanup?
  (cleanup_interval_jobs && job_count > cleanup_interval_jobs) ||
    (cleanup_interval_seconds && last_at < Time.current - cleanup_interval_seconds) ||
    false
end

#incrementInteger

Increments job count.

Returns:

  • (Integer)


22
23
24
# File 'lib/good_job/cleanup_tracker.rb', line 22

def increment
  self.job_count += 1
end

#resetvoid

This method returns an undefined value.

Resets the counters.



36
37
38
39
# File 'lib/good_job/cleanup_tracker.rb', line 36

def reset
  self.job_count = 0
  self.last_at = Time.current
end