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

Add scope for queue negation #194

Open
wants to merge 3 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
19 changes: 17 additions & 2 deletions lib/delayed/backend/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Job < ::ActiveRecord::Base
scope :min_priority, lambda { where("priority >= ?", Worker.min_priority) if Worker.min_priority }
scope :max_priority, lambda { where("priority <= ?", Worker.max_priority) if Worker.max_priority }
scope :for_queues, lambda { |queues = Worker.queues| where(queue: queues) if Array(queues).any? }
scope :not_for_queues, lambda { |queues = Worker.queues| where.not(queue: queues).yield_self { |scope| Array(queues).include?(nil) ? scope : scope.or(where(queue: nil)) } if Array(queues).any? }

before_save :set_default_run_at

Expand Down Expand Up @@ -71,17 +72,31 @@ def self.after_fork

# When a worker is exiting, make sure we don't have any locked jobs.
def self.clear_locks!(worker_name)
where(locked_by: worker_name).update_all(locked_by: nil, locked_at: nil)
case Delayed::Backend::ActiveRecord.configuration.reserve_sql_strategy
# Optimizations for faster lookups on some common databases
when :optimized_sql
where(locked_by: worker_name).update_all(locked_by: nil, locked_at: nil)
# Slower but in some cases more unproblematic strategy to lookup records
# See https://github.com/collectiveidea/delayed_job_active_record/pull/89 for more details.
when :default_sql
job_ids = where(locked_by: worker_name).pluck(:id)
where(id: job_ids).update_all(locked_by: nil, locked_at: nil) if job_ids.present?
end
end

def self.reserve(worker, max_run_time = Worker.max_run_time)
ready_scope =
ready_to_run(worker.name, max_run_time)
.min_priority
.max_priority
.for_queues
.by_priority

if Worker.exclude_specified_queues
ready_scope = ready_scope.not_for_queues
else
ready_scope = ready_scope.for_queues
end

reserve_with_scope(ready_scope, worker, db_time_now)
end

Expand Down
19 changes: 19 additions & 0 deletions spec/delayed/backend/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,25 @@
end
end

describe 'not_for_queues' do
let(:job) { Delayed::Backend::ActiveRecord::Job.create!(handler: 'something') }

it 'does not return jobs with any of the given queues' do
job.update_columns(queue: 'somequeue')
expect(Delayed::Backend::ActiveRecord::Job.not_for_queues('somequeue')).not_to include(job)
end

it 'returns jobs with no queue' do
job.update_columns(queue: nil)
expect(Delayed::Backend::ActiveRecord::Job.not_for_queues('somequeue')).to include(job)
end

it 'does not return jobs with no queue if `nil` is one of the given queues' do
job.update_columns(queue: nil)
expect(Delayed::Backend::ActiveRecord::Job.not_for_queues(['somequeue', nil])).not_to include(job)
end
end

describe "reserve_with_scope" do
let(:relation_class) { Delayed::Job.limit(1).class }
let(:worker) { instance_double(Delayed::Worker, name: "worker01", read_ahead: 1) }
Expand Down