Module: Sidekiq::ApiUtils

Included in:
JobRecord, Queue, Stats
Defined in:
lib/sidekiq/api.rb

Instance Method Summary collapse

Instance Method Details

#calculate_latency(job) ⇒ Float

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Calculate the latency in seconds for a job based on its enqueued timestamp

Parameters:

  • job (Hash)

    the job hash

Returns:

  • (Float)

    latency in seconds



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sidekiq/api.rb', line 25

def calculate_latency(job)
  timestamp = job["enqueued_at"] || job["created_at"]
  return 0.0 unless timestamp

  if timestamp.is_a?(Float)
    # old format
    Time.now.to_f - timestamp
  else
    now = ::Process.clock_gettime(::Process::CLOCK_REALTIME, :millisecond)
    (now - timestamp) / 1000.0
  end
end