Class: Sidekiq::JobSet

Inherits:
SortedSet show all
Defined in:
lib/sidekiq/api.rb

Direct Known Subclasses

DeadSet, RetrySet, ScheduledSet

Instance Attribute Summary

Attributes inherited from SortedSet

#name

Instance Method Summary collapse

Methods inherited from SortedSet

#clear, #initialize, #size

Constructor Details

This class inherits a constructor from Sidekiq::SortedSet

Instance Method Details

#delete_by_jid(score, jid) ⇒ Object Also known as: delete


615
616
617
618
619
620
621
622
623
624
625
626
627
628
# File 'lib/sidekiq/api.rb', line 615

def delete_by_jid(score, jid)
  Sidekiq.redis do |conn|
    elements = conn.zrangebyscore(name, score, score)
    elements.each do |element|
      message = Sidekiq.load_json(element)
      if message["jid"] == jid
        ret = conn.zrem(name, element)
        @_size -= 1 if ret
        break ret
      end
      false
    end
  end
end

#delete_by_value(name, value) ⇒ Object


607
608
609
610
611
612
613
# File 'lib/sidekiq/api.rb', line 607

def delete_by_value(name, value)
  Sidekiq.redis do |conn|
    ret = conn.zrem(name, value)
    @_size -= 1 if ret
    ret
  end
end

#eachObject


561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
# File 'lib/sidekiq/api.rb', line 561

def each
  initial_size = @_size
  offset_size = 0
  page = -1
  page_size = 50

  while true do
    range_start = page * page_size + offset_size
    range_end   = range_start + page_size - 1
    elements = Sidekiq.redis do |conn|
      conn.zrange name, range_start, range_end, with_scores: true
    end
    break if elements.empty?
    page -= 1
    elements.reverse.each do |element, score|
      yield SortedEntry.new(self, score, element)
    end
    offset_size = initial_size - @_size
  end
end

#fetch(score, jid = nil) ⇒ Object


582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
# File 'lib/sidekiq/api.rb', line 582

def fetch(score, jid = nil)
  elements = Sidekiq.redis do |conn|
    conn.zrangebyscore(name, 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

Find the job with the given JID within this sorted set.

This is a slow, inefficient operation. Do not use under normal conditions. Sidekiq Pro contains a faster version.


603
604
605
# File 'lib/sidekiq/api.rb', line 603

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

#schedule(timestamp, message) ⇒ Object


555
556
557
558
559
# File 'lib/sidekiq/api.rb', line 555

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