Skip to content

Commit cb1269f

Browse files
BBrannickclaude
andcommitted
Rename run_at_cursor to run_at_lower_bound where it represents a timestamp value
run_at_cursor is now only used as the boolean feature flag on Locker/Worker. The actual timestamp lower bound passed through the locking methods is renamed run_at_lower_bound to make the distinction clear. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 379b044 commit cb1269f

3 files changed

Lines changed: 24 additions & 24 deletions

File tree

lib/que/adapters/active_record_with_lock.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ def checkout_lock_database_connection(&block)
3636
def execute(command, params = [])
3737
case command
3838
when :lock_job
39-
queue, cursor, run_at_cursor = params
40-
lock_job_with_lock_database(queue, cursor, run_at_cursor)
39+
queue, cursor, run_at_lower_bound = params
40+
lock_job_with_lock_database(queue, cursor, run_at_lower_bound)
4141
when :unlock_job
4242
job_id = params[0]
4343
unlock_job(job_id)
@@ -48,11 +48,11 @@ def execute(command, params = [])
4848

4949
# This method continues looping through the que_jobs table until it either
5050
# locks a job successfully or determines that there are no jobs to process.
51-
def lock_job_with_lock_database(queue, cursor, run_at_cursor = Que::Locker::RUN_AT_CURSOR_RESET)
51+
def lock_job_with_lock_database(queue, cursor, run_at_lower_bound = Que::Locker::RUN_AT_CURSOR_RESET)
5252
loop do
5353
observe(duration_metric: FindJobSecondsTotal, labels: { queue: queue }) do
5454
Que.transaction do
55-
job_to_lock = Que.execute(:find_job_to_lock, [queue, cursor, run_at_cursor])
55+
job_to_lock = Que.execute(:find_job_to_lock, [queue, cursor, run_at_lower_bound])
5656
return job_to_lock if job_to_lock.empty?
5757

5858
cursor = job_to_lock.first["job_id"]

lib/que/locker.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def initialize(queue:, cursor_expiry:, window: nil, budget: nil, secondary_queue
6060
@cursor_expiry = cursor_expiry
6161
@run_at_cursor = run_at_cursor
6262
@queue_cursors = {}
63-
@queue_run_at_cursors = {}
63+
@run_at_lower_bounds = {}
6464
@queue_expires_at = {}
6565
@secondary_queues = secondary_queues
6666
@consolidated_queues = Array.wrap(queue).concat(secondary_queues)
@@ -82,8 +82,8 @@ def with_locked_job
8282

8383
job = @consolidated_queues.lazy.filter_map do |queue|
8484
cursor = @queue_cursors.fetch(queue, 0)
85-
run_at_cursor = @queue_run_at_cursors.fetch(queue, RUN_AT_CURSOR_RESET)
86-
found_job = lock_job_in(queue, cursor, run_at_cursor)
85+
run_at_lower_bound = @run_at_lower_bounds.fetch(queue, RUN_AT_CURSOR_RESET)
86+
found_job = lock_job_in(queue, cursor, run_at_lower_bound)
8787

8888
# Because we were using a cursor when we tried to lock this job, if we fail to
8989
# find a job it is not necessarily the case that there aren't jobs in the
@@ -121,7 +121,7 @@ def with_locked_job
121121
return if job && !exists?(job)
122122

123123
@queue_cursors[job[:queue]] = job[:job_id] if job
124-
@queue_run_at_cursors[job[:queue]] = job[:run_at] if job && @run_at_cursor
124+
@run_at_lower_bounds[job[:queue]] = job[:run_at] if job && @run_at_cursor
125125

126126
yield job
127127
ensure
@@ -134,17 +134,17 @@ def with_locked_job
134134

135135
private
136136

137-
def lock_job_in(queue, cursor, run_at_cursor)
137+
def lock_job_in(queue, cursor, run_at_lower_bound)
138138
observe(nil, ThrottleSecondsTotal, worked_queue: queue) { @leaky_bucket.refill }
139-
@leaky_bucket.observe { execute_lock_job_in(queue, cursor, run_at_cursor) }
139+
@leaky_bucket.observe { execute_lock_job_in(queue, cursor, run_at_lower_bound) }
140140
end
141141

142-
def execute_lock_job_in(queue, cursor, run_at_cursor)
142+
def execute_lock_job_in(queue, cursor, run_at_lower_bound)
143143
strategy = cursor.zero? ? "full" : "cursor"
144144
observe(AcquireTotal, AcquireSecondsTotal,
145145
worked_queue: queue,
146146
strategy: strategy) do
147-
lock_job_query(queue, cursor, run_at_cursor)
147+
lock_job_query(queue, cursor, run_at_lower_bound)
148148
end
149149
end
150150

@@ -154,8 +154,8 @@ def exists?(job)
154154
end
155155
end
156156

157-
def lock_job_query(queue, cursor, run_at_cursor)
158-
Que.execute(:lock_job, [queue, cursor, run_at_cursor]).first
157+
def lock_job_query(queue, cursor, run_at_lower_bound)
158+
Que.execute(:lock_job, [queue, cursor, run_at_lower_bound]).first
159159
end
160160

161161
def handle_expired_cursors!
@@ -167,7 +167,7 @@ def handle_expired_cursors!
167167

168168
def reset_cursor_for!(queue)
169169
@queue_cursors[queue] = 0
170-
@queue_run_at_cursors[queue] = RUN_AT_CURSOR_RESET
170+
@run_at_lower_bounds[queue] = RUN_AT_CURSOR_RESET
171171
@queue_expires_at[queue] = monotonic_now + @cursor_expiry
172172
end
173173

spec/lib/que/locker_spec.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ def expect_to_work(job)
4343
end
4444

4545
# Our tests are very concerned with which cursor we use and when
46-
def expect_to_lock_with(cursor:, run_at_cursor: "-infinity")
47-
expect(Que).to receive(:execute).with(:lock_job, [queue, cursor, run_at_cursor])
46+
def expect_to_lock_with(cursor:, run_at_lower_bound: "-infinity")
47+
expect(Que).to receive(:execute).with(:lock_job, [queue, cursor, run_at_lower_bound])
4848
end
4949

5050
context "with no jobs to lock" do
@@ -131,7 +131,7 @@ def expect_to_lock_with(cursor:, run_at_cursor: "-infinity")
131131

132132
context "with run_at_cursor enabled" do
133133
subject(:locker) do
134-
described_class.new(queue: queue, cursor_expiry: 60, run_at_cursor: true)
134+
described_class.new(queue: queue, cursor_expiry: 60, run_at_lower_bound: true)
135135
end
136136

137137
let!(:job_1) { FakeJob.enqueue(1, queue: queue, priority: 1).attrs }
@@ -142,20 +142,20 @@ def expect_to_lock_with(cursor:, run_at_cursor: "-infinity")
142142
end
143143

144144
it "advances the run_at cursor to the previous job's run_at after locking" do
145-
expect_to_lock_with(cursor: 0, run_at_cursor: "-infinity")
145+
expect_to_lock_with(cursor: 0, run_at_lower_bound: "-infinity")
146146
expect_to_work(job_1)
147147

148-
expect_to_lock_with(cursor: job_1[:job_id], run_at_cursor: job_1[:run_at])
148+
expect_to_lock_with(cursor: job_1[:job_id], run_at_lower_bound: job_1[:run_at])
149149
with_locked_job { |_job| }
150150
end
151151

152152
it "resets both cursors when the expiry elapses" do
153-
expect_to_lock_with(cursor: 0, run_at_cursor: "-infinity")
153+
expect_to_lock_with(cursor: 0, run_at_lower_bound: "-infinity")
154154
expect_to_work(job_1)
155155

156156
allow(Process).to receive(:clock_gettime).and_return(61)
157157

158-
expect_to_lock_with(cursor: 0, run_at_cursor: "-infinity")
158+
expect_to_lock_with(cursor: 0, run_at_lower_bound: "-infinity")
159159
with_locked_job { |_job| }
160160
end
161161
end
@@ -170,10 +170,10 @@ def expect_to_lock_with(cursor:, run_at_cursor: "-infinity")
170170
end
171171

172172
it "always passes -infinity as the run_at cursor regardless of jobs worked" do
173-
expect_to_lock_with(cursor: 0, run_at_cursor: "-infinity")
173+
expect_to_lock_with(cursor: 0, run_at_lower_bound: "-infinity")
174174
expect_to_work(job_1)
175175

176-
expect_to_lock_with(cursor: job_1[:job_id], run_at_cursor: "-infinity")
176+
expect_to_lock_with(cursor: job_1[:job_id], run_at_lower_bound: "-infinity")
177177
with_locked_job { |_job| }
178178
end
179179
end

0 commit comments

Comments
 (0)