diff --git a/lib/delayed/backend/active_record.rb b/lib/delayed/backend/active_record.rb index 747c2f16..3da65ea2 100644 --- a/lib/delayed/backend/active_record.rb +++ b/lib/delayed/backend/active_record.rb @@ -48,6 +48,7 @@ def self.reserve(worker, max_run_time = Worker.max_run_time) ready_scope = ready_scope.where('priority >= ?', Worker.min_priority) if Worker.min_priority ready_scope = ready_scope.where('priority <= ?', Worker.max_priority) if Worker.max_priority ready_scope = ready_scope.where(:queue => Worker.queues) if Worker.queues.any? + ready_scope = ready_scope.where("queue NOT IN (?) OR queue IS NULL", Worker.excludes) if Worker.respond_to?(:excludes) && Worker.excludes.any? ready_scope = ready_scope.by_priority now = self.db_time_now diff --git a/spec/delayed/backend/active_record_spec.rb b/spec/delayed/backend/active_record_spec.rb index 293fdc93..0829cbbf 100644 --- a/spec/delayed/backend/active_record_spec.rb +++ b/spec/delayed/backend/active_record_spec.rb @@ -43,6 +43,33 @@ end end + describe "process" do + it "reserves jobs from the correct queue" do + # clear any jobs sitting around + Delayed::Backend::ActiveRecord::Job.delete_all + + job = Delayed::Backend::ActiveRecord::Job.enqueue :payload_object => SimpleJob.new, :queue => 'queue' + Delayed::Backend::ActiveRecord::Job.count.should == 1 + Delayed::Worker.new(:queue => 'queue').work_off + Delayed::Backend::ActiveRecord::Job.count.should == 0 + end + + if Delayed::Worker.respond_to?(:excludes) + it "ignores excluded queues when reserving jobs" do + # clear any jobs sitting around + Delayed::Backend::ActiveRecord::Job.delete_all + + job = Delayed::Backend::ActiveRecord::Job.enqueue :payload_object => SimpleJob.new, :queue => 'process' + excluded_job = Delayed::Backend::ActiveRecord::Job.enqueue :payload_object => SimpleJob.new, :queue => 'ignore' + Delayed::Worker.excludes << 'ignore' + Delayed::Worker.new.work_off + + Delayed::Backend::ActiveRecord::Job.find(excluded_job.id).queue.should == 'ignore' + Delayed::Backend::ActiveRecord::Job.count.should == 1 + end + end + end + if ::ActiveRecord::VERSION::MAJOR < 4 || defined?(::ActiveRecord::MassAssignmentSecurity) context "ActiveRecord::Base.send(:attr_accessible, nil)" do before do