Class: RedisScheduler::ItemEnumerator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/redis-scheduler.rb

Overview

Enumerable class for iterating over everything in the schedule. Paginates calls to Redis under the hood (and is thus usable for very large schedules), but is not synchronized with write traffic and thus may return duplicates or skip items when paginating.

Supports random access with #[], with the same caveats as above.

Constant Summary collapse

PAGE_SIZE =
50

Instance Method Summary collapse

Constructor Details

#initialize(redis, q, uniq) ⇒ ItemEnumerator

Returns a new instance of ItemEnumerator.


193
194
195
196
197
# File 'lib/redis-scheduler.rb', line 193

def initialize redis, q, uniq
  @redis = redis
  @q = q
  @uniq = uniq
end

Instance Method Details

#[](start, num = nil) ⇒ Object


209
210
211
212
213
214
215
216
# File 'lib/redis-scheduler.rb', line 209

def [] start, num=nil
  elements = @redis.zrange @q, start, start + (num || 0), :withscores => true
  v = elements.map do |entry, at|
    item = parse_entry entry
    [item, Time.at(at.to_f)]
  end
  num ? v : v.first
end

#eachObject


200
201
202
203
204
205
206
207
# File 'lib/redis-scheduler.rb', line 200

def each
  start = 0
  while start < size
    elements = self[start, PAGE_SIZE]
    elements.each { |*x| yield(*x) }
    start += elements.size
  end
end

#parse_entry(entry) ⇒ Object

duplicated :(


221
222
223
224
225
226
227
228
# File 'lib/redis-scheduler.rb', line 221

def parse_entry entry
  if @uniq
    entry
  else
    entry =~ /^\d+:(\S+)$/ or raise InvalidEntryException, entry
    $1
  end
end

#sizeObject


218
# File 'lib/redis-scheduler.rb', line 218

def size; @redis.zcard @q end