Skip to content

Commit 9dcd3f4

Browse files
rosaclaude
andcommitted
Let the job answer for the concurrency slot it holds
concurrency_limited? now just means the job has a concurrency key, as on the Active Job side, and the code checks for a missing class where that matters. The job exposes the semaphore it got its slot from and the limit it got it with, so the semaphore asks for those instead of reading the recorded data itself. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 126da2f commit 9dcd3f4

6 files changed

Lines changed: 53 additions & 41 deletions

File tree

‎app/models/solid_queue/job/concurrency_controls.rb‎

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ module ConcurrencyControls
1111
included do
1212
has_one :blocked_execution
1313

14-
delegate :concurrency_limit, to: :job_class
15-
16-
before_destroy :unblock_next_blocked_job, if: -> { concurrency_key.present? && ready? }
14+
before_destroy :unblock_next_blocked_job, if: -> { concurrency_limited? && ready? }
1715
end
1816

1917
class_methods do
@@ -29,32 +27,38 @@ def unblock_next_blocked_job
2927
end
3028

3129
def concurrency_limited?
32-
concurrency_key.present? && job_class.present?
30+
concurrency_key.present?
31+
end
32+
33+
# A job that kept its concurrency key after its class dropped limits_concurrency gets a limit of 1
34+
def concurrency_limit
35+
job_class.concurrency_limit || 1 if concurrency_limited?
3336
end
3437

3538
def concurrency_duration
3639
job_class ? job_class.concurrency_duration : SolidQueue.default_concurrency_control_period
3740
end
3841

3942
def acquire_concurrency_lock
40-
if concurrency_limited?
41-
if semaphore_id = Semaphore.wait(self)
42-
record_concurrency_lock(semaphore_id:, limit: concurrency_limit || 1)
43-
end
44-
else
43+
return true unless concurrency_limited?
44+
45+
if job_class.nil?
4546
# A job whose class doesn't load skips its limits. Record that it holds no slot,
4647
# so it doesn't give one back if it runs somewhere its class does load.
47-
record_concurrency_lock(semaphore_id: nil) if concurrency_key.present?
48-
true
48+
record_concurrency_lock(semaphore_id: nil)
49+
elsif semaphore_id = Semaphore.wait(self)
50+
record_concurrency_lock(semaphore_id:, limit: concurrency_limit)
4951
end
5052
end
5153

52-
def concurrency_lock
53-
arguments[CONCURRENCY_LOCK_KEY]
54+
# The semaphore the job got its concurrency slot from
55+
def acquired_semaphore_id
56+
concurrency_lock&.dig("semaphore_id")
5457
end
5558

56-
def concurrency_lock_recorded?
57-
arguments.key?(CONCURRENCY_LOCK_KEY)
59+
# The concurrency limit the job had when it got its slot
60+
def acquired_concurrency_limit
61+
concurrency_lock&.dig("limit")
5862
end
5963

6064
def blocked?
@@ -76,12 +80,21 @@ def record_concurrency_lock(semaphore_id:, limit: nil)
7680
true
7781
end
7882

83+
def concurrency_lock
84+
arguments[CONCURRENCY_LOCK_KEY]
85+
end
86+
87+
def concurrency_lock_recorded?
88+
arguments.key?(CONCURRENCY_LOCK_KEY)
89+
end
90+
7991
def release_concurrency_lock
8092
if concurrency_lock_recorded?
81-
concurrency_lock["semaphore_id"].present? && Semaphore.signal(self)
93+
acquired_semaphore_id.present? && Semaphore.signal(self)
8294
else
83-
# Jobs dispatched before Solid Queue recorded their semaphore
84-
concurrency_limited? && Semaphore.signal(self)
95+
# Jobs dispatched before Solid Queue recorded their semaphore give it back by key,
96+
# which needs their class to know their limit
97+
concurrency_limited? && job_class.present? && Semaphore.signal(self)
8598
end
8699
end
87100

‎app/models/solid_queue/job/executable.rb‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ def prepare_all_for_execution(jobs)
2626
end
2727

2828
def dispatch_all(jobs)
29-
# Jobs with a concurrency key go one by one, so they record the slot they get, or that they skipped it
30-
with_concurrency_limits, without_concurrency_limits = jobs.partition { |job| job.concurrency_key.present? }
29+
with_concurrency_limits, without_concurrency_limits = jobs.partition(&:concurrency_limited?)
3130

3231
dispatch_all_at_once(without_concurrency_limits)
3332
dispatch_all_one_by_one(with_concurrency_limits)

‎app/models/solid_queue/semaphore.rb‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ def attempt_decrement
7070
end
7171

7272
def attempt_increment
73-
semaphore_to_release.where(value: ...limit_for_release).update_all([ "value = value + 1, expires_at = ?", expires_at ]) > 0
73+
semaphore_to_release.where(value: ...limit_to_release).update_all([ "value = value + 1, expires_at = ?", expires_at ]) > 0
7474
end
7575

7676
# A job only gives its slot back to the semaphore it got it from. If that one expired
7777
# and was deleted, a semaphore for the same key now belongs to other jobs.
7878
# Jobs dispatched before Solid Queue recorded their semaphore give it back by key.
7979
def semaphore_to_release
80-
if job.concurrency_lock_recorded?
81-
Semaphore.where(id: job.concurrency_lock["semaphore_id"])
80+
if job.acquired_semaphore_id
81+
Semaphore.where(id: job.acquired_semaphore_id)
8282
else
8383
Semaphore.where(key: key)
8484
end
@@ -93,12 +93,12 @@ def expires_at
9393
end
9494

9595
def limit
96-
job.concurrency_limit || 1
96+
job.concurrency_limit
9797
end
9898

9999
# Recorded when the job got its slot, so it's known even if the job's class no longer loads
100-
def limit_for_release
101-
job.concurrency_lock&.dig("limit") || limit
100+
def limit_to_release
101+
job.acquired_concurrency_limit || limit
102102
end
103103
end
104104
end

‎test/models/solid_queue/claimed_execution_test.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ class SolidQueue::ClaimedExecutionTest < ActiveSupport::TestCase
170170
test "dispatch job with missing class and concurrency key skips concurrency controls" do
171171
job = create_job_with_missing_class(concurrency_key: "test_key")
172172

173-
assert_not job.concurrency_limited?
173+
assert_nil job.job_class
174174

175175
job.prepare_for_execution
176176

‎test/models/solid_queue/concurrency_lock_test.rb‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,33 +94,33 @@ def perform(name)
9494
test "a blocked job records the semaphore it gets when it's released" do
9595
a = claim(NonOverlappingJob.perform_later("A"))
9696
b = job_for(NonOverlappingJob.perform_later("B"))
97-
assert a.job.concurrency_lock["semaphore_id"].present?
98-
assert_not b.concurrency_lock_recorded?
97+
assert a.job.acquired_semaphore_id.present?
98+
assert_nil b.acquired_semaphore_id
9999

100100
a.perform
101101

102102
assert b.reload.ready?
103-
assert_equal semaphore.id, b.concurrency_lock["semaphore_id"]
104-
assert_equal 1, b.concurrency_lock["limit"]
103+
assert_equal semaphore.id, b.acquired_semaphore_id
104+
assert_equal 1, b.acquired_concurrency_limit
105105
end
106106

107107
test "jobs enqueued in bulk record the semaphore they get" do
108108
ActiveJob.perform_all_later([ NonOverlappingJob.new("A"), NonOverlappingJob.new("B") ])
109109
a, b = SolidQueue::Job.where(class_name: NonOverlappingJob.name).order(:id).to_a
110110

111111
assert a.ready?
112-
assert_equal semaphore.id, a.concurrency_lock["semaphore_id"]
112+
assert_equal semaphore.id, a.acquired_semaphore_id
113113
assert b.blocked?
114-
assert_not b.concurrency_lock_recorded?
114+
assert_nil b.acquired_semaphore_id
115115
end
116116

117117
test "jobs sharing a semaphore give their slots back to it" do
118118
a = claim(TwoAtATimeJob.perform_later("A"))
119119
b = claim(TwoAtATimeJob.perform_later("B"))
120120
c = job_for(TwoAtATimeJob.perform_later("C"))
121121

122-
assert_equal [ semaphore.id ] * 2, [ a, b ].map { |execution| execution.job.concurrency_lock["semaphore_id"] }
123-
assert_equal 2, a.job.concurrency_lock["limit"]
122+
assert_equal [ semaphore.id ] * 2, [ a, b ].map { |execution| execution.job.acquired_semaphore_id }
123+
assert_equal 2, a.job.acquired_concurrency_limit
124124
assert c.blocked?
125125

126126
a.perform
@@ -150,23 +150,23 @@ def perform(name)
150150
test "a retried job records the semaphore it gets on the retry" do
151151
a = claim(NonOverlappingJob.perform_later("A"))
152152
a.failed_with(ExpectedTestError.new)
153-
first_semaphore_id = a.job.reload.concurrency_lock["semaphore_id"]
153+
first_semaphore_id = a.job.reload.acquired_semaphore_id
154154

155155
travel_to 2.minutes.from_now
156156
SolidQueue::Semaphore.expired.delete_all
157157

158158
a.job.failed_execution.retry
159159

160160
assert a.job.reload.ready?
161-
assert_not_equal first_semaphore_id, a.job.concurrency_lock["semaphore_id"]
162-
assert_equal semaphore.id, a.job.concurrency_lock["semaphore_id"]
161+
assert_not_equal first_semaphore_id, a.job.acquired_semaphore_id
162+
assert_equal semaphore.id, a.job.acquired_semaphore_id
163163
end
164164

165165
test "a job records the limit it got its slot with, even when its class doesn't set one" do
166166
# A job that kept its concurrency key after its class dropped limits_concurrency
167167
a = SolidQueue::Job.create!(queue_name: "default", class_name: AddToBufferJob.name, active_job_id: SecureRandom.uuid,
168168
arguments: { "job_class" => AddToBufferJob.name, "arguments" => [ 1 ] }, concurrency_key: "concurrency-lock-test")
169-
assert_equal 1, a.concurrency_lock["limit"]
169+
assert_equal 1, a.acquired_concurrency_limit
170170

171171
# ...and then runs where the class is missing
172172
a.update_columns(class_name: "RemovedJobClass")

‎test/models/solid_queue/semaphore_test.rb‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ def perform(key)
3434
job = SolidQueue::Job.enqueue(NonOverlappingUpdateResultJob.new(@result))
3535

3636
assert job.ready?
37-
assert_not_equal expired.id, job.concurrency_lock["semaphore_id"]
38-
assert_equal SolidQueue::Semaphore.lock.find_by!(key: job.concurrency_key).id, job.concurrency_lock["semaphore_id"]
37+
assert_not_equal expired.id, job.acquired_semaphore_id
38+
assert_equal SolidQueue::Semaphore.lock.find_by!(key: job.concurrency_key).id, job.acquired_semaphore_id
3939
end
4040
end
4141

0 commit comments

Comments
 (0)