mirror of
https://github.com/mperham/sidekiq.git
synced 2022-11-09 13:52:34 -05:00
* Revert instance methods
* Add better test coverage
This commit is contained in:
parent
4dcbec2ba2
commit
187e6e90bd
2 changed files with 44 additions and 27 deletions
|
@ -3,33 +3,33 @@ require 'sidekiq'
|
||||||
module Sidekiq
|
module Sidekiq
|
||||||
|
|
||||||
class Stats
|
class Stats
|
||||||
class << self
|
def processed
|
||||||
def processed
|
count = Sidekiq.redis do |conn|
|
||||||
Sidekiq.redis do |conn|
|
conn.get("stat:processed")
|
||||||
conn.get("stat:processed")
|
end
|
||||||
end || 0
|
count.nil? ? 0 : count.to_i
|
||||||
end
|
end
|
||||||
|
|
||||||
def failed
|
def failed
|
||||||
Sidekiq.redis do |conn|
|
count = Sidekiq.redis do |conn|
|
||||||
conn.get("stat:failed")
|
conn.get("stat:failed")
|
||||||
end || 0
|
end
|
||||||
end
|
count.nil? ? 0 : count.to_i
|
||||||
|
end
|
||||||
|
|
||||||
def queues
|
def queues
|
||||||
Sidekiq.redis do |conn|
|
Sidekiq.redis do |conn|
|
||||||
queues = conn.smembers('queues')
|
queues = conn.smembers('queues')
|
||||||
|
|
||||||
queues.inject({}) do |memo, queue|
|
queues.inject({}) do |memo, queue|
|
||||||
memo[queue] = conn.llen("queue:#{queue}")
|
memo[queue] = conn.llen("queue:#{queue}")
|
||||||
memo
|
memo
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def enqueued
|
def enqueued
|
||||||
queues.values.inject(&:+) || 0
|
queues.values.inject(&:+) || 0
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -8,20 +8,34 @@ class TestApi < MiniTest::Unit::TestCase
|
||||||
|
|
||||||
describe "processed" do
|
describe "processed" do
|
||||||
it "is initially zero" do
|
it "is initially zero" do
|
||||||
assert_equal 0, Sidekiq::Stats.processed
|
s = Sidekiq::Stats.new
|
||||||
|
assert_equal 0, s.processed
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns number of processed jobs" do
|
||||||
|
Sidekiq.redis { |conn| conn.set("stat:processed", 5) }
|
||||||
|
s = Sidekiq::Stats.new
|
||||||
|
assert_equal 5, s.processed
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "failed" do
|
describe "failed" do
|
||||||
it "is initially zero" do
|
it "is initially zero" do
|
||||||
assert_equal 0, Sidekiq::Stats.processed
|
s = Sidekiq::Stats.new
|
||||||
|
assert_equal 0, s.processed
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns number of failed jobs" do
|
||||||
|
Sidekiq.redis { |conn| conn.set("stat:failed", 5) }
|
||||||
|
s = Sidekiq::Stats.new
|
||||||
|
assert_equal 5, s.failed
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "queues" do
|
describe "queues" do
|
||||||
it "is initially empty" do
|
it "is initially empty" do
|
||||||
s = Sidekiq::Stats.new
|
s = Sidekiq::Stats.new
|
||||||
assert_equal 0, Sidekiq::Stats.queues.size
|
assert_equal 0, s.queues.size
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns a hash of queue and size" do
|
it "returns a hash of queue and size" do
|
||||||
|
@ -33,13 +47,15 @@ class TestApi < MiniTest::Unit::TestCase
|
||||||
conn.sadd 'queues', 'bar'
|
conn.sadd 'queues', 'bar'
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_equal ({ "foo" => 1, "bar" => 3 }), Sidekiq::Stats.queues
|
s = Sidekiq::Stats.new
|
||||||
|
assert_equal ({ "foo" => 1, "bar" => 3 }), s.queues
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "enqueued" do
|
describe "enqueued" do
|
||||||
it "is initially empty" do
|
it "is initially empty" do
|
||||||
assert_equal 0, Sidekiq::Stats.enqueued
|
s = Sidekiq::Stats.new
|
||||||
|
assert_equal 0, s.enqueued
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns total enqueued jobs" do
|
it "returns total enqueued jobs" do
|
||||||
|
@ -51,7 +67,8 @@ class TestApi < MiniTest::Unit::TestCase
|
||||||
conn.sadd 'queues', 'bar'
|
conn.sadd 'queues', 'bar'
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_equal 4, Sidekiq::Stats.enqueued
|
s = Sidekiq::Stats.new
|
||||||
|
assert_equal 4, s.enqueued
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue