From 8e09ec04f1716b160179f784b9b80ed4567c1de9 Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Fri, 12 Nov 2010 12:25:57 -0600 Subject: [PATCH] Use new #reserve method in the worker --- lib/delayed/backend/base.rb | 2 ++ lib/delayed/worker.rb | 24 +----------------------- 2 files changed, 3 insertions(+), 23 deletions(-) diff --git a/lib/delayed/backend/base.rb b/lib/delayed/backend/base.rb index 23491deb1..e2b9a309d 100644 --- a/lib/delayed/backend/base.rb +++ b/lib/delayed/backend/base.rb @@ -33,6 +33,8 @@ def enqueue(*args) end 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) end diff --git a/lib/delayed/worker.rb b/lib/delayed/worker.rb index ad1b3e4aa..0bdd9f234 100644 --- a/lib/delayed/worker.rb +++ b/lib/delayed/worker.rb @@ -161,31 +161,9 @@ 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 - - # 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 - if self.class.log_queries || !logger - available_jobs = find_available_jobs - else - logger.silence { available_jobs = find_available_jobs} - end - - job = available_jobs.detect do |job| - if job.lock_exclusively!(self.class.max_run_time, name) - say "acquired lock on #{job.name}" - true - else - say "failed to acquire exclusive lock for #{job.name}", Logger::WARN - false - end - end - + job = Delayed::Job.reserve(worker, self.class.max_run_time) run(job) if job end - - def find_available_jobs - Delayed::Job.find_available(name, 5, self.class.max_run_time) - end end end