Sha256: 9070ebaaf9923101c53baf1e56045bae9a78bce8b88bf141e82f45dd8ec9cbbb

Contents?: true

Size: 1.17 KB

Versions: 36

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true
module GoodJob # :nodoc:
  # Tracks thresholds for cleaning up old jobs.
  class CleanupTracker
    attr_accessor :cleanup_interval_seconds,
                  :cleanup_interval_jobs,
                  :job_count,
                  :last_at

    def initialize(cleanup_interval_seconds: false, cleanup_interval_jobs: false)
      raise ArgumentError, "Do not use `0`. 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

    # Increments job count.
    # @return [void]
    def increment
      self.job_count += 1
    end

    # Whether a cleanup should be run.
    # @return [Boolean]
    def cleanup?
      (cleanup_interval_jobs && job_count > cleanup_interval_jobs) ||
        (cleanup_interval_seconds && last_at < Time.current - cleanup_interval_seconds) ||
        false
    end

    # Resets the counters.
    # @return [void]
    def reset
      self.job_count = 0
      self.last_at = Time.current
    end
  end
end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
good_job-3.15.14 lib/good_job/cleanup_tracker.rb
good_job-3.15.13 lib/good_job/cleanup_tracker.rb
good_job-3.15.12 lib/good_job/cleanup_tracker.rb
good_job-3.15.11 lib/good_job/cleanup_tracker.rb
good_job-3.15.10 lib/good_job/cleanup_tracker.rb
good_job-3.15.9 lib/good_job/cleanup_tracker.rb
good_job-3.15.8 lib/good_job/cleanup_tracker.rb
good_job-3.15.7 lib/good_job/cleanup_tracker.rb
good_job-3.15.6 lib/good_job/cleanup_tracker.rb
good_job-3.15.5 lib/good_job/cleanup_tracker.rb
good_job-3.15.4 lib/good_job/cleanup_tracker.rb
good_job-3.15.3 lib/good_job/cleanup_tracker.rb
good_job-3.15.2 lib/good_job/cleanup_tracker.rb
good_job-3.15.1 lib/good_job/cleanup_tracker.rb
good_job-3.15.0 lib/good_job/cleanup_tracker.rb
good_job-3.14.2 lib/good_job/cleanup_tracker.rb
good_job-3.14.1 lib/good_job/cleanup_tracker.rb
good_job-3.14.0 lib/good_job/cleanup_tracker.rb
good_job-3.13.0 lib/good_job/cleanup_tracker.rb
good_job-3.12.8 lib/good_job/cleanup_tracker.rb