Skip to content

Commit

Permalink
respect perform_throttle even if perform_limit is provided (#1470)
Browse files Browse the repository at this point in the history
* fix #1469

* Use the same pattern for throttle too

---------

Co-authored-by: Ben Sheldon [he/him] <bensheldon@github.com>
  • Loading branch information
doits and bensheldon authored Aug 16, 2024
1 parent cc9784d commit 2da97eb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/good_job/active_job_extensions/concurrency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
32 changes: 32 additions & 0 deletions spec/lib/good_job/active_job_extensions/concurrency_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,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
Expand Down

0 comments on commit 2da97eb

Please sign in to comment.