Skip to content

Commit

Permalink
Merge branch 'v2.1-per-job-retry' of https://github.com/Viximo/delaye…
Browse files Browse the repository at this point in the history
…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
bkeepers committed Dec 1, 2010
2 parents 3121164 + 7d7bf45 commit 02d2e14
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
6 changes: 5 additions & 1 deletion lib/delayed/backend/base.rb
Expand Up @@ -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
Expand Down
17 changes: 16 additions & 1 deletion lib/delayed/backend/shared_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
6 changes: 5 additions & 1 deletion lib/delayed/worker.rb
Expand Up @@ -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
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions spec/sample_jobs.rb
Expand Up @@ -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
Expand Down

0 comments on commit 02d2e14

Please sign in to comment.