Skip to content

Commit

Permalink
Job lock and run are now performed independently within Delayed::Worker
Browse files Browse the repository at this point in the history
  • Loading branch information
David Genord II authored and bkeepers committed Oct 16, 2009
1 parent 747fb3d commit 2c3b88c
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions lib/delayed/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ def start
Delayed::Job.clear_locks!(name)
end

def say(text)
def say(text, level = Logger::INFO)
puts text unless @quiet
logger.info text if logger
logger.add level, text if logger
end

protected
Expand All @@ -76,12 +76,21 @@ def reserve_and_run_one_job(max_run_time = job_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
Delayed::Job.find_available(name, 5, max_run_time).each do |job|
t = job.run_with_lock(max_run_time, name)
return t unless t == nil # return if we did work (good or bad)
job = Delayed::Job.find_available(name, 5, max_run_time).detect do |job|
if job.lock_exclusively!(max_run_time, name)
say "* [Worker(#{name})] acquired lock on #{job.name}"
true
else
say "* [Worker(#{name})] failed to acquire exclusive lock for #{job.name}", Logger::WARN
false
end
end

nil # we didn't do any work, all 5 were not lockable
if job.nil?
nil # we didn't do any work, all 5 were not lockable
else
job.run(max_run_time)
end
end

# Do num jobs and return stats on success/failure.
Expand Down

0 comments on commit 2c3b88c

Please sign in to comment.