Module: SidekiqUniqueJobs::Util
- Defined in:
- lib/sidekiq_unique_jobs/util.rb
Class Method Summary collapse
- .connection(&block) ⇒ Object
- .del(keys) ⇒ Object
- .del_by(pattern = SCAN_PATTERN, count:, dry_run: true) ⇒ Object
- .dry_run(keys, pattern = nil) ⇒ Object
- .keys(pattern = SCAN_PATTERN, count: DEFAULT_COUNT) ⇒ Object
- .logger ⇒ Object
- .prefix(key) ⇒ Object
- .prefix_keys(keys) ⇒ Object
- .scan(pattern, count: 1000) ⇒ Object
- .timed(&block) ⇒ Object
- .unique_prefix ⇒ Object
Class Method Details
.connection(&block) ⇒ Object
69 70 71 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 69 def connection(&block) SidekiqUniqueJobs.connection(&block) end |
.del(keys) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 26 def del(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 |
.del_by(pattern = SCAN_PATTERN, count:, dry_run: true) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 12 def del_by(pattern = SCAN_PATTERN, count:, dry_run: true) logger.debug { "Deleting keys by: #{pattern}" } keys, time = timed { scan(pattern, count: 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 { del(keys) } logger.debug { "Deleted in #{time} sec." } end keys.size end |
.dry_run(keys, pattern = nil) ⇒ Object
38 39 40 41 42 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 38 def dry_run(keys, pattern = nil) return keys if pattern.nil? regex = Regexp.new(pattern) keys.select { |k| regex.match k } end |
.keys(pattern = SCAN_PATTERN, count: DEFAULT_COUNT) ⇒ Object
8 9 10 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 8 def keys(pattern = SCAN_PATTERN, count: DEFAULT_COUNT) scan(pattern, count: count) end |
.logger ⇒ Object
73 74 75 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 73 def logger Sidekiq.logger end |
.prefix(key) ⇒ Object
60 61 62 63 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 60 def prefix(key) return key if unique_prefix.nil? "#{unique_prefix}:#{key}" end |
.prefix_keys(keys) ⇒ Object
55 56 57 58 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 55 def prefix_keys(keys) keys = Array(keys).flatten.compact keys.map { |key| prefix(key) } end |
.scan(pattern, count: 1000) ⇒ Object
51 52 53 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 51 def scan(pattern, count: 1000) connection { |conn| conn.scan_each(match: prefix(pattern), count: count).to_a } end |
.timed(&block) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 44 def timed(&block) start = Time.now result = block.call elapsed = (Time.now - start).round(2) [result, elapsed] end |
.unique_prefix ⇒ Object
65 66 67 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 65 def unique_prefix SidekiqUniqueJobs.config.unique_prefix end |