Class: Sidekiq::WorkSet

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

Overview

The WorkSet stores the work being done by this Sidekiq cluster. It tracks the process and thread working on each job.

WARNING WARNING WARNING

This is live data that can change every millisecond. If you call #size => 5 and then expect #each to be called 5 times, you’re going to have a bad time.

works = Sidekiq::WorkSet.new
works.size => 2
works.each do |process_id, thread_id, work|
  # process_id is a unique identifier per Sidekiq process
  # thread_id is a unique identifier per thread
  # work is a Hash which looks like:
  # { 'queue' => name, 'run_at' => timestamp, 'payload' => job_hash }
  # run_at is an epoch Integer.
end

Instance Method Summary collapse

Instance Method Details

#each(&block) ⇒ Object



1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
# File 'lib/sidekiq/api.rb', line 1125

def each(&block)
  results = []
  procs = nil
  all_works = nil

  Sidekiq.redis do |conn|
    procs = conn.sscan("processes").to_a.sort
    all_works = conn.pipelined do |pipeline|
      procs.each do |key|
        pipeline.hgetall("#{key}:work")
      end
    end
  end

  procs.zip(all_works).each do |key, workers|
    workers.each_pair do |tid, json|
      results << [key, tid, Sidekiq::Work.new(key, tid, Sidekiq.load_json(json))] unless json.empty?
    end
  end

  results.sort_by { |(_, _, hsh)| hsh.raw("run_at") }.each(&block)
end

#find_work_by_jid(jid) ⇒ Sidekiq::Work

Find the work which represents a job with the given JID. *This is a slow O(n) operation*. Do not use for app logic.

Parameters:

  • jid (String)

    the job identifier

Returns:



1175
1176
1177
1178
1179
1180
1181
# File 'lib/sidekiq/api.rb', line 1175

def find_work_by_jid(jid)
  each do |_process_id, _thread_id, work|
    job = work.job
    return work if job.jid == jid
  end
  nil
end

#sizeObject

Note that #size is only as accurate as Sidekiq’s heartbeat, which happens every 5 seconds. It is NOT real-time.

Not very efficient if you have lots of Sidekiq processes but the alternative is a global counter which can easily get out of sync with crashy processes.



1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
# File 'lib/sidekiq/api.rb', line 1154

def size
  Sidekiq.redis do |conn|
    procs = conn.sscan("processes").to_a
    if procs.empty?
      0
    else
      conn.pipelined { |pipeline|
        procs.each do |key|
          pipeline.hget(key, "busy")
        end
      }.sum(&:to_i)
    end
  end
end