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 1 commit
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this perhaps intended to be

@latency ||= begin

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ooh good catch! 🙈

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just fixed it and created a test for the memoization!

now = Time.current
oldest_enqueued_at = SolidQueue::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
21 changes: 21 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,23 @@ 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

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 "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