Permalink
Browse files

Fix #reserve method to call Worker#name

  • Loading branch information...
1 parent 5cbc4b3 commit c03b0655ad1356287da1e1b2698e5b31acbe5785 @bkeepers bkeepers committed Nov 15, 2010
Showing with 20 additions and 27 deletions.
  1. +1 −1 Gemfile.lock
  2. +2 −2 lib/delayed/backend/base.rb
  3. +16 −23 lib/delayed/backend/shared_spec.rb
  4. +1 −1 lib/delayed/worker.rb
View
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- delayed_job (2.1.0.pre2)
+ delayed_job (2.1.0)
activesupport (~> 3.0)
daemons
@@ -32,8 +32,8 @@ def enqueue(*args)
def reserve(worker, max_run_time = Worker.max_run_time)
# We get up to 5 jobs from the db. In case we cannot get exclusive access to a job we try the next.
# this leads to a more even distribution of jobs across the worker processes
- find_available(worker, 5, max_run_time).detect do |job|
- job.lock_exclusively!(max_run_time, worker)
+ find_available(worker.name, 5, max_run_time).detect do |job|
+ job.lock_exclusively!(max_run_time, worker.name)
end
end
@@ -135,49 +135,42 @@ def create_job(opts = {})
end
describe "reserve" do
+ before do
+ Delayed::Worker.max_run_time = 2.minutes
+ @worker = Delayed::Worker.new(:quiet => true)
+ end
+
it "should not reserve failed jobs" do
create_job :attempts => 50, :failed_at => described_class.db_time_now
- described_class.reserve('worker', 1.second).should be_nil
+ described_class.reserve(@worker).should be_nil
end
it "should not reserve jobs scheduled for the future" do
create_job :run_at => (described_class.db_time_now + 1.minute)
- described_class.reserve('worker', 4.hours).should be_nil
+ described_class.reserve(@worker).should be_nil
end
it "should lock the job so other workers can't reserve it" do
job = create_job
- described_class.reserve('worker1', 4.hours).should == job
- described_class.reserve('worker2', 4.hours).should be_nil
+ described_class.reserve(@worker).should == job
+ new_worker = Delayed::Worker.new(:quiet => true)
+ new_worker.name = 'worker2'
+ described_class.reserve(new_worker).should be_nil
end
it "should reserve open jobs" do
job = create_job
- described_class.reserve('worker', 4.hours).should == job
+ described_class.reserve(@worker).should == job
end
it "should reserve expired jobs" do
- job = create_job(:locked_by => 'worker', :locked_at => described_class.db_time_now - 2.minutes)
- described_class.reserve('worker', 1.minute).should == job
+ job = create_job(:locked_by => @worker.name, :locked_at => described_class.db_time_now - 3.minutes)
+ described_class.reserve(@worker).should == job
end
it "should reserve own jobs" do
- job = create_job(:locked_by => 'worker', :locked_at => (described_class.db_time_now - 1.minutes))
- described_class.reserve('worker', 4.hours).should == job
- end
-
- context "when another worker is already performing a task" do
- before :each do
- @job = described_class.create :payload_object => SimpleJob.new, :locked_by => 'worker1', :locked_at => described_class.db_time_now - 5.minutes
- end
-
- it "should not allow a second worker to get exclusive access" do
- described_class.reserve('worker2').should be_nil
- end
-
- it "should allow a second worker to get exclusive access if the timeout has passed" do
- described_class.reserve('worker2', 1.minute).should == @job
- end
+ job = create_job(:locked_by => @worker.name, :locked_at => (described_class.db_time_now - 1.minutes))
+ described_class.reserve(@worker).should == job
end
end
@@ -168,7 +168,7 @@ def handle_failed_job(job, error)
# Run the next job we can get an exclusive lock on.
# If no jobs are left we return nil
def reserve_and_run_one_job
- job = Delayed::Job.reserve(self, self.class.max_run_time)
+ job = Delayed::Job.reserve(self)
run(job) if job
end
end

0 comments on commit c03b065

Please sign in to comment.