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

respect perform_throttle even if perform_limit is provided #1470

Merged
merged 2 commits into from
Aug 16, 2024
Merged
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
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 @@ -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
Expand Down