Skip to content

Commit

Permalink
Simplify queue_attributes
Browse files Browse the repository at this point in the history
This changes the queue attributes syntax from
```
[
  { name: :high_priority, priority: -10 },
  { name: :low_priority, priority: 10 }
]
```
to
```
{
  high_priority: { priority: -10 },
  low_priority: { priority: 10 }
}
```
  • Loading branch information
albus522 committed May 16, 2016
1 parent 3dfdab2 commit 2026cd4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,16 @@ handle_asynchronously :tweet_later, :queue => 'tweets'
You can configure default priorities for named queues:

```ruby
Delayed::Worker.queue_attributes = [
{ name: :high_priority, priority: -10 },
{ name: :low_priority, priority: 10 }
]
Delayed::Worker.queue_attributes = {
high_priority: { priority: -10 },
low_priority: { priority: 10 }
}
```

Configured queue priorities can be overriden by passing priority to the delay method

```ruby
object.delay(:queue => 'high_priority', priority: 0).method
```

Running Jobs
Expand Down
10 changes: 5 additions & 5 deletions lib/delayed/backend/job_preparer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ def set_queue_name
end

def set_priority
options[:priority] ||= Delayed::Worker.default_priority
queue_attributes = Delayed::Worker.queue_attributes.select { |queue| queue[:name].to_s == options[:queue] }
options[:priority] = queue_attributes.first[:priority] if queue_attributes.any?
queue_attribute = Delayed::Worker.queue_attributes[options[:queue]]
options[:priority] ||= (queue_attribute && queue_attribute[:priority]) || Delayed::Worker.default_priority
end

def handle_deprecation
Expand All @@ -43,8 +42,9 @@ def handle_deprecation
options[:run_at] = args[1]
end

return if options[:payload_object].respond_to?(:perform)
raise ArgumentError, 'Cannot enqueue items which do not respond to perform'
unless options[:payload_object].respond_to?(:perform)
raise ArgumentError, 'Cannot enqueue items which do not respond to perform'
end
end
end
end
Expand Down
9 changes: 8 additions & 1 deletion lib/delayed/backend/shared_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ def create_job(opts = {})
after do
Delayed::Worker.max_priority = nil
Delayed::Worker.min_priority = nil
Delayed::Worker.queue_attributes = {}
end

it 'fetches jobs ordered by priority' do
Expand Down Expand Up @@ -316,10 +317,16 @@ def create_job(opts = {})
end

it 'sets job priority based on queue_attributes configuration' do
Delayed::Worker.queue_attributes = [{:name => 'job_tracking', :priority => 4}]
Delayed::Worker.queue_attributes = {'job_tracking' => {:priority => 4}}
job = described_class.enqueue :payload_object => NamedQueueJob.new
expect(job.priority).to eq(4)
end

it 'sets job priority based on the passed in priority overrideing queue_attributes configuration' do
Delayed::Worker.queue_attributes = {'job_tracking' => {:priority => 4}}
job = described_class.enqueue :payload_object => NamedQueueJob.new, :priority => 10
expect(job.priority).to eq(10)
end
end

context 'clear_locks!' do
Expand Down
17 changes: 12 additions & 5 deletions lib/delayed/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
require 'active_support/dependencies'
require 'active_support/core_ext/numeric/time'
require 'active_support/core_ext/class/attribute_accessors'
require 'active_support/hash_with_indifferent_access'
require 'active_support/core_ext/hash/indifferent_access'
require 'logger'
require 'benchmark'

Expand All @@ -14,18 +16,18 @@ class Worker # rubocop:disable ClassLength
DEFAULT_DEFAULT_PRIORITY = 0
DEFAULT_DELAY_JOBS = true
DEFAULT_QUEUES = [].freeze
DEFAULT_QUEUE_ATTRIBUTES = [].freeze
DEFAULT_QUEUE_ATTRIBUTES = HashWithIndifferentAccess.new.freeze
DEFAULT_READ_AHEAD = 5

cattr_accessor :min_priority, :max_priority, :max_attempts, :max_run_time,
:default_priority, :sleep_delay, :logger, :delay_jobs, :queues,
:read_ahead, :plugins, :destroy_failed_jobs, :exit_on_complete,
:default_log_level, :queue_attributes
:default_log_level

# Named queue into which jobs are enqueued by default
cattr_accessor :default_queue_name

cattr_reader :backend
cattr_reader :backend, :queue_attributes

# name_prefix is ignored if name is set directly
attr_accessor :name_prefix
Expand All @@ -43,8 +45,6 @@ def self.reset
@lifecycle = nil
end

reset

# Add or remove plugins in this list before the worker is instantiated
self.plugins = [Delayed::Plugins::ClearLocks]

Expand All @@ -71,6 +71,11 @@ def self.backend=(backend)
silence_warnings { ::Delayed.const_set(:Job, backend) }
end

# rubocop:disable ClassVars
def self.queue_attributes=(val)
@@queue_attributes = val.with_indifferent_access
end

def self.guess_backend
warn '[DEPRECATION] guess_backend is deprecated. Please remove it from your code.'
end
Expand Down Expand Up @@ -320,3 +325,5 @@ def reload!
end
end
end

Delayed::Worker.reset

1 comment on commit 2026cd4

@nruth
Copy link

@nruth nruth commented on 2026cd4 Aug 14, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking API change. Isn't the gem using SemVer?

http:https://guides.rubygems.org/patterns/#semantic-versioning this should have been v5

Please sign in to comment.