Skip to content

Commit

Permalink
Merge pull request sidekiq#1402 from bwthomas/20131218_reset_individu…
Browse files Browse the repository at this point in the history
…al_stats

Allow Sidekiq::Stats#reset to reset individual stats independently
  • Loading branch information
mperham committed Dec 18, 2013
2 parents 7f589c5 + db0a333 commit fbc96b9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
8 changes: 5 additions & 3 deletions lib/sidekiq/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ def failed
Sidekiq.redis { |conn| conn.get("stat:failed") }.to_i
end

def reset
def reset(*stats)
all = %w(failed processed)
stats = stats.empty? ? all : all & stats.flatten.compact.map(&:to_s)

Sidekiq.redis do |conn|
conn.set("stat:failed", 0)
conn.set("stat:processed", 0)
stats.each { |stat| conn.set("stat:#{stat}", 0) }
end
end

Expand Down
33 changes: 31 additions & 2 deletions test/test_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,44 @@ class TestApi < Sidekiq::Test
end

describe "reset" do
it 'can reset stats' do
before do
Sidekiq.redis do |conn|
conn.set('stat:processed', 5)
conn.set('stat:failed', 10)
Sidekiq::Stats.new.reset
end
end

it 'will reset all stats by default' do
Sidekiq::Stats.new.reset
Sidekiq.redis do |conn|
assert_equal '0', conn.get('stat:processed')
assert_equal '0', conn.get('stat:failed')
end
end

it 'can reset individual stats' do
Sidekiq::Stats.new.reset('failed')
Sidekiq.redis do |conn|
assert_equal '5', conn.get('stat:processed')
assert_equal '0', conn.get('stat:failed')
end
end

it 'can accept anything that responds to #to_s' do
Sidekiq::Stats.new.reset(:failed)
Sidekiq.redis do |conn|
assert_equal '5', conn.get('stat:processed')
assert_equal '0', conn.get('stat:failed')
end
end

it 'ignores anything other than "failed" or "processed"' do
Sidekiq::Stats.new.reset((1..10).to_a, ['failed'])
Sidekiq.redis do |conn|
assert_equal '5', conn.get('stat:processed')
assert_equal '0', conn.get('stat:failed')
end
end
end

describe "queues" do
Expand Down

0 comments on commit fbc96b9

Please sign in to comment.