Class: Sidekiq::JobSet
Overview
Base class for all sorted sets which contain jobs, e.g. scheduled, retry and dead. Sidekiq Pro and Enterprise add additional sorted sets which do not contain job data, e.g. Batches.
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from SortedSet
Instance Method Summary collapse
- #each ⇒ Object
-
#fetch(score, jid = nil) ⇒ Array<SortedEntry>
Fetch jobs that match a given time or Range.
-
#find_job(jid) ⇒ SortedEntry
Find the job with the given JID within this sorted set.
-
#schedule(timestamp, job) ⇒ Object
Add a job with the associated timestamp to this set.
Methods inherited from SortedSet
Instance Method Details
#each ⇒ Object
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 |
# File 'lib/sidekiq/api.rb', line 645 def each initial_size = @_size offset_size = 0 page = -1 page_size = 50 loop do range_start = page * page_size + offset_size range_end = range_start + page_size - 1 elements = Sidekiq.redis { |conn| conn.zrange name, range_start, range_end, withscores: true } 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) ⇒ Array<SortedEntry>
Fetch jobs that match a given time or Range. Job ID is an optional second argument.
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 |
# File 'lib/sidekiq/api.rb', line 673 def fetch(score, jid = nil) begin_score, end_score = if score.is_a?(Range) [score.first, score.last] else [score, score] end elements = Sidekiq.redis { |conn| conn.zrangebyscore(name, begin_score, end_score, withscores: true) } elements.each_with_object([]) do |element, result| data, job_score = element entry = SortedEntry.new(self, job_score, data) result << entry if jid.nil? || entry.jid == jid end end |
#find_job(jid) ⇒ SortedEntry
Find the job with the given JID within this sorted set. *This is a slow O(n) operation*. Do not use for app logic.
698 699 700 701 702 703 704 705 706 707 |
# File 'lib/sidekiq/api.rb', line 698 def find_job(jid) Sidekiq.redis do |conn| conn.zscan(name, match: "*#{jid}*", count: 100) do |entry, score| job = JSON.parse(entry) matched = job["jid"] == jid return SortedEntry.new(self, score, entry) if matched end end nil end |