Skip to content

Commit

Permalink
Fix #reserve method to call Worker#name
Browse files Browse the repository at this point in the history
  • Loading branch information
bkeepers committed Nov 15, 2010
1 parent 5cbc4b3 commit c03b065
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
delayed_job (2.1.0.pre2)
delayed_job (2.1.0)
activesupport (~> 3.0)
daemons

Expand Down
4 changes: 2 additions & 2 deletions lib/delayed/backend/base.rb
Expand Up @@ -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

Expand Down
39 changes: 16 additions & 23 deletions lib/delayed/backend/shared_spec.rb
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/delayed/worker.rb
Expand Up @@ -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
Expand Down

0 comments on commit c03b065

Please sign in to comment.