Skip to content

Commit

Permalink
Merge cf558cc into 7db5654
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyshields committed Oct 23, 2017
2 parents 7db5654 + cf558cc commit 1380703
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,14 @@ Configured queue priorities can be overriden by passing priority to the delay me
object.delay(:queue => 'high_priority', priority: 0).method
```

You can also set a default `run_delay` which will affect the `run_at` of jobs within that queue:

```ruby
Delayed::Worker.queue_attributes = {
mailers: { run_delay: 10.seconds }
}
```

You can start processes to only work certain queues with the `queue` and `queues`
options defined below. Processes started without specifying a queue will run jobs
from **any** queue. To effectively have a process that runs jobs where a queue is not
Expand Down
17 changes: 15 additions & 2 deletions lib/delayed/backend/job_preparer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def prepare
set_payload
set_queue_name
set_priority
set_run_at
handle_deprecation
options
end
Expand All @@ -31,8 +32,14 @@ def set_queue_name
end

def set_priority
queue_attribute = Delayed::Worker.queue_attributes[options[:queue]]
options[:priority] ||= (queue_attribute && queue_attribute[:priority]) || Delayed::Worker.default_priority
options[:priority] ||= queue_attribute(:priority) || Delayed::Worker.default_priority
end

def set_run_at
run_delay = queue_attribute(:run_delay)
return unless run_delay
raise ArgumentError 'Option `:run_delay` must be a Numeric' unless run_delay.is_a?(Numeric)
options[:run_at] ||= run_delay.seconds.from_now
end

def handle_deprecation
Expand All @@ -48,6 +55,12 @@ def handle_deprecation
end
# rubocop:enabled GuardClause
end

# Must be called after #set_queue_name
def queue_attribute(attribute)
attrs = Delayed::Worker.queue_attributes[options[:queue]]
attrs && attrs[attribute]
end
end
end
end
28 changes: 27 additions & 1 deletion spec/message_sending_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,33 @@ def tell
end

it 'sets job options' do
run_at = Time.parse('2010-05-03 12:55 AM')
run_at = Time.parse('2010-05-03 12:55')
job = FairyTail.delay(:priority => 20, :run_at => run_at).to_s
expect(job.run_at).to eq(run_at)
expect(job.priority).to eq(20)
end

it 'sets queue priority' do
Delayed::Worker.default_queue_name = 'abbazabba'
Delayed::Worker.queue_attributes = {'abbazabba' => {:priority => 30}}
Delayed::Worker.default_priority = 99
job = FairyTail.delay.to_s
expect(job.priority).to eq(30)
end

it 'sets queue run_delay' do
allow(Time).to receive(:current).and_return(Time.parse('2017-01-01 12:55'))
Delayed::Worker.default_queue_name = 'abbazabba'
Delayed::Worker.queue_attributes = {'abbazabba' => {:run_delay => 30.minutes}}
job = FairyTail.delay.to_s
expect(job.run_at).to eq(Time.parse('2017-01-01 13:25'))
end

it 'sets job options override queue and default options' do
Delayed::Worker.default_priority = 99
Delayed::Worker.default_queue_name = 'abbazabba'
Delayed::Worker.queue_attributes = {'abbazabba' => {:priority => 30, :run_delay => 5.minutes}}
run_at = Time.parse('2010-05-03 12:55')
job = FairyTail.delay(:priority => 20, :run_at => run_at).to_s
expect(job.run_at).to eq(run_at)
expect(job.priority).to eq(20)
Expand Down

0 comments on commit 1380703

Please sign in to comment.