Skip to content

Commit

Permalink
Load stats with pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
ismaelga committed Jan 16, 2015
1 parent 483a376 commit 8f36e26
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 14 deletions.
78 changes: 64 additions & 14 deletions lib/sidekiq/api.rb
Expand Up @@ -4,11 +4,73 @@
module Sidekiq
class Stats
def processed
Sidekiq.redis { |conn| conn.get("stat:processed") }.to_i
stat 'processed'
end

def failed
Sidekiq.redis { |conn| conn.get("stat:failed") }.to_i
stat 'failed'
end

def scheduled_size
stat 'scheduled_size'
end

def retry_size
stat 'retry_size'
end

def dead_size
stat 'dead_size'
end

def stats(*only)
all = %w(processed failed queues enqueued scheduled_size retry_size dead_size)
metrics = only.any? ? only : all

if @all_stats
@all_stats.slice(*metrics)
else
if metrics == all
@all_stats = load_stats(*all)
else
load_stats(*metrics)
end
end
end

def fetch_stats!
stats
end

def stat(s)
stats(s)[s]
end

def load_stats(*metrics)
read_pipelined = %w(processed failed scheduled_size retry_size dead_size) & metrics

loaded_stats = {}
if read_pipelined.any?
results = Sidekiq.redis do |conn|
conn.pipelined do
read_pipelined.each do |key|
case key
when 'processed' then conn.get('stat:processed')
when 'failed' then conn.get('stat:failed')
when 'scheduled_size' then conn.zcard('schedule')
when 'retry_size' then conn.zcard('retry')
when 'dead_size' then conn.zcard('dead')
end
end
end
end
read_pipelined.zip(results).each {|metric, v| loaded_stats[metric] = v.to_i }
end

(metrics - read_pipelined).each do |metric|
loaded_stats[metric] = public_send(metric)
end
loaded_stats
end

def reset(*stats)
Expand Down Expand Up @@ -50,18 +112,6 @@ def enqueued
queues.values.inject(&:+) || 0
end

def scheduled_size
Sidekiq.redis {|c| c.zcard('schedule') }
end

def retry_size
Sidekiq.redis {|c| c.zcard('retry') }
end

def dead_size
Sidekiq.redis {|c| c.zcard('dead') }
end

class History
def initialize(days_previous, start_date = nil)
@days_previous = days_previous
Expand Down
1 change: 1 addition & 0 deletions lib/sidekiq/web.rb
Expand Up @@ -210,6 +210,7 @@ def custom_tabs

get '/stats' do
sidekiq_stats = Sidekiq::Stats.new
sidekiq_stats.fetch_stats!
queue = Sidekiq::Queue.new
redis_stats = redis_info.select { |k, v| REDIS_KEYS.include? k }

Expand Down

0 comments on commit 8f36e26

Please sign in to comment.