Skip to content

Not an issue. #38

Open
russ1985 opened this Issue Mar 2, 2011 · 0 comments

1 participant

@russ1985
russ1985 commented Mar 2, 2011

This is not really an issue not sure where else to post this. I added the functionality to pass what handler you want to run for a worker. This allows you to have multiple workers going and some looking for this handler and some looking for this handler. Just wanted to add that this is an awesome plugin. Below is the updated code.

usage

Delayed::Worker.new(:min_priority => ENV['MIN_PRIORITY'], :max_priority => ENV['MAX_PRIORITY'], :handler => handler).start

updated files

job.rb

#added to set what handler to run
named_scope :with_handler, lambda {|handler|
{:conditions => ['handler like ?',"%#{handler}%"]}
}

def self.find_available(worker_name, limit = 5, max_run_time = max_run_time, handler = nil)
#added to set what handler to run
unless handler.nil?
if min_priority
scope = self.ready_to_run(worker_name, max_run_time).with_handler(handler).min_priority.by_priority(limit)
elsif max_priority
scope = self.ready_to_run(worker_name, max_run_time).with_handler(handler).max_priority.by_priority(limit)
else
scope = self.ready_to_run(worker_name, max_run_time).with_handler(handler).by_priority(limit)
end
else
if min_priority
scope = self.ready_to_run(worker_name, max_run_time).min_priority.by_priority(limit)
elsif max_priority
scope = self.ready_to_run(worker_name, max_run_time).max_priority.by_priority(limit)
else
scope = self.ready_to_run(worker_name, max_run_time).by_priority(limit)
end
end

worker.rb

def initialize(options={})
@quiet = options[:quiet]

  #added to set what handler to run
  @handler = options[:handler]

  Delayed::Job.min_priority = options[:min_priority] if options.has_key?(:min_priority)
  Delayed::Job.max_priority = options[:max_priority] if options.has_key?(:max_priority)
end

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
  job = Delayed::Job.find_available(name, 5, max_run_time, @handler).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

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

  scope
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.