Skip to content

Commit

Permalink
allow args to be modified on retry
Browse files Browse the repository at this point in the history
  • Loading branch information
rcarver committed Apr 9, 2010
1 parent 2964c8a commit aaa4a01
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/resque/plugins/retried.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@ def seconds_until_retry
Resque.respond_to?(:enqueue_in) ? 5 : 1
end

# Modify the arguments used to requeue the job. Use this to do something
# other than try the exact same job again.
def args_for_try_again(*args)
args
end

def try_again(*args)
if Resque.respond_to?(:enqueue_in)
Resque.enqueue_in(seconds_until_retry || 0, self, *args)
Resque.enqueue_in(seconds_until_retry || 0, self, *args_for_try_again(*args))
else
sleep(seconds_until_retry) if seconds_until_retry
Resque.enqueue(self, *args)
Resque.enqueue(self, *args_for_try_again(*args))
end
end

Expand Down
26 changes: 26 additions & 0 deletions test/retried_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,30 @@ def test_job_does_not_enqueues_if_failed_and_exception_does_not_allow_retry
assert_equal(0, Resque.redis.llen("queue:testqueue").to_i, "job is NOT enqueued")
end

def test_job_args_are_maintained
thread = Thread.new { perform_job RetriedOnFailJob, 1, FooError }
assert_equal(0, Resque.redis.llen("queue:testqueue").to_i, "queue is empty")
begin
thread.join
assert(false, "Should have raised an exception")
rescue StandardError => e
assert_equal(FooError, e.class, e.message)
end
assert job = Resque.pop(:testqueue)
assert_equal [1, "FooError"], job["args"]
end

def test_job_args_can_be_modified
thread = Thread.new { perform_job RetriedOnFailWithDifferentArgsJob, 1, FooError }
assert_equal(0, Resque.redis.llen("queue:testqueue").to_i, "queue is empty")
begin
thread.join
assert(false, "Should have raised an exception")
rescue StandardError => e
assert_equal(FooError, e.class, e.message)
end
assert job = Resque.pop(:testqueue)
assert_equal [2, "FooError"], job["args"]
end

end
12 changes: 12 additions & 0 deletions test/retried_test_with_scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,16 @@ def test_job_enqueues_in_time_if_failed_and_exception_allows_retry
assert_equal([5, RetriedOnFailJob, 0, FooError], Resque.last_enqueued_in)
end

def test_job_args_can_be_modified
thread = Thread.new { perform_job RetriedOnFailWithDifferentArgsJob, 0, FooError }
assert_equal(0, Resque.redis.llen("queue:testqueue").to_i, "queue is empty")
begin
thread.join
assert(false, "Should have raised an exception")
rescue StandardError => e
assert_equal(FooError, e.class, e.message)
end
assert_equal([5, RetriedOnFailWithDifferentArgsJob, 1, FooError], Resque.last_enqueued_in)
end

end
18 changes: 18 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,24 @@ def self.lock(*args)
end
end

class RetriedOnFailWithDifferentArgsJob
extend ::Resque::Plugins::RetryOnFail
@queue = :testqueue
def self.perform(sleep_time, ex)
sleep sleep_time
raise ex
end
def self.args_for_try_again(sleep_time, ex)
[sleep_time + 1, ex]
end
def self.retried_exceptions
[FooError, BarError]
end
def self.lock(*args)
"TestLock"
end
end

class RetriedOnLockAndFailJob
extend ::Resque::Plugins::RetryOnLock
extend ::Resque::Plugins::RetryOnFail
Expand Down

0 comments on commit aaa4a01

Please sign in to comment.