Skip to content

Commit

Permalink
Merge pull request #37 from rainkinz/resque-status
Browse files Browse the repository at this point in the history
---

Hi,

I added a killall method as I find it pretty useful. Im a little new to Resque so maybe there is a better way to do this? If so, please disregard. 

Thanks
  • Loading branch information
quirkey committed Nov 15, 2011
2 parents afa2320 + 86a0f7c commit c57fea6
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/resque/status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,21 @@ def self.status_ids(range_start = nil, range_end = nil)
end
end


# Kills <tt>num</tt> jobs within range starting with the most recent first.
# By default kills all jobs.
# Note that the same conditions apply as <tt>kill</tt>, i.e. only jobs that check
# on each iteration by calling <tt>tick</tt> or <tt>at</tt> are eligible to killed.
# @param [Numeric] range_start The optional starting range
# @param [Numeric] range_end The optional ending range
# @example killing the last 20 submitted jobs
# Resque::Status.killall(0, 19)
def self.killall(range_start = nil, range_end = nil)
status_ids(range_start, range_end).collect do |id|
kill(id)
end
end

# Kill the job at UUID on its next iteration this works by adding the UUID to a
# kill list (a.k.a. a list of jobs to be killed. Each iteration the job checks
# if it _should_ be killed by calling <tt>tick</tt> or <tt>at</tt>. If so, it raises
Expand Down
84 changes: 84 additions & 0 deletions test/test_resque-job_with_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,90 @@ class TestResqueJobWithStatus < Test::Unit::TestCase
end
end

context "killing all jobs" do
setup do
@uuid1 = KillableJob.create(:num => 100)
@uuid2 = KillableJob.create(:num => 100)

Resque::Status.killall

assert_contains Resque::Status.kill_ids, @uuid1
assert_contains Resque::Status.kill_ids, @uuid2

@payload1 = Resque.pop(:statused)
@payload2 = Resque.pop(:statused)

@performed = KillableJob.perform(*@payload1['args'])
@performed = KillableJob.perform(*@payload2['args'])

@status1 = Resque::Status.get(@uuid1)
@status2 = Resque::Status.get(@uuid2)
end

should "set the status to killed" do
assert_equal 'killed', @status1.status
assert @status1.killed?
assert !@status1.completed?

assert_equal 'killed', @status2.status
assert @status2.killed?
assert !@status2.completed?
end

should "only perform iterations up to kill" do
assert_equal 1, Resque.redis.get("#{@uuid1}:iterations").to_i
assert_equal 1, Resque.redis.get("#{@uuid2}:iterations").to_i
end

should "not persist the kill key" do
assert_does_not_contain Resque::Status.kill_ids, @uuid1
assert_does_not_contain Resque::Status.kill_ids, @uuid2
end

end

context "invoking killall jobs to kill a range" do
setup do
@uuid1 = KillableJob.create(:num => 100)
@uuid2 = KillableJob.create(:num => 100)

Resque::Status.killall(0,0) # only @uuid2 should be killed

assert_does_not_contain Resque::Status.kill_ids, @uuid1
assert_contains Resque::Status.kill_ids, @uuid2

@payload1 = Resque.pop(:statused)
@payload2 = Resque.pop(:statused)

@performed = KillableJob.perform(*@payload1['args'])
@performed = KillableJob.perform(*@payload2['args'])

@status1 = Resque::Status.get(@uuid1)
@status2 = Resque::Status.get(@uuid2)
end

should "set the status to killed" do
assert_equal 'completed', @status1.status
assert !@status1.killed?
assert @status1.completed?

assert_equal 'killed', @status2.status
assert @status2.killed?
assert !@status2.completed?
end

should "only perform iterations up to kill" do
assert_equal 100, Resque.redis.get("#{@uuid1}:iterations").to_i
assert_equal 1, Resque.redis.get("#{@uuid2}:iterations").to_i
end

should "not persist the kill key" do
assert_does_not_contain Resque::Status.kill_ids, @uuid1
assert_does_not_contain Resque::Status.kill_ids, @uuid2
end

end

context "with an invoked job" do
setup do
@job = WorkingJob.new('123', {'num' => 100})
Expand Down

0 comments on commit c57fea6

Please sign in to comment.