Skip to content

Commit

Permalink
Add helper method to query the latency on the Queue object
Browse files Browse the repository at this point in the history
  • Loading branch information
andersonkrs committed Nov 17, 2024
1 parent 60e4ba4 commit 8cc892d
Show file tree
Hide file tree
Showing 2 changed files with 34 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 = 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

0 comments on commit 8cc892d

Please sign in to comment.