Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'v2.1-per-job-retry' of https://github.com/Viximo/delaye…

…d_job

* 'v2.1-per-job-retry' of https://github.com/Viximo/delayed_job:
  Job payloads may implement #max_attempts to control how many times they are retried
  • Loading branch information...
commit 02d2e1437330fa994f3d52630106b5f38d8d0f19 2 parents 3121164 + 7d7bf45
Brandon Keepers bkeepers authored
6 lib/delayed/backend/base.rb
View
@@ -109,7 +109,11 @@ def reschedule_at
payload_object.reschedule_at(self.class.db_time_now, attempts) :
self.class.db_time_now + (attempts ** 4) + 5
end
-
+
+ def max_attempts
+ payload_object.max_attempts if payload_object.respond_to?(:max_attempts)
+ end
+
protected
def set_default_run_at
17 lib/delayed/backend/shared_spec.rb
View
@@ -271,6 +271,21 @@ def create_job(opts = {})
@job.id.should_not be_nil
end
end
+
+ context "max_attempts" do
+ before(:each) do
+ @job = described_class.enqueue SimpleJob.new
+ end
+
+ it 'should not be defined' do
+ @job.max_attempts.should be_nil
+ end
+
+ it 'should use the max_retries value on the payload when defined' do
+ @job.payload_object.stub!(:max_attempts).and_return(99)
+ @job.max_attempts.should == 99
+ end
+ end
describe "yaml serialization" do
it "should reload changed attributes" do
@@ -382,7 +397,7 @@ def create_job(opts = {})
it "should run that hook" do
@job.payload_object.should_receive :failure
- Delayed::Worker.max_attempts.times { worker.reschedule(@job) }
+ worker.reschedule(@job)
end
end
6 lib/delayed/worker.rb
View
@@ -132,7 +132,7 @@ def run(job)
# Reschedule the job in the future (when a job fails).
# Uses an exponential scale depending on the number of failed attempts.
def reschedule(job, time = nil)
- if (job.attempts += 1) < self.class.max_attempts
+ if (job.attempts += 1) < max_attempts(job)
time ||= job.reschedule_at
job.run_at = time
job.unlock
@@ -157,6 +157,10 @@ def say(text, level = Logger::INFO)
logger.add level, "#{Time.now.strftime('%FT%T%z')}: #{text}" if logger
end
+ def max_attempts(job)
+ job.max_attempts || self.class.max_attempts
+ end
+
protected
def handle_failed_job(job, error)
4 spec/sample_jobs.rb
View
@@ -25,8 +25,8 @@ def perform; sleep 250; end
end
class OnPermanentFailureJob < SimpleJob
- def failure
- end
+ def failure; end
+ def max_attempts; 1; end
end
module M
Please sign in to comment.
Something went wrong with that request. Please try again.