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
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 63 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
103 104 105 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 103 def connection(&block) SidekiqUniqueJobs.connection(&block) end |
.del(pattern = SCAN_PATTERN, count = 0, dry_run = true) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 22 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
75 76 77 78 79 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 75 def dry_run(keys, pattern = nil) return keys if pattern.nil? regex = Regexp.new(pattern) keys.select { |k| regex.match k } end |
.expire ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 43 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
12 13 14 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 12 def keys(pattern = SCAN_PATTERN, count = DEFAULT_COUNT) send("keys_by_#{redis_keys_method}", pattern, count) end |
.keys_by_keys(pattern, _count) ⇒ Object
59 60 61 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 59 def keys_by_keys(pattern, _count) connection { |conn| conn.keys(prefix(pattern)).to_a } end |
.keys_by_scan(pattern, count) ⇒ Object
55 56 57 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 55 def keys_by_scan(pattern, count) connection { |conn| conn.scan_each(match: prefix(pattern), count: count).to_a } end |
.logger ⇒ Object
115 116 117 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 115 def logger SidekiqUniqueJobs.logger end |
.prefix(key) ⇒ Object
93 94 95 96 97 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 93 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
88 89 90 91 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 88 def prefix_keys(keys) keys = Array(keys).flatten.compact keys.map { |key| prefix(key) } end |
.redis_keys_method ⇒ Object
111 112 113 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 111 def redis_keys_method (redis_version >= '2.8') ? SCAN_METHOD : KEYS_METHOD end |
.redis_version ⇒ Object
107 108 109 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 107 def redis_version SidekiqUniqueJobs.redis_version end |
.timed(&_block) ⇒ Object
81 82 83 84 85 86 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 81 def timed(&_block) start = Time.now result = yield elapsed = (Time.now - start).round(2) [result, elapsed] end |
.unique_hash ⇒ Object
37 38 39 40 41 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 37 def unique_hash connection do |conn| conn.hgetall(SidekiqUniqueJobs::HASH_KEY) end end |
.unique_key(jid) ⇒ Object
16 17 18 19 20 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 16 def unique_key(jid) connection do |conn| conn.hget(SidekiqUniqueJobs::HASH_KEY, jid) end end |
.unique_prefix ⇒ Object
99 100 101 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 99 def unique_prefix SidekiqUniqueJobs.config.unique_prefix end |