Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper method to query the latency on the Queue object #415

Merged
merged 3 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions app/models/solid_queue/queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ def size
@size ||= ReadyExecution.queued_as(name).count
end

def latency
@latency ||= begin
now = Time.current
oldest_enqueued_at = ReadyExecution.queued_as(name).minimum(:created_at) || now

(now - oldest_enqueued_at).to_i
end
end

def human_latency
ActiveSupport::Duration.build(latency).inspect
end

def ==(queue)
name == queue.name
end
Expand Down
33 changes: 33 additions & 0 deletions test/unit/queue_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class QueueTest < ActiveSupport::TestCase
setup do
freeze_time

5.times do
AddToBufferJob.perform_later "hey!"
end
Expand Down Expand Up @@ -39,4 +41,35 @@ class QueueTest < ActiveSupport::TestCase
@default_queue.resume
end
end

test "return latency in seconds on each queue" do
travel_to 5.minutes.from_now

assert_in_delta 5.minutes.to_i, @background_queue.latency, 1.second.to_i
assert_equal 0, @default_queue.latency

@background_queue = SolidQueue::Queue.find_by_name("background")
@default_queue = SolidQueue::Queue.find_by_name("default")
travel_to 10.minutes.from_now

assert_in_delta 15.minutes.to_i, @background_queue.latency, 1.second.to_i
assert_equal 0, @default_queue.latency
end

test "returns memoized latency after the first call" do
travel_to 5.minutes.from_now

assert_in_delta 5.minutes.to_i, @background_queue.latency, 1.second.to_i

travel_to 10.minutes.from_now

assert_in_delta 5.minutes.to_i, @background_queue.latency, 1.second.to_i
end

test "return human latency on each queue" do
travel_to 5.minutes.from_now

assert_match (/5 minutes/), @background_queue.human_latency
assert_match (/0 seconds/), @default_queue.human_latency
end
end