Class: Sidekiq::Stats
Overview
Retrieve runtime statistics from Redis regarding this Sidekiq cluster.
stat = Sidekiq::Stats.new
stat.processed
Defined Under Namespace
Classes: History, QueueSummary
Instance Method Summary
collapse
Methods included from ApiUtils
#calculate_latency
Constructor Details
#initialize ⇒ Stats
Returns a new instance of Stats.
51
52
53
|
# File 'lib/sidekiq/api.rb', line 51
def initialize
fetch_stats_fast!
end
|
Instance Method Details
#dead_size ⇒ Object
71
72
73
|
# File 'lib/sidekiq/api.rb', line 71
def dead_size
stat :dead_size
end
|
#default_queue_latency ⇒ Object
87
88
89
|
# File 'lib/sidekiq/api.rb', line 87
def default_queue_latency
stat :default_queue_latency
end
|
#enqueued ⇒ Object
75
76
77
|
# File 'lib/sidekiq/api.rb', line 75
def enqueued
stat :enqueued
end
|
#failed ⇒ Object
59
60
61
|
# File 'lib/sidekiq/api.rb', line 59
def failed
stat :failed
end
|
#fetch_stats! ⇒ 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.
209
210
211
212
|
# File 'lib/sidekiq/api.rb', line 209
def fetch_stats!
fetch_stats_fast!
fetch_stats_slow!
end
|
#fetch_stats_fast! ⇒ 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.
O(1) redis calls
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
# File 'lib/sidekiq/api.rb', line 144
def fetch_stats_fast!
pipe1_res = Sidekiq.redis { |conn|
conn.pipelined do |pipeline|
pipeline.get("stat:processed")
pipeline.get("stat:failed")
pipeline.zcard("schedule")
pipeline.zcard("retry")
pipeline.zcard("dead")
pipeline.scard("processes")
pipeline.lindex("queue:default", -1)
end
}
default_queue_latency = if (entry = pipe1_res[6])
job = begin
Sidekiq.load_json(entry)
rescue
{}
end
calculate_latency(job)
else
0.0
end
@stats = {
processed: pipe1_res[0].to_i,
failed: pipe1_res[1].to_i,
scheduled_size: pipe1_res[2],
retry_size: pipe1_res[3],
dead_size: pipe1_res[4],
processes_size: pipe1_res[5],
default_queue_latency: default_queue_latency
}
end
|
#fetch_stats_slow! ⇒ 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.
O(number of processes + number of queues) redis calls
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
# File 'lib/sidekiq/api.rb', line 183
def fetch_stats_slow!
processes = Sidekiq.redis { |conn|
conn.sscan("processes").to_a
}
queues = Sidekiq.redis { |conn|
conn.sscan("queues").to_a
}
pipe2_res = Sidekiq.redis { |conn|
conn.pipelined do |pipeline|
processes.each { |key| pipeline.hget(key, "busy") }
queues.each { |queue| pipeline.llen("queue:#{queue}") }
end
}
s = processes.size
workers_size = pipe2_res[0...s].sum(&:to_i)
enqueued = pipe2_res[s..].sum(&:to_i)
@stats[:workers_size] = workers_size
@stats[:enqueued] = enqueued
@stats
end
|
#processed ⇒ Object
55
56
57
|
# File 'lib/sidekiq/api.rb', line 55
def processed
stat :processed
end
|
#processes_size ⇒ Object
79
80
81
|
# File 'lib/sidekiq/api.rb', line 79
def processes_size
stat :processes_size
end
|
#queue_summaries ⇒ Array<QueueSummary>
More detailed information about each queue: name, size, latency, paused status
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
# File 'lib/sidekiq/api.rb', line 109
def queue_summaries
Sidekiq.redis do |conn|
queues = conn.sscan("queues").to_a
return [] if queues.empty?
results = conn.pipelined { |pipeline|
queues.each do |queue|
pipeline.llen("queue:#{queue}")
pipeline.lindex("queue:#{queue}", -1)
pipeline.sismember("paused", queue)
end
}
queue_summaries = []
queues.each_with_index do |name, idx|
size = results[idx * 3]
last_item = results[idx * 3 + 1]
paused = results[idx * 3 + 2] > 0
latency = if last_item
job = Sidekiq.load_json(last_item)
calculate_latency(job)
else
0.0
end
queue_summaries << QueueSummary.new(name:, size:, latency:, paused:)
end
queue_summaries.sort_by { |qd| -qd.size }
end
end
|
#queues ⇒ Hash{String => Integer}
Returns a hash of queue names to their lengths.
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/sidekiq/api.rb', line 92
def queues
Sidekiq.redis do |conn|
queues = conn.sscan("queues").to_a
lengths = conn.pipelined { |pipeline|
queues.each do |queue|
pipeline.llen("queue:#{queue}")
end
}
array_of_arrays = queues.zip(lengths).sort_by { |_, size| -size }
array_of_arrays.to_h
end
end
|
#reset(*stats) ⇒ 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.
215
216
217
218
219
220
221
222
223
224
225
226
227
|
# File 'lib/sidekiq/api.rb', line 215
def reset(*stats)
all = %w[failed processed]
stats = stats.empty? ? all : all & stats.flatten.compact.map(&:to_s)
mset_args = []
stats.each do |stat|
mset_args << "stat:#{stat}"
mset_args << 0
end
Sidekiq.redis do |conn|
conn.mset(*mset_args)
end
end
|
#retry_size ⇒ Object
67
68
69
|
# File 'lib/sidekiq/api.rb', line 67
def retry_size
stat :retry_size
end
|
#scheduled_size ⇒ Object
63
64
65
|
# File 'lib/sidekiq/api.rb', line 63
def scheduled_size
stat :scheduled_size
end
|
#workers_size ⇒ Object
83
84
85
|
# File 'lib/sidekiq/api.rb', line 83
def workers_size
stat :workers_size
end
|