Skip to content

Commit c282f54

Browse files
committed
Use nil as constant default for run_at cursor
1 parent 033e384 commit c282f54

5 files changed

Lines changed: 19 additions & 19 deletions

File tree

lib/que/locker.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module Que
1515
# For more information, see the 'Predicate Specificity' chapter of:
1616
# https://brandur.org/postgres-queues
1717
class Locker
18-
RUN_AT_CURSOR_RESET = "-infinity"
18+
RUN_AT_CURSOR_RESET = nil
1919

2020
METRICS = [
2121
ExistsTotal = Prometheus::Client::Counter.new(

lib/que/sql.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ module Que
5353
FROM que_jobs AS j
5454
WHERE queue = $1::text
5555
AND job_id >= $2
56-
AND run_at >= $3::timestamptz
56+
AND run_at >= COALESCE($3::timestamptz, '-infinity'::timestamptz)
5757
AND run_at <= now()
5858
AND retryable = true
5959
ORDER BY priority, run_at, job_id
@@ -66,7 +66,7 @@ module Que
6666
SELECT j
6767
FROM que_jobs AS j
6868
WHERE queue = $1::text
69-
AND run_at >= $3::timestamptz
69+
AND run_at >= COALESCE($3::timestamptz, '-infinity'::timestamptz)
7070
AND run_at <= now()
7171
AND retryable = true
7272
AND (priority, run_at, job_id) > (jobs.priority, jobs.run_at, jobs.job_id)
@@ -185,7 +185,7 @@ module Que
185185
AND run_at <= now()
186186
AND retryable = true
187187
AND job_id >= $2
188-
AND run_at >= $3::timestamptz
188+
AND run_at >= COALESCE($3::timestamptz, '-infinity'::timestamptz)
189189
ORDER BY priority, run_at, job_id
190190
FOR UPDATE SKIP LOCKED
191191
LIMIT 1

spec/lib/que/adapters/active_record_with_lock_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
end
3131

3232
describe ".lock_job_with_lock_database" do
33-
subject(:lock_job) { adapter.lock_job_with_lock_database("default", 0, "-infinity") }
33+
subject(:lock_job) { adapter.lock_job_with_lock_database("default", 0) }
3434

3535
context "with no jobs enqueued" do
3636
it "exists the loop and sets correct metric values" do

spec/lib/que/locker_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ 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_lower_bound: "-infinity")
46+
def expect_to_lock_with(cursor:, run_at_lower_bound: nil)
4747
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
5151
it "scans entire table and calls block with nil job" do
52-
expect(Que).to receive(:execute).with(:lock_job, [queue, 0, "-infinity"])
52+
expect(Que).to receive(:execute).with(:lock_job, [queue, 0, nil])
5353

5454
with_locked_job do |job|
5555
expect(job).to be_nil
@@ -142,20 +142,20 @@ def expect_to_lock_with(cursor:, run_at_lower_bound: "-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_lower_bound: "-infinity")
145+
expect_to_lock_with(cursor: 0, run_at_lower_bound: nil)
146146
expect_to_work(job_1)
147147

148148
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_lower_bound: "-infinity")
153+
expect_to_lock_with(cursor: 0, run_at_lower_bound: nil)
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_lower_bound: "-infinity")
158+
expect_to_lock_with(cursor: 0, run_at_lower_bound: nil)
159159
with_locked_job { |_job| }
160160
end
161161
end
@@ -170,10 +170,10 @@ def expect_to_lock_with(cursor:, run_at_lower_bound: "-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_lower_bound: "-infinity")
173+
expect_to_lock_with(cursor: 0, run_at_lower_bound: nil)
174174
expect_to_work(job_1)
175175

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

spec/lib/que/worker_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,15 +211,15 @@
211211
FakeJob.enqueue(1)
212212

213213
expect(Que).
214-
to receive(:execute).with(:lock_job, ["default", 0, "-infinity"]).and_raise(PG::Error)
214+
to receive(:execute).with(:lock_job, ["default", 0, nil]).and_raise(PG::Error)
215215
expect(work).to eq(:postgres_error)
216216
end
217217
end
218218

219219
context "when postgres raises a bad connection error while processing a job" do
220220
before do
221221
allow(Que).to receive(:execute).
222-
with(:lock_job, ["default", 0, "-infinity"]).
222+
with(:lock_job, ["default", 0, nil]).
223223
and_raise(PG::ConnectionBad)
224224

225225
# Ensure we don't have any currently leased connections, since in a thread
@@ -246,7 +246,7 @@
246246
FakeJob.enqueue(1)
247247

248248
expect(Que).
249-
to receive(:execute).with(:lock_job, ["default", 0, "-infinity"]).
249+
to receive(:execute).with(:lock_job, ["default", 0, nil]).
250250
and_raise(ActiveRecord::ConnectionTimeoutError)
251251
expect(work).to eq(:postgres_error)
252252
end
@@ -257,7 +257,7 @@
257257
FakeJob.enqueue(1)
258258

259259
expect(Que).
260-
to receive(:execute).with(:lock_job, ["default", 0, "-infinity"]).
260+
to receive(:execute).with(:lock_job, ["default", 0, nil]).
261261
and_raise(ActiveRecord::ConnectionNotEstablished)
262262
expect(work).to eq(:postgres_error)
263263
end
@@ -286,18 +286,18 @@
286286
it "is false after a postgres error" do
287287
FakeJob.enqueue(1)
288288
expect(Que).
289-
to receive(:execute).with(:lock_job, ["default", 0, "-infinity"]).and_raise(PG::Error)
289+
to receive(:execute).with(:lock_job, ["default", 0, nil]).and_raise(PG::Error)
290290
worker.work
291291
expect(worker).to_not be_healthy
292292
end
293293

294294
it "recovers once work succeeds after a postgres error" do
295295
expect(Que).
296-
to receive(:execute).with(:lock_job, ["default", 0, "-infinity"]).and_raise(PG::Error)
296+
to receive(:execute).with(:lock_job, ["default", 0, nil]).and_raise(PG::Error)
297297
worker.work
298298
expect(worker).to_not be_healthy
299299

300-
allow(Que).to receive(:execute).with(:lock_job, ["default", 0, "-infinity"]).and_return([])
300+
allow(Que).to receive(:execute).with(:lock_job, ["default", 0, nil]).and_return([])
301301
worker.work # no job found, returns :job_not_found
302302
expect(worker).to be_healthy
303303
end

0 commit comments

Comments
 (0)