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