Class: Wurk::BatchSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/wurk/batch_set.rb

Overview

Discovery API over the global ‘batches` sorted set. `size`, `each` (yields Status), `scan_tags(tag)` for tag-indexed lookup.

Spec: docs/target/sidekiq-pro.md §2.7.

Direct Known Subclasses

Wurk::Batch::DeadSet

Constant Summary collapse

PAGE_SIZE =
100

Instance Method Summary collapse

Constructor Details

#initialize(key: 'batches') ⇒ BatchSet

Returns a new instance of BatchSet.



15
16
17
# File 'lib/wurk/batch_set.rb', line 15

def initialize(key: 'batches')
  @key = key
end

Instance Method Details

#eachObject

Newest-first iteration of every batch indexed in the set. Yields ‘Wurk::Batch::Status` instances — they HGETALL on construction so the caller pays one Redis round-trip per batch.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/wurk/batch_set.rb', line 26

def each
  return enum_for(:each) unless block_given?

  page = 0
  loop do
    start = page * PAGE_SIZE
    stop  = start + PAGE_SIZE - 1
    bids  = Wurk.redis { |conn| conn.call('ZRANGE', @key, start, stop, 'REV') }
    bids.each { |bid| yield Wurk::Batch::Status.new(bid) }
    break if bids.size < PAGE_SIZE

    page += 1
  end
end

#scan_tags(tag, &block) ⇒ Object

Yields each BID indexed under the given tag (set at ‘tags:<tag>`). No JSON parsing; cheap iteration. Caller wraps in `Status.new(bid)` if it needs full state.



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/wurk/batch_set.rb', line 44

def scan_tags(tag, &block)
  return enum_for(:scan_tags, tag) unless block_given?

  cursor = '0'
  Wurk.redis do |conn|
    loop do
      cursor, members = conn.call('SSCAN', "tags:#{tag}", cursor, 'COUNT', PAGE_SIZE)
      members.each(&block)
      break if cursor == '0'
    end
  end
end

#sizeObject



19
20
21
# File 'lib/wurk/batch_set.rb', line 19

def size
  Wurk.redis { |conn| conn.call('ZCARD', @key) }.to_i
end