Class: Sidekiq::SortedSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/sidekiq/api.rb

Direct Known Subclasses

RetrySet, ScheduledSet

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ SortedSet

Returns a new instance of SortedSet.



253
254
255
# File 'lib/sidekiq/api.rb', line 253

def initialize(name)
  @zset = name
end

Instance Method Details

#clearObject



326
327
328
329
330
# File 'lib/sidekiq/api.rb', line 326

def clear
  Sidekiq.redis do |conn|
    conn.del(@zset)
  end
end

#delete(score, jid = nil) ⇒ Object



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/sidekiq/api.rb', line 304

def delete(score, jid = nil)
  if jid
    elements = Sidekiq.redis do |conn|
      conn.zrangebyscore(@zset, score, score)
    end

    elements_with_jid = elements.map do |element|
      message = Sidekiq.load_json(element)

      if message["jid"] == jid
        Sidekiq.redis { |conn| conn.zrem(@zset, element) }
      end
    end
    elements_with_jid.count != 0
  else
    count = Sidekiq.redis do |conn|
      conn.zremrangebyscore(@zset, score, score)
    end
    count != 0
  end
end

#each(&block) ⇒ Object



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/sidekiq/api.rb', line 267

def each(&block)
  # page thru the sorted set backwards so deleting entries doesn't screw up indexing
  page = -1
  page_size = 50

  loop do
    elements = Sidekiq.redis do |conn|
      conn.zrange @zset, page * page_size, (page * page_size) + (page_size - 1), :with_scores => true
    end
    break if elements.empty?
    page -= 1
    elements.each do |element, score|
      block.call SortedEntry.new(self, score, element)
    end
  end
end

#fetch(score, jid = nil) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/sidekiq/api.rb', line 284

def fetch(score, jid = nil)
  elements = Sidekiq.redis do |conn|
    conn.zrangebyscore(@zset, score, score)
  end

  elements.inject([]) do |result, element|
    entry = SortedEntry.new(self, score, element)
    if jid
      result << entry if entry.jid == jid
    else
      result << entry
    end
    result
  end
end

#find_job(jid) ⇒ Object



300
301
302
# File 'lib/sidekiq/api.rb', line 300

def find_job(jid)
  self.detect { |j| j.jid == jid }
end

#schedule(timestamp, message) ⇒ Object



261
262
263
264
265
# File 'lib/sidekiq/api.rb', line 261

def schedule(timestamp, message)
  Sidekiq.redis do |conn|
    conn.zadd(@zset, timestamp.to_s, Sidekiq.dump_json(message))
  end
end

#sizeObject



257
258
259
# File 'lib/sidekiq/api.rb', line 257

def size
  Sidekiq.redis {|c| c.zcard(@zset) }
end