Skip to content

Commit

Permalink
new worker-options: exclude_queues and eval_qeues
Browse files Browse the repository at this point in the history
One can explicitly exclude queues by name (see collectiveidea#789) or even give an
argument through, for eval with where.
  • Loading branch information
philister committed Nov 9, 2015
1 parent 3b31ffd commit 7dac119
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
8 changes: 8 additions & 0 deletions lib/delayed/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ def initialize(args) # rubocop:disable MethodLength
opt.on('--queue=queue', 'Specify which queue DJ must look up for jobs') do |queue|
@options[:queues] = queue.split(',')
end
opt.on('--exclude_queues=queues', 'Specify which queues DJ must NOT look up for jobs') do |exclude_queues|
@options[:exlude_queues] = exclude_queues.split(',')
end
opt.on('--eval_queues=evalstring', "Specify a string, which will be eval'ed to where() while lookup. E.g. \"['created_at < ',2.days.ago]\"") do |eval_queues|
# this will fail (throwing an exception), if option eval_queues is not a valid argument to eval or where
Delayed::Job.where(eval(eval_queues)).any?
@options[:eval_queues] = eval_queues
end
opt.on('--pool=queue1[,queue2][:worker_count]', 'Specify queues and number of workers for a worker pool') do |pool|
parse_worker_pool(pool)
end
Expand Down
10 changes: 9 additions & 1 deletion lib/delayed/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class Worker # rubocop:disable ClassLength
DEFAULT_DEFAULT_PRIORITY = 0
DEFAULT_DELAY_JOBS = true
DEFAULT_QUEUES = []
DEFAULT_EXCLUDE_QUEUES = []
DEFAULT_EVAL_QUEUES = nil
DEFAULT_READ_AHEAD = 5

cattr_accessor :min_priority, :max_priority, :max_attempts, :max_run_time,
Expand All @@ -25,6 +27,10 @@ class Worker # rubocop:disable ClassLength
# Named queue into which jobs are enqueued by default
cattr_accessor :default_queue_name

# Named queues to exclude
cattr_accessor :exclude_queues, :eval_queues


cattr_reader :backend

# name_prefix is ignored if name is set directly
Expand All @@ -38,6 +44,8 @@ def self.reset
self.default_priority = DEFAULT_DEFAULT_PRIORITY
self.delay_jobs = DEFAULT_DELAY_JOBS
self.queues = DEFAULT_QUEUES
self.exclude_queues = DEFAULT_EXCLUDE_QUEUES
self.eval_queues = DEFAULT_EVAL_QUEUES
self.read_ahead = DEFAULT_READ_AHEAD
@lifecycle = nil
end
Expand Down Expand Up @@ -125,7 +133,7 @@ def initialize(options = {})
@quiet = options.key?(:quiet) ? options[:quiet] : true
@failed_reserve_count = 0

[:min_priority, :max_priority, :sleep_delay, :read_ahead, :queues, :exit_on_complete].each do |option|
[:min_priority, :max_priority, :sleep_delay, :read_ahead, :queues, :exit_on_complete, :exclude_queues, :eval_queues].each do |option|
self.class.send("#{option}=", options[option]) if options.key?(option)
end

Expand Down

0 comments on commit 7dac119

Please sign in to comment.