Skip to content

Commit fabf713

Browse files
authored
Merge pull request #415 from andersonkrs/main
Add helper method to query the latency on the Queue object
2 parents aa104e1 + 50003ac commit fabf713

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

app/models/solid_queue/queue.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@ def size
4040
@size ||= ReadyExecution.queued_as(name).count
4141
end
4242

43+
def latency
44+
@latency ||= begin
45+
now = Time.current
46+
oldest_enqueued_at = ReadyExecution.queued_as(name).minimum(:created_at) || now
47+
48+
(now - oldest_enqueued_at).to_i
49+
end
50+
end
51+
52+
def human_latency
53+
ActiveSupport::Duration.build(latency).inspect
54+
end
55+
4356
def ==(queue)
4457
name == queue.name
4558
end

test/unit/queue_test.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
class QueueTest < ActiveSupport::TestCase
44
setup do
5+
freeze_time
6+
57
5.times do
68
AddToBufferJob.perform_later "hey!"
79
end
@@ -39,4 +41,35 @@ class QueueTest < ActiveSupport::TestCase
3941
@default_queue.resume
4042
end
4143
end
44+
45+
test "return latency in seconds on each queue" do
46+
travel_to 5.minutes.from_now
47+
48+
assert_in_delta 5.minutes.to_i, @background_queue.latency, 1.second.to_i
49+
assert_equal 0, @default_queue.latency
50+
51+
@background_queue = SolidQueue::Queue.find_by_name("background")
52+
@default_queue = SolidQueue::Queue.find_by_name("default")
53+
travel_to 10.minutes.from_now
54+
55+
assert_in_delta 15.minutes.to_i, @background_queue.latency, 1.second.to_i
56+
assert_equal 0, @default_queue.latency
57+
end
58+
59+
test "returns memoized latency after the first call" do
60+
travel_to 5.minutes.from_now
61+
62+
assert_in_delta 5.minutes.to_i, @background_queue.latency, 1.second.to_i
63+
64+
travel_to 10.minutes.from_now
65+
66+
assert_in_delta 5.minutes.to_i, @background_queue.latency, 1.second.to_i
67+
end
68+
69+
test "return human latency on each queue" do
70+
travel_to 5.minutes.from_now
71+
72+
assert_match (/5 minutes/), @background_queue.human_latency
73+
assert_match (/0 seconds/), @default_queue.human_latency
74+
end
4275
end

0 commit comments

Comments
 (0)