Skip to content

Commit

Permalink
Use new #reserve method in the worker
Browse files Browse the repository at this point in the history
  • Loading branch information
bkeepers committed Nov 12, 2010
1 parent 488aaae commit 8e09ec0
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 23 deletions.
2 changes: 2 additions & 0 deletions lib/delayed/backend/base.rb
Expand Up @@ -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
Expand Down
24 changes: 1 addition & 23 deletions lib/delayed/worker.rb
Expand Up @@ -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

0 comments on commit 8e09ec0

Please sign in to comment.