class Sidekiq::ProcessSet

Enumerates the set of Sidekiq processes which are actively working right now. Each process sends a heartbeat to Redis every 5 seconds so this set should be relatively accurate, barring network partitions.

Yields a Sidekiq::Process.

Public Class Methods

new(clean_plz = true) click to toggle source
# File lib/sidekiq/api.rb, line 773
def initialize(clean_plz = true)
  cleanup if clean_plz
end

Public Instance Methods

cleanup() click to toggle source

Cleans up dead processes recorded in Redis. Returns the number of processes cleaned.

# File lib/sidekiq/api.rb, line 779
def cleanup
  count = 0
  Sidekiq.redis do |conn|
    procs = conn.sscan_each("processes").to_a.sort
    heartbeats = conn.pipelined {
      procs.each do |key|
        conn.hget(key, "info")
      end
    }

    # the hash named key has an expiry of 60 seconds.
    # if it's not found, that means the process has not reported
    # in to Redis and probably died.
    to_prune = procs.select.with_index { |proc, i|
      heartbeats[i].nil?
    }
    count = conn.srem("processes", to_prune) unless to_prune.empty?
  end
  count
end
each() { |process(merge("busy" => to_i, "beat" => to_f, "quiet" => quiet, "rss" => to_i, "rtt_us" => to_i))| ... } click to toggle source
# File lib/sidekiq/api.rb, line 800
def each
  result = Sidekiq.redis { |conn|
    procs = conn.sscan_each("processes").to_a.sort

    # We're making a tradeoff here between consuming more memory instead of
    # making more roundtrips to Redis, but if you have hundreds or thousands of workers,
    # you'll be happier this way
    conn.pipelined do
      procs.each do |key|
        conn.hmget(key, "info", "busy", "beat", "quiet", "rss", "rtt_us")
      end
    end
  }

  result.each do |info, busy, at_s, quiet, rss, rtt|
    # If a process is stopped between when we query Redis for `procs` and
    # when we query for `result`, we will have an item in `result` that is
    # composed of `nil` values.
    next if info.nil?

    hash = Sidekiq.load_json(info)
    yield Process.new(hash.merge("busy" => busy.to_i,
                                 "beat" => at_s.to_f,
                                 "quiet" => quiet,
                                 "rss" => rss.to_i,
                                 "rtt_us" => rtt.to_i))
  end
end
leader() click to toggle source

Returns the identity of the current cluster leader or “” if no leader. This is a Sidekiq Enterprise feature, will always return “” in Sidekiq or Sidekiq Pro.

# File lib/sidekiq/api.rb, line 852
def leader
  @leader ||= begin
    x = Sidekiq.redis { |c| c.get("dear-leader") }
    # need a non-falsy value so we can memoize
    x ||= ""
    x
  end
end
size() click to toggle source

This method is not guaranteed accurate since it does not prune the set based on current heartbeat. each does that and ensures the set only contains Sidekiq processes which have sent a heartbeat within the last 60 seconds.

# File lib/sidekiq/api.rb, line 833
def size
  Sidekiq.redis { |conn| conn.scard("processes") }
end
total_concurrency() click to toggle source

Total number of threads available to execute jobs. For Sidekiq Enterprise customers this number (in production) must be less than or equal to your licensed concurrency.

# File lib/sidekiq/api.rb, line 840
def total_concurrency
  sum { |x| x["concurrency"].to_i }
end
total_rss()
Alias for: total_rss_in_kb
total_rss_in_kb() click to toggle source
# File lib/sidekiq/api.rb, line 844
def total_rss_in_kb
  sum { |x| x["rss"].to_i }
end
Also aliased as: total_rss