Permalink
Browse files

Merge remote branch 'Viximo/rescheduling'

* Viximo/rescheduling:
  Remove broken spec that should not have been committed
  Reschedule jobs using a time provided by the handler, or fall back to default
  • Loading branch information...
2 parents 4784f9d + 92d7e31 commit 4a98683acf1d5af072ac6058393ee5f619d9884c @bkeepers bkeepers committed Sep 27, 2010
Showing with 24 additions and 5 deletions.
  1. +6 −1 lib/delayed/backend/base.rb
  2. +11 −3 lib/delayed/backend/shared_spec.rb
  3. +1 −1 lib/delayed/worker.rb
  4. +6 −0 spec/sample_jobs.rb
@@ -96,12 +96,17 @@ def hook(name, *args)
end
end
+ def reschedule_at
+ payload_object.respond_to?(:reschedule_at) ?
+ payload_object.reschedule_at(self.class.db_time_now, attempts) :
+ self.class.db_time_now + (attempts ** 4) + 5
+ end
+
protected
def set_default_run_at
self.run_at ||= self.class.db_time_now
end
-
end
end
end
@@ -294,7 +294,7 @@ def create_job(opts = {})
@job.locked_at.should be_nil
end
end
-
+
context "large handler" do
before do
text = "Lorem ipsum dolor sit amet. " * 1000
@@ -402,8 +402,8 @@ def create_job(opts = {})
# reset defaults
Delayed::Worker.destroy_failed_jobs = true
Delayed::Worker.max_attempts = 25
-
- @job = Delayed::Job.enqueue ErrorJob.new
+
+ @job = Delayed::Job.enqueue(ErrorJob.new)
end
it "should record last_error when destroy_failed_jobs = false, max_attempts = 1" do
@@ -425,6 +425,14 @@ def create_job(opts = {})
@job.run_at.should > Delayed::Job.db_time_now - 10.minutes
@job.run_at.should < Delayed::Job.db_time_now + 10.minutes
end
+
+ it 'should re-schedule with handler provided time if present' do
+ @job = Delayed::Job.enqueue(CustomRescheduleJob.new(99.minutes))
+ @worker.run(@job)
+ @job.reload
+
+ (Delayed::Job.db_time_now + 99.minutes - @job.run_at).abs.should < 1
+ end
end
context "reschedule" do
@@ -128,7 +128,7 @@ def run(job)
# Uses an exponential scale depending on the number of failed attempts.
def reschedule(job, time = nil)
if (job.attempts += 1) < self.class.max_attempts
- time ||= Job.db_time_now + (job.attempts ** 4) + 5
+ time ||= job.reschedule_at
job.run_at = time
job.unlock
job.save!
View
@@ -14,6 +14,12 @@ class ErrorJob
def perform; raise 'did not work'; end
end
+class CustomRescheduleJob < Struct.new(:offset)
+ cattr_accessor :runs; self.runs = 0
+ def perform; raise 'did not work'; end
+ def reschedule_at(time, attempts); time + offset; end
+end
+
class LongRunningJob
def perform; sleep 250; end
end

0 comments on commit 4a98683

Please sign in to comment.