Class: Sidekiq::SortedSet
- Inherits:
-
Object
- Object
- Sidekiq::SortedSet
- Includes:
- Enumerable
- Defined in:
- lib/sidekiq/api.rb
Direct Known Subclasses
Instance Method Summary collapse
- #clear ⇒ Object
- #delete(score, jid = nil) ⇒ Object
- #each(&block) ⇒ Object
- #fetch(score, jid = nil) ⇒ Object
- #find_job(jid) ⇒ Object
-
#initialize(name) ⇒ SortedSet
constructor
A new instance of SortedSet.
- #schedule(timestamp, message) ⇒ Object
- #size ⇒ Object
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
#clear ⇒ Object
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| = Sidekiq.load_json(element) if ["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 |