diff --git a/lib/sidekiq/web.rb b/lib/sidekiq/web.rb index d0bbefdf..f6e0acac 100644 --- a/lib/sidekiq/web.rb +++ b/lib/sidekiq/web.rb @@ -225,6 +225,54 @@ module Sidekiq }) end + get '/stats' do + sidekiq_stats = Sidekiq::Stats.new + queues = Sidekiq::Queue.all + + content_type :json + Sidekiq.dump_json( + processed: sidekiq_stats.processed, + failed: sidekiq_stats.failed, + busy: workers_size, + enqueued: sidekiq_stats.enqueued, + scheduled: sidekiq_stats.scheduled_size, + retries: sidekiq_stats.retry_size, + dead: sidekiq_stats.dead_size, + queues: { + count: queues.size, + href: "#{uri}/queues" + } + ) + end + + get '/stats/queues' do + queues = Sidekiq::Queue.all + + queue_sizes = queues.reduce({}) do |ret, queue| + ret[queue.name] = { + depth: queue.size, + href: "#{uri}/#{queue.name}" + } + ret + end + + content_type :json + Sidekiq.dump_json( + queue_sizes + ) + end + + get '/stats/queues/:name' do + queue = Sidekiq::Queue.new(params[:name]) + + content_type :json + Sidekiq.dump_json( + name: queue.name, + depth: queue.size, + href: uri + ) + end + private def retry_or_delete_or_kill job, params diff --git a/test/test_web.rb b/test/test_web.rb index 9ab3c38a..5be4a66b 100644 --- a/test/test_web.rb +++ b/test/test_web.rb @@ -355,6 +355,74 @@ class TestWeb < Sidekiq::Test describe 'stats' do include Sidekiq::Util + before do + Sidekiq.redis do |conn| + conn.set("stat:processed", 5) + conn.set("stat:failed", 2) + conn.sadd("queues", "default") + end + 2.times { add_retry } + 3.times { add_scheduled } + 4.times { add_worker } + + get '/stats' + @response = Sidekiq.load_json(last_response.body) + end + + it 'reports processed' do + assert_equal 5, @response["processed"] + end + + it 'reports failed' do + assert_equal 2, @response["failed"] + end + + it 'reports busy' do + assert_equal 4, @response["busy"] + end + + it 'reports retries' do + assert_equal 2, @response["retries"] + end + + it 'reports scheduled' do + assert_equal 3, @response["scheduled"] + end + + it 'reports queue count' do + assert_equal 1, @response["queues"]["count"] + end + + describe 'queues' do + before do + get '/stats/queues' + @response = Sidekiq.load_json(last_response.body) + end + + it 'reports the queue depth' do + assert_equal 0, @response["default"]["depth"] + end + end + + describe 'queues/:name' do + before do + get '/stats/queues/default' + @response = Sidekiq.load_json(last_response.body) + end + + it 'reports the queue depth' do + assert_equal 0, @response["depth"] + end + + it 'reports the queue name' do + assert_equal 'default', @response["name"] + end + end + end + + describe 'dashboard/stats' do + include Sidekiq::Util + before do Sidekiq.redis do |conn| conn.set("stat:processed", 5)