From 081b0cd72848255e92b5ce44fec1db594f9b7806 Mon Sep 17 00:00:00 2001 From: Matt Conway Date: Thu, 27 Feb 2014 07:32:13 -0500 Subject: [PATCH] add pagination to worker counts, preserves backwards compatibility --- lib/qless.rb | 4 ++-- lib/qless/server.rb | 5 +++-- lib/qless/server/views/workers.erb | 1 + spec/integration/qless_spec.rb | 28 +++++++++++++++++++++++++++ spec/integration/server_spec.rb | 31 +++++++++++++++++++++--------- 5 files changed, 56 insertions(+), 13 deletions(-) diff --git a/lib/qless.rb b/lib/qless.rb index ff1aa255..c9da029a 100644 --- a/lib/qless.rb +++ b/lib/qless.rb @@ -110,8 +110,8 @@ def initialize(client) @client = client end - def counts - JSON.parse(@client.call('workers')) + def counts(offset = 0, count = 0) + JSON.parse(@client.call('workers', offset, count)) end def [](name) diff --git a/lib/qless/server.rb b/lib/qless/server.rb index 7894e3cc..5168a6a8 100755 --- a/lib/qless/server.rb +++ b/lib/qless/server.rb @@ -100,7 +100,7 @@ def tracked end def workers - client.workers.counts + client.workers.counts(0, PAGE_SIZE) end def failed @@ -242,7 +242,8 @@ def strftime(t) get '/workers/?' do erb :workers, layout: true, locals: { - title: 'Workers' + title: 'Workers', + workers: paginated(client.workers, :counts) } end diff --git a/lib/qless/server/views/workers.erb b/lib/qless/server/views/workers.erb index 0c3cfb34..23a70e43 100644 --- a/lib/qless/server/views/workers.erb +++ b/lib/qless/server/views/workers.erb @@ -12,3 +12,4 @@ <% end %> +<%= erb :_pagination, :layout => false %> diff --git a/spec/integration/qless_spec.rb b/spec/integration/qless_spec.rb index 459cdbe0..c9fae240 100644 --- a/spec/integration/qless_spec.rb +++ b/spec/integration/qless_spec.rb @@ -46,6 +46,34 @@ module Qless }) end + it 'paginates access to worker stats' do + expect(client.workers.counts).to eq({}) + + 3.times do |i| + client.worker_name = "worker-#{i}" + queue.put('Foo', {}, jid: "jid-#{i}") + queue.pop + end + + worker_names = client.workers.counts().collect {|h| h['name'] } + expect(worker_names).to eq(['worker-2', 'worker-1', 'worker-0']) + + worker_names = client.workers.counts(0, 0).collect {|h| h['name'] } + expect(worker_names).to eq(['worker-2', 'worker-1', 'worker-0']) + + worker_names = client.workers.counts(0, 1).collect {|h| h['name'] } + expect(worker_names).to eq(['worker-2']) + + worker_names = client.workers.counts(1, 1).collect {|h| h['name'] } + expect(worker_names).to eq(['worker-1']) + + worker_names = client.workers.counts(2, 1).collect {|h| h['name'] } + expect(worker_names).to eq(['worker-0']) + + worker_names = client.workers.counts(1, 2).collect {|h| h['name'] } + expect(worker_names).to eq(['worker-1', 'worker-0']) + end + it 'can deregister workers' do # Ensure there's a worker listed queue.put('Foo', {}, jid: 'jid') diff --git a/spec/integration/server_spec.rb b/spec/integration/server_spec.rb index 06f52419..cc97639c 100644 --- a/spec/integration/server_spec.rb +++ b/spec/integration/server_spec.rb @@ -45,19 +45,19 @@ module Qless end end - def build_paginated_objects + def build_paginated_objects(pattern = 'jid-%d') # build 30 since our page size is 25 so we have at least 2 pages 30.times do |i| - yield "jid-#{i + 1}" + yield pattern % (i + 1) end end # We put periods on the end of these jids so that # an assertion about "jid-1" will not pass if "jid-11" # is on the page. The jids are formatted as "#{jid}..." - def assert_page(present_jid_num, absent_jid_num) - page.should have_content("jid-#{present_jid_num}.") - page.should_not have_content("jid-#{absent_jid_num}.") + def assert_page(present_id_num, absent_id_num, pattern = 'jid-%d.') + page.should have_content(pattern % present_id_num) + page.should_not have_content(pattern % absent_id_num) end def click_pagination_link(text) @@ -66,14 +66,14 @@ def click_pagination_link(text) end end - def test_pagination(page_1_jid = 1, page_2_jid = 27) - assert_page page_1_jid, page_2_jid + def test_pagination(page_1_id = 1, page_2_id = 27, pattern = 'jid-%d.') + assert_page page_1_id, page_2_id, pattern click_pagination_link 'Next' - assert_page page_2_jid, page_1_jid + assert_page page_2_id, page_1_id, pattern click_pagination_link 'Prev' - assert_page page_1_jid, page_2_jid + assert_page page_1_id, page_2_id, pattern end it 'can paginate a group of tagged jobs' do @@ -122,6 +122,19 @@ def test_pagination(page_1_jid = 1, page_2_jid = 27) test_pagination end + it 'can paginate the workers pages' do + build_paginated_objects('worker-%d') do |worker_name| + q.client.worker_name = worker_name + q.put(Qless::Job, {}) + q.pop + end + + visit '/workers' + + # The workers page shows the workers in reverse order + test_pagination 30, 4, 'worker-%d' + end + it 'can see the root-level summary' do visit '/'