diff --git a/lib/good_job/active_job_extensions/concurrency.rb b/lib/good_job/active_job_extensions/concurrency.rb index ff82b226..ef9b911f 100644 --- a/lib/good_job/active_job_extensions/concurrency.rb +++ b/lib/good_job/active_job_extensions/concurrency.rb @@ -149,8 +149,10 @@ def deserialize(job_data) .order(Arel.sql("COALESCE(performed_at, scheduled_at, created_at) ASC")) .limit(limit).pluck(:active_job_id) # The current job has already been locked and will appear in the previous query - exceeded = :limit unless allowed_active_job_ids.include?(job.job_id) - next + unless allowed_active_job_ids.include?(job.job_id) + exceeded = :limit + next + end end if throttle @@ -165,8 +167,10 @@ def deserialize(job_data) .limit(throttle_limit) .pluck(:active_job_id) - exceeded = :throttle unless allowed_active_job_ids.include?(job.job_id) - next + unless allowed_active_job_ids.include?(job.job_id) + exceeded = :throttle + next + end end end diff --git a/spec/lib/good_job/active_job_extensions/concurrency_spec.rb b/spec/lib/good_job/active_job_extensions/concurrency_spec.rb index 8310e39e..01184cb7 100644 --- a/spec/lib/good_job/active_job_extensions/concurrency_spec.rb +++ b/spec/lib/good_job/active_job_extensions/concurrency_spec.rb @@ -214,6 +214,38 @@ def perform expect(GoodJob::Job.finished.count).to eq 3 end end + + describe 'perform_limit: together with perform_throttle:' do + before do + allow(GoodJob).to receive(:preserve_job_records).and_return(true) + + TestJob.good_job_control_concurrency_with( + perform_limit: -> { 1 }, + perform_throttle: -> { [1, 1.minute] }, + key: -> { arguments.first[:name] } + ) + end + + it 'does not perform if throttle period has not passed' do + TestJob.perform_later(name: "Alice") + TestJob.perform_later(name: "Alice") + TestJob.perform_later(name: "Alice") + GoodJob.perform_inline + + expect(GoodJob::Job.finished.count).to eq 1 + + Timecop.travel(61.seconds) + TestJob.perform_later(name: "Alice") + GoodJob.perform_inline + + expect(GoodJob::Job.finished.count).to eq 2 + + Timecop.travel(61.seconds) + GoodJob.perform_inline + + expect(GoodJob::Job.finished.count).to eq 3 + end + end end describe '#good_job_concurrency_key' do