Class: Sidekiq::ProcessSet
- Inherits:
-
Object
- Object
- Sidekiq::ProcessSet
- Includes:
- Enumerable
- Defined in:
- lib/sidekiq/api.rb
Overview
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.
Class Method Summary collapse
Instance Method Summary collapse
-
#cleanup ⇒ Object
private
Cleans up dead processes recorded in Redis.
- #each ⇒ Object
-
#initialize(clean_plz = true) ⇒ ProcessSet
constructor
private
:nodoc:.
-
#leader ⇒ String
Returns the identity of the current cluster leader or “” if no leader.
-
#size ⇒ Integer
This method is not guaranteed accurate since it does not prune the set based on current heartbeat.
-
#total_concurrency ⇒ Integer
Total number of threads available to execute jobs.
-
#total_rss_in_kb ⇒ Integer
(also: #total_rss)
Total amount of RSS memory consumed by Sidekiq processes.
Constructor Details
#initialize(clean_plz = true) ⇒ ProcessSet
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
:nodoc:
1006 1007 1008 |
# File 'lib/sidekiq/api.rb', line 1006 def initialize(clean_plz = true) cleanup if clean_plz end |
Class Method Details
.[](identity) ⇒ Object
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 |
# File 'lib/sidekiq/api.rb', line 986 def self.[](identity) exists, (info, busy, beat, quiet, rss, rtt_us) = Sidekiq.redis { |conn| conn.multi { |transaction| transaction.sismember("processes", identity) transaction.hmget(identity, "info", "busy", "beat", "quiet", "rss", "rtt_us") } } return nil if exists == 0 || info.nil? hash = Sidekiq.load_json(info) Process.new(hash.merge("busy" => busy.to_i, "beat" => beat.to_f, "quiet" => quiet, "rss" => rss.to_i, "rtt_us" => rtt_us.to_i)) end |
Instance Method Details
#cleanup ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Cleans up dead processes recorded in Redis. Returns the number of processes cleaned. :nodoc:
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 |
# File 'lib/sidekiq/api.rb', line 1014 def cleanup # dont run cleanup more than once per minute return 0 unless Sidekiq.redis { |conn| conn.set("process_cleanup", "1", "NX", "EX", "60") } count = 0 Sidekiq.redis do |conn| procs = conn.sscan("processes").to_a heartbeats = conn.pipelined { |pipeline| procs.each do |key| pipeline.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 ⇒ Object
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 |
# File 'lib/sidekiq/api.rb', line 1038 def each result = Sidekiq.redis { |conn| procs = conn.sscan("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 |pipeline| procs.each do |key| pipeline.hmget(key, "info", "concurrency", "busy", "beat", "quiet", "rss", "rtt_us") end end } result.each do |info, concurrency, busy, beat, quiet, rss, rtt_us| # 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("concurrency" => concurrency.to_i, "busy" => busy.to_i, "beat" => beat.to_f, "quiet" => quiet, "rss" => rss.to_i, "rtt_us" => rtt_us.to_i)) end end |
#leader ⇒ String
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.
1096 1097 1098 1099 1100 1101 1102 1103 |
# File 'lib/sidekiq/api.rb', line 1096 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 ⇒ Integer
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.
1073 1074 1075 |
# File 'lib/sidekiq/api.rb', line 1073 def size Sidekiq.redis { |conn| conn.scard("processes") } end |
#total_concurrency ⇒ Integer
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.
1081 1082 1083 |
# File 'lib/sidekiq/api.rb', line 1081 def total_concurrency sum { |x| x["concurrency"].to_i } end |
#total_rss_in_kb ⇒ Integer Also known as: total_rss
Returns total amount of RSS memory consumed by Sidekiq processes.
1086 1087 1088 |
# File 'lib/sidekiq/api.rb', line 1086 def total_rss_in_kb sum { |x| x["rss"].to_i } end |