Module: SidekiqUniqueJobs::Util
- Defined in:
- lib/sidekiq_unique_jobs/util.rb
Class Method Summary collapse
- .batch_delete(keys) ⇒ Object
- .connection(&block) ⇒ Object
- .del(pattern = SCAN_PATTERN, count = 0, dry_run = true) ⇒ Object
- .dry_run(keys, pattern = nil) ⇒ Object
- .expire ⇒ Object
- .keys(pattern = SCAN_PATTERN, count = DEFAULT_COUNT) ⇒ Object
- .keys_by_keys(pattern, _count) ⇒ Object
- .keys_by_scan(pattern, count) ⇒ Object
- .logger ⇒ Object
- .prefix(key) ⇒ Object
- .prefix_keys(keys) ⇒ Object
- .redis_keys_method ⇒ Object
- .redis_version ⇒ Object
- .timed(&_block) ⇒ Object
- .unique_hash ⇒ Object
- .unique_key(jid) ⇒ Object
- .unique_prefix ⇒ Object
Class Method Details
.batch_delete(keys) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 61 def batch_delete(keys) connection do |conn| keys.each_slice(500) do |chunk| conn.pipelined do chunk.each do |key| conn.del key end end end end end |
.connection(&block) ⇒ Object
101 102 103 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 101 def connection(&block) SidekiqUniqueJobs.connection(&block) end |
.del(pattern = SCAN_PATTERN, count = 0, dry_run = true) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 20 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 postprocessing" } unless dry_run logger.debug { "deleting #{keys}..." } _, time = timed { batch_delete(keys) } logger.debug { "Deleted in #{time} sec." } end keys.size end |
.dry_run(keys, pattern = nil) ⇒ Object
73 74 75 76 77 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 73 def dry_run(keys, pattern = nil) return keys if pattern.nil? regex = Regexp.new(pattern) keys.select { |k| regex.match k } end |
.expire ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 41 def expire removed_keys = {} connection do |conn| conn.hgetall(SidekiqUniqueJobs::HASH_KEY).each do |jid, unique_key| next if conn.get(unique_key) conn.hdel(SidekiqUniqueJobs::HASH_KEY, jid) removed_keys[jid] = unique_key end end removed_keys end |
.keys(pattern = SCAN_PATTERN, count = DEFAULT_COUNT) ⇒ Object
10 11 12 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 10 def keys(pattern = SCAN_PATTERN, count = DEFAULT_COUNT) send("keys_by_#{redis_keys_method}", pattern, count) end |
.keys_by_keys(pattern, _count) ⇒ Object
57 58 59 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 57 def keys_by_keys(pattern, _count) connection { |conn| conn.keys(prefix(pattern)).to_a } end |
.keys_by_scan(pattern, count) ⇒ Object
53 54 55 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 53 def keys_by_scan(pattern, count) connection { |conn| conn.scan_each(match: prefix(pattern), count: count).to_a } end |
.logger ⇒ Object
113 114 115 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 113 def logger SidekiqUniqueJobs.logger end |
.prefix(key) ⇒ Object
91 92 93 94 95 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 91 def prefix(key) return key if unique_prefix.nil? return key if key.start_with?("#{unique_prefix}:") "#{unique_prefix}:#{key}" end |
.prefix_keys(keys) ⇒ Object
86 87 88 89 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 86 def prefix_keys(keys) keys = Array(keys).flatten.compact keys.map { |key| prefix(key) } end |
.redis_keys_method ⇒ Object
109 110 111 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 109 def redis_keys_method redis_version >= '2.8' ? SCAN_METHOD : KEYS_METHOD end |
.redis_version ⇒ Object
105 106 107 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 105 def redis_version SidekiqUniqueJobs.redis_version end |
.timed(&_block) ⇒ Object
79 80 81 82 83 84 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 79 def timed(&_block) start = Time.now result = yield elapsed = (Time.now - start).round(2) [result, elapsed] end |
.unique_hash ⇒ Object
35 36 37 38 39 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 35 def unique_hash connection do |conn| conn.hgetall(SidekiqUniqueJobs::HASH_KEY) end end |
.unique_key(jid) ⇒ Object
14 15 16 17 18 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 14 def unique_key(jid) connection do |redis| redis.hget(SidekiqUniqueJobs::HASH_KEY, jid) end end |
.unique_prefix ⇒ Object
97 98 99 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 97 def unique_prefix SidekiqUniqueJobs.config.unique_prefix end |