Skip to content

Commit

Permalink
Merge pull request #415 from andersonkrs/main
Browse files Browse the repository at this point in the history
Add helper method to query the latency on the Queue object
  • Loading branch information
rosa authored Dec 1, 2024
2 parents aa104e1 + 50003ac commit fabf713
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
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

0 comments on commit fabf713

Please sign in to comment.