Skip to content

Commit

Permalink
Clean up some logic for rescheduling
Browse files Browse the repository at this point in the history
  • Loading branch information
bkeepers committed Mar 15, 2010
1 parent 426a3b9 commit 9018ba1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 14 deletions.
11 changes: 3 additions & 8 deletions lib/delayed/job.rb
Expand Up @@ -84,22 +84,17 @@ def payload_object=(object)
# Reschedule the job in the future (when a job fails).
# Uses an exponential scale depending on the number of failed attempts.
def reschedule(message, backtrace = [], time = nil)
self.last_error = message + "\n" + backtrace.join("\n")

if (self.attempts += 1) < max_attempts
time ||= Job.db_time_now + (attempts ** 4) + 5

self.run_at = time
self.last_error = message + "\n" + backtrace.join("\n")
self.unlock
save!
else
logger.info "* [JOB] PERMANENTLY removing #{self.name} because of #{attempts} consecutive failures."
if destroy_failed_jobs
destroy
else
self.failed_at = Delayed::Job.db_time_now
self.last_error = message + "\n" + backtrace.join("\n")
save!
end
destroy_failed_jobs ? destroy : update_attribute(:failed_at, Delayed::Job.db_time_now)
end
end

Expand Down
9 changes: 3 additions & 6 deletions spec/job_spec.rb
Expand Up @@ -114,14 +114,11 @@ def perform; @@runs += 1; end
it "should record last_error when destroy_failed_jobs = false, max_attempts = 1" do
Delayed::Job.destroy_failed_jobs = false
Delayed::Job::max_attempts = 1
Delayed::Job.enqueue ErrorJob.new
Delayed::Job.work_off(1)

job = Delayed::Job.find(:first)

job = Delayed::Job.enqueue ErrorJob.new
job.run(1)
job.reload
job.last_error.should =~ /did not work/
job.last_error.should =~ /job_spec.rb:10:in `perform'/
job.last_error.should =~ /job_spec.rb/
job.attempts.should == 1

job.failed_at.should_not == nil
Expand Down

0 comments on commit 9018ba1

Please sign in to comment.