Skip to content

Commit

Permalink
Added pagination to statuses (resque-web)
Browse files Browse the repository at this point in the history
  • Loading branch information
maimonetti committed Oct 7, 2010
1 parent fcb2430 commit ebee28a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
4 changes: 4 additions & 0 deletions lib/resque/server/views/statuses.erb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
<% end %>
</table>

<% unless @statuses.empty? %>
<%= partial :next_more, :start => @start, :size => @size %>
<% end %>
<%= poll %>

<script type="text/javascript" charset="utf-8">
Expand Down
32 changes: 28 additions & 4 deletions lib/resque/status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,42 @@ def self.logger(uuid, options = {})
Redisk.redis = redis
Redisk::Logger.new(logger_key(uuid), options)
end

def self.count
redis.zcard(set_key)
end

# Return <tt>num</tt> Resque::Status objects in reverse chronological order.
# By default returns the entire set.
def self.statuses(num = -1)
status_ids(num).collect do |id|
# @param [Numeric] range_start The optional starting range
# @param [Numeric] range_end The optional ending range
# @example retuning the last 20 statuses
# Resque::Status.statuses(0, 20)
def self.statuses(range_start=nil, range_end=nil)
status_ids(range_start, range_end).collect do |id|
get(id)
end.compact
end

# Return the <tt>num</tt> most recent status/job UUIDs in reverse chronological order.
def self.status_ids(num = -1)
redis.zrevrange(set_key, 0, num) || []
def self.status_ids(range_start=nil, range_end=nil)
unless range_end && range_start
# Because we want a reverse chronological order, we need to get a range starting
# by the higest negative number.
redis.zrevrange(set_key, 0, -1) || []
else
# Because we want a reverse chronological order, we need to get a range starting
# by the higest negative number. The ordering is transparent from the API user's
# perspective so we need to convert the passed params
if range_start == 0
range_start = -1
else
range_end -= 1
end


(redis.zrevrange(set_key, -(range_end.abs), -(range_start.abs)) || []).reverse
end
end

# Kill the job at UUID on its next iteration this works by adding the UUID to a
Expand Down
5 changes: 4 additions & 1 deletion lib/resque/status_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ module StatusServer
def self.registered(app)

app.get '/statuses' do
@statuses = Resque::Status.statuses
@start = params[:start].to_i
@end = @start + (params[:per_page] || 50)
@statuses = Resque::Status.statuses(@start, @end)
@size = @statuses.size
status_view(:statuses)
end

Expand Down
23 changes: 23 additions & 0 deletions test/test_resque-status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,21 @@ class TestResqueStatus < Test::Unit::TestCase
end

context ".status_ids" do

setup do
@uuids = []
30.times{ Resque::Status.create }
end

should "return an array of job ids" do
assert Resque::Status.status_ids.is_a?(Array)
assert_equal 32, Resque::Status.status_ids.size # 30 + 2
end

should "let you paginate through the statuses" do
assert_equal Resque::Status.status_ids.reverse[0, 10], Resque::Status.status_ids(0, 10)
assert_equal Resque::Status.status_ids.reverse[9, 10], Resque::Status.status_ids(10, 20)
# assert_equal Resque::Status.status_ids.reverse[0, 10], Resque::Status.status_ids(0, 10)
end
end

Expand All @@ -102,6 +115,16 @@ class TestResqueStatus < Test::Unit::TestCase

end

# context ".count" do
#
# should "return a count of statuses" do
# statuses = Resque::Status.statuses
# assert_equal 2, statuses.size
# assert_equal statuses.size, Resque::Status.count
# end
#
# end

context ".logger" do
setup do
@logger = Resque::Status.logger(@uuid)
Expand Down

0 comments on commit ebee28a

Please sign in to comment.