mirror of
https://github.com/mperham/sidekiq.git
synced 2022-11-09 13:52:34 -05:00
Safer latency calculation for incomplete jobs, fixes #3340
This commit is contained in:
parent
72c763e02c
commit
0c193abcae
2 changed files with 33 additions and 3 deletions
|
@ -75,7 +75,10 @@ module Sidekiq
|
|||
enqueued = pipe2_res[s..-1].map(&:to_i).inject(0, &:+)
|
||||
|
||||
default_queue_latency = if (entry = pipe1_res[6].first)
|
||||
Time.now.to_f - Sidekiq.load_json(entry)['enqueued_at'.freeze]
|
||||
job = Sidekiq.load_json(entry)
|
||||
now = Time.now.to_f
|
||||
thence = job['enqueued_at'.freeze] || now
|
||||
now - thence
|
||||
else
|
||||
0
|
||||
end
|
||||
|
@ -225,7 +228,10 @@ module Sidekiq
|
|||
conn.lrange(@rname, -1, -1)
|
||||
end.first
|
||||
return 0 unless entry
|
||||
Time.now.to_f - Sidekiq.load_json(entry)['enqueued_at']
|
||||
job = Sidekiq.load_json(entry)
|
||||
now = Time.now.to_f
|
||||
thence = job['enqueued_at'] || now
|
||||
now - thence
|
||||
end
|
||||
|
||||
def each
|
||||
|
@ -351,7 +357,8 @@ module Sidekiq
|
|||
end
|
||||
|
||||
def latency
|
||||
Time.now.to_f - (@item['enqueued_at'] || @item['created_at'])
|
||||
now = Time.now.to_f
|
||||
now - (@item['enqueued_at'] || @item['created_at'] || now)
|
||||
end
|
||||
|
||||
##
|
||||
|
|
|
@ -16,6 +16,7 @@ class TestApi < Sidekiq::Test
|
|||
assert_equal 0, s.processed
|
||||
assert_equal 0, s.failed
|
||||
assert_equal 0, s.enqueued
|
||||
assert_equal 0, s.default_queue_latency
|
||||
end
|
||||
|
||||
describe "processed" do
|
||||
|
@ -95,6 +96,28 @@ class TestApi < Sidekiq::Test
|
|||
end
|
||||
|
||||
describe "enqueued" do
|
||||
it 'handles latency for good jobs' do
|
||||
Sidekiq.redis do |conn|
|
||||
conn.rpush 'queue:default', "{\"enqueued_at\": #{Time.now.to_f}}"
|
||||
conn.sadd 'queues', 'default'
|
||||
end
|
||||
s = Sidekiq::Stats.new
|
||||
assert s.default_queue_latency > 0
|
||||
q = Sidekiq::Queue.new
|
||||
assert q.latency > 0
|
||||
end
|
||||
|
||||
it 'handles latency for incomplete jobs' do
|
||||
Sidekiq.redis do |conn|
|
||||
conn.rpush 'queue:default', '{}'
|
||||
conn.sadd 'queues', 'default'
|
||||
end
|
||||
s = Sidekiq::Stats.new
|
||||
assert_equal 0, s.default_queue_latency
|
||||
q = Sidekiq::Queue.new
|
||||
assert_equal 0, q.latency
|
||||
end
|
||||
|
||||
it "returns total enqueued jobs" do
|
||||
Sidekiq.redis do |conn|
|
||||
conn.rpush 'queue:foo', '{}'
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue