Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Can specify a default priority per job type #1218

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/delayed/backend/job_preparer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ 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] ||= if options[:payload_object].respond_to?(:priority)
options[:payload_object].priority
else
queue_attribute = Delayed::Worker.queue_attributes[options[:queue]]
(queue_attribute && queue_attribute[:priority]) || Delayed::Worker.default_priority
end
end

def handle_deprecation
Expand Down
22 changes: 22 additions & 0 deletions lib/delayed/backend/shared_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ def create_job(opts = {})
job = described_class.enqueue :payload_object => NamedQueueJob.new
expect(job.queue).to eq(NamedQueueJob.new.queue_name)
end

it "uses the payload object's priority" do
job = described_class.enqueue :payload_object => PrioritizedQueueJob.new
expect(job.priority).to eq(PrioritizedQueueJob.new.priority)
end
end

context 'with multiple arguments' do
Expand Down Expand Up @@ -333,6 +338,23 @@ def create_job(opts = {})
job = described_class.enqueue :payload_object => NamedQueueJob.new, :priority => 10
expect(job.priority).to eq(10)
end

it 'sets job priority based on queue_attributes configuration' do
Delayed::Worker.queue_attributes = {'job_tracking' => {:priority => 4}}
job = described_class.enqueue :payload_object => PrioritizedQueueJob.new
expect(job.priority).to eq(5)
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 => PrioritizedQueueJob.new, :priority => 10
expect(job.priority).to eq(10)
end

it 'sets job priority based on the passed in priority in job class' do
job = described_class.enqueue :payload_object => PrioritizedQueueJob.new
expect(job.priority).to eq(5)
end
end

context 'clear_locks!' do
Expand Down
6 changes: 6 additions & 0 deletions spec/sample_jobs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ def queue_name
end
end

class PrioritizedQueueJob < SimpleJob
def priority
5
end
end

class ErrorJob
cattr_accessor :runs
@runs = 0
Expand Down