Module: SidekiqUniqueJobs::Util

Extended by:
Util
Included in:
Util
Defined in:
lib/sidekiq_unique_jobs/util.rb

Overview

rubocop:disable Metrics/ModuleLength

Constant Summary collapse

COUNT =
'COUNT'
DEFAULT_COUNT =
1_000
EXPIRE_BATCH_SIZE =
100
MATCH =
'MATCH'
KEYS_METHOD =
'keys'
SCAN_METHOD =
'scan'
SCAN_PATTERN =
'*'

Instance Method Summary collapse

Instance Method Details

#del(pattern = SCAN_PATTERN, count = 0, dry_run = true) ⇒ Object

Raises:

  • (ArgumentError)

25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sidekiq_unique_jobs/util.rb', line 25

def del(pattern = SCAN_PATTERN, count = 0, dry_run = true)
  raise ArgumentError, 'Please provide a number of keys to delete greater than zero' if count.zero?
  logger.debug { "Deleting keys by: #{pattern}" }
  keys, time = timed { keys(pattern, count) }
  logger.debug { "#{keys.size} matching keys found in #{time} sec." }
  keys = dry_run(keys)
  logger.debug { "#{keys.size} matching keys after post-processing" }
  unless dry_run
    logger.debug { "deleting #{keys}..." }
    _, time = timed { batch_delete(keys) }
    logger.debug { "Deleted in #{time} sec." }
  end
  keys.size
end

#expireObject

rubocop:disable Metrics/MethodLength


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sidekiq_unique_jobs/util.rb', line 46

def expire # rubocop:disable Metrics/MethodLength
  removed_keys = {}
  connection do |conn|
    cursor = '0'
    loop do
      cursor, jobs = get_jobs(conn, cursor)
      jobs.each do |job_array|
        jid, unique_key = job_array

        next if conn.get(unique_key)
        conn.hdel(SidekiqUniqueJobs::HASH_KEY, jid)
        removed_keys[jid] = unique_key
      end

      break if cursor == '0'
    end
  end

  removed_keys
end

#keys(pattern = SCAN_PATTERN, count = DEFAULT_COUNT) ⇒ Object


15
16
17
# File 'lib/sidekiq_unique_jobs/util.rb', line 15

def keys(pattern = SCAN_PATTERN, count = DEFAULT_COUNT)
  send("keys_by_#{redis_keys_method}", pattern, count)
end

#unique_hashObject


40
41
42
43
44
# File 'lib/sidekiq_unique_jobs/util.rb', line 40

def unique_hash
  connection do |conn|
    conn.hgetall(SidekiqUniqueJobs::HASH_KEY)
  end
end

#unique_key(jid) ⇒ Object


19
20
21
22
23
# File 'lib/sidekiq_unique_jobs/util.rb', line 19

def unique_key(jid)
  connection do |conn|
    conn.hget(SidekiqUniqueJobs::HASH_KEY, jid)
  end
end