Sha256: 785bdea93c382164c0252847d6a44394c025928086cf081b17116a737391506e

Contents?: true

Size: 1.17 KB

Versions: 50

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 [Integer]
    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

50 entries across 50 versions & 1 rubygems

Version Path
good_job-3.25.0 lib/good_job/cleanup_tracker.rb
good_job-3.24.0 lib/good_job/cleanup_tracker.rb
good_job-3.23.0 lib/good_job/cleanup_tracker.rb
good_job-3.22.0 lib/good_job/cleanup_tracker.rb
good_job-3.21.5 lib/good_job/cleanup_tracker.rb
good_job-3.21.4 lib/good_job/cleanup_tracker.rb
good_job-3.21.3 lib/good_job/cleanup_tracker.rb
good_job-3.21.2 lib/good_job/cleanup_tracker.rb
good_job-3.21.1 lib/good_job/cleanup_tracker.rb
good_job-3.21.0 lib/good_job/cleanup_tracker.rb
good_job-3.20.0 lib/good_job/cleanup_tracker.rb
good_job-3.19.4 lib/good_job/cleanup_tracker.rb
good_job-3.19.3 lib/good_job/cleanup_tracker.rb
good_job-3.19.2 lib/good_job/cleanup_tracker.rb
good_job-3.19.1 lib/good_job/cleanup_tracker.rb
good_job-3.19.0 lib/good_job/cleanup_tracker.rb
good_job-3.18.3 lib/good_job/cleanup_tracker.rb
good_job-3.18.2 lib/good_job/cleanup_tracker.rb
good_job-3.18.1 lib/good_job/cleanup_tracker.rb
good_job-3.18.0 lib/good_job/cleanup_tracker.rb