From 9018ba118d2be78b32ba3d50974f6f60512f2ab2 Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Tue, 10 Nov 2009 08:07:59 -0500 Subject: [PATCH] Clean up some logic for rescheduling --- lib/delayed/job.rb | 11 +++-------- spec/job_spec.rb | 9 +++------ 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/lib/delayed/job.rb b/lib/delayed/job.rb index 2c50b9828..bef64b801 100644 --- a/lib/delayed/job.rb +++ b/lib/delayed/job.rb @@ -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 diff --git a/spec/job_spec.rb b/spec/job_spec.rb index ff839a3be..b449a8c77 100644 --- a/spec/job_spec.rb +++ b/spec/job_spec.rb @@ -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