Sha256: 71b66675a5b29d367f17bea36e00d5720dfe2676b3be8541b4340f8d5259f032

Contents?: true

Size: 1.94 KB

Versions: 6

Compression:

Stored size: 1.94 KB

Contents

# frozen_string_literal: true

module SidekiqUniqueJobs
  module Util
    COUNT             = 'COUNT'
    DEFAULT_COUNT     = 1_000
    EXPIRE_BATCH_SIZE = 100
    SCAN_METHOD       = 'SCAN'
    SCAN_PATTERN      = '*'

    include SidekiqUniqueJobs::Logging
    include SidekiqUniqueJobs::Connection
    extend self # rubocop:disable Style/ModuleFunction

    def keys(pattern = SCAN_PATTERN, count = DEFAULT_COUNT)
      return redis(&:keys) if pattern.nil?
      redis { |conn| conn.scan_each(match: prefix(pattern), count: count).to_a }
    end

    # Deletes unique keys from redis
    #
    #
    # @param pattern [String] a pattern to scan for in redis
    # @param count [Integer] the maximum number of keys to delete
    # @return [Boolean] report success
    def del(pattern = SCAN_PATTERN, count = 0)
      raise ArgumentError, 'Please provide a number of keys to delete greater than zero' if count.zero?
      pattern = "#{pattern}:*" unless pattern.end_with?(':*')

      log_debug { "Deleting keys by: #{pattern}" }
      keys, time = timed { keys(pattern, count) }
      key_size   = keys.size
      log_debug { "#{key_size} keys found in #{time} sec." }
      _, time = timed { batch_delete(keys) }
      log_debug { "Deleted #{key_size} keys in #{time} sec." }

      key_size
    end

    private

    def batch_delete(keys)
      redis do |conn|
        keys.each_slice(500) do |chunk|
          conn.pipelined do
            chunk.each do |key|
              conn.del key
            end
          end
        end
      end
    end

    def timed
      start   = current_time
      result  = yield
      elapsed = (current_time - start).round(2)
      [result, elapsed]
    end

    def current_time
      Time.now
    end

    def prefix(key)
      return key if unique_prefix.nil?
      return key if key.start_with?("#{unique_prefix}:")
      "#{unique_prefix}:#{key}"
    end

    def unique_prefix
      SidekiqUniqueJobs.config.unique_prefix
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
sidekiq-unique-jobs-6.0.0.rc6 lib/sidekiq_unique_jobs/util.rb
sidekiq-unique-jobs-6.0.0.rc5 lib/sidekiq_unique_jobs/util.rb
sidekiq-unique-jobs-6.0.0.rc4 lib/sidekiq_unique_jobs/util.rb
sidekiq-unique-jobs-6.0.0.rc3 lib/sidekiq_unique_jobs/util.rb
sidekiq-unique-jobs-6.0.0.rc2 lib/sidekiq_unique_jobs/util.rb
sidekiq-unique-jobs-6.0.0.rc1 lib/sidekiq_unique_jobs/util.rb