@@ -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
@@ -28,33 +26,39 @@ def unblock_next_blocked_job
2826 end
2927 end
3028
29+ def acquire_concurrency_lock
30+ return true unless concurrency_limited?
31+
32+ if job_class . nil?
33+ # A job whose class doesn't load skips its limits. Record that it holds no slot,
34+ # so it doesn't give one back if it runs somewhere its class does load.
35+ record_concurrency_lock ( semaphore_id : nil )
36+ elsif semaphore_id = Semaphore . wait ( self )
37+ record_concurrency_lock ( semaphore_id :, limit : concurrency_limit )
38+ end
39+ end
40+
3141 def concurrency_limited?
32- concurrency_key . present? && job_class . present?
42+ concurrency_key . present?
3343 end
3444
35- def concurrency_duration
36- job_class ? job_class . concurrency_duration : SolidQueue . default_concurrency_control_period
45+ # A job that kept its concurrency key after its class dropped limits_concurrency gets a limit of 1
46+ def concurrency_limit
47+ job_class . concurrency_limit || 1 if concurrency_limited?
3748 end
3849
39- 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
45- # A job whose class doesn't load skips its limits. Record that it holds no slot,
46- # 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
49- end
50+ def concurrency_duration
51+ job_class ? job_class . concurrency_duration : SolidQueue . default_concurrency_control_period
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?
@@ -66,8 +70,25 @@ def job_class
6670 end
6771
6872 private
69- def concurrency_on_conflict
70- job_class . concurrency_on_conflict . to_s . inquiry
73+ def release_concurrency_lock
74+ holds_concurrency_slot? && Semaphore . signal ( self )
75+ end
76+
77+ def holds_concurrency_slot?
78+ if concurrency_lock_recorded?
79+ acquired_semaphore_id . present?
80+ else
81+ # Jobs dispatched before Solid Queue recorded their semaphore, which need their class to know their limit
82+ concurrency_limited? && job_class . present?
83+ end
84+ end
85+
86+ def concurrency_lock_recorded?
87+ arguments . key? ( CONCURRENCY_LOCK_KEY )
88+ end
89+
90+ def release_next_blocked_job
91+ BlockedExecution . release_one ( concurrency_key )
7192 end
7293
7394 def record_concurrency_lock ( semaphore_id :, limit : nil )
@@ -76,13 +97,8 @@ def record_concurrency_lock(semaphore_id:, limit: nil)
7697 true
7798 end
7899
79- def release_concurrency_lock
80- if concurrency_lock_recorded?
81- concurrency_lock [ "semaphore_id" ] . present? && Semaphore . signal ( self )
82- else
83- # Jobs dispatched before Solid Queue recorded their semaphore
84- concurrency_limited? && Semaphore . signal ( self )
85- end
100+ def concurrency_lock
101+ arguments [ CONCURRENCY_LOCK_KEY ]
86102 end
87103
88104 def handle_concurrency_conflict
@@ -93,12 +109,12 @@ def handle_concurrency_conflict
93109 end
94110 end
95111
96- def block
97- BlockedExecution . create_or_find_by! ( job : self )
112+ def concurrency_on_conflict
113+ job_class . concurrency_on_conflict . to_s . inquiry
98114 end
99115
100- def release_next_blocked_job
101- BlockedExecution . release_one ( concurrency_key )
116+ def block
117+ BlockedExecution . create_or_find_by! ( job : self )
102118 end
103119
104120 def execution
0 commit comments