lib/resque/status.rb in resque-status-0.2.0 vs lib/resque/status.rb in resque-status-0.2.1
- old
+ new
@@ -5,11 +5,11 @@
module Resque
# Resque::Status is a Hash object that has helper methods for dealing with
# the common status attributes. It also has a number of class methods for
# creating/updating/retrieving status objects from Redis
class Status < Hash
- VERSION = '0.2.0'
+ VERSION = '0.2.1'
extend Resque::Helpers
# Create a status, generating a new UUID, passing the message to the status
# Returns the UUID of the new status.
@@ -36,12 +36,14 @@
redis.expire(status_key(uuid), expire_in)
end
val
end
- def self.clear
- status_ids.each do |id|
+ # clear statuses from redis passing an optional range. See `statuses` for info
+ # about ranges
+ def self.clear(range_start = nil, range_end = nil)
+ status_ids(range_start, range_end).each do |id|
redis.del(status_key(id))
redis.zrem(set_key, id)
end
end
@@ -62,18 +64,18 @@
# By default returns the entire set.
# @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)
+ 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(range_start=nil, range_end=nil)
+ 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
@@ -83,12 +85,10 @@
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
@@ -198,10 +198,9 @@
# and <tt>total</tt>
def pct_complete
case status
when 'completed' then 100
when 'queued' then 0
- when 'failed' then 100
else
t = (total == 0 || total.nil?) ? 1 : total
(((num || 0).to_f / t.to_f) * 100).to_i
end
end