Class: Sidekiq::Stats

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq/api.rb

Defined Under Namespace

Classes: History, Queues

Instance Method Summary collapse

Constructor Details

#initializeStats

Returns a new instance of Stats.


7
8
9
# File 'lib/sidekiq/api.rb', line 7

def initialize
  fetch_stats!
end

Instance Method Details

#dead_sizeObject


27
28
29
# File 'lib/sidekiq/api.rb', line 27

def dead_size
  stat :dead_size
end

#default_queue_latencyObject


43
44
45
# File 'lib/sidekiq/api.rb', line 43

def default_queue_latency
  stat :default_queue_latency
end

#enqueuedObject


31
32
33
# File 'lib/sidekiq/api.rb', line 31

def enqueued
  stat :enqueued
end

#failedObject


15
16
17
# File 'lib/sidekiq/api.rb', line 15

def failed
  stat :failed
end

#fetch_stats!Object


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/sidekiq/api.rb', line 51

def fetch_stats!
  pipe1_res = Sidekiq.redis do |conn|
    conn.pipelined do
      conn.get('stat:processed'.freeze)
      conn.get('stat:failed'.freeze)
      conn.zcard('schedule'.freeze)
      conn.zcard('retry'.freeze)
      conn.zcard('dead'.freeze)
      conn.scard('processes'.freeze)
      conn.lrange('queue:default'.freeze, -1, -1)
      conn.smembers('processes'.freeze)
      conn.smembers('queues'.freeze)
    end
  end

  pipe2_res = Sidekiq.redis do |conn|
    conn.pipelined do
      pipe1_res[7].each {|key| conn.hget(key, 'busy'.freeze) }
      pipe1_res[8].each {|queue| conn.llen("queue:#{queue}") }
    end
  end

  s = pipe1_res[7].size
  workers_size = pipe2_res[0...s].map(&:to_i).inject(0, &:+)
  enqueued     = pipe2_res[s..-1].map(&:to_i).inject(0, &:+)

  default_queue_latency = if (entry = pipe1_res[6].first)
                            job = Sidekiq.load_json(entry) rescue {}
                            now = Time.now.to_f
                            thence = job['enqueued_at'.freeze] || now
                            now - thence
                          else
                            0
                          end
  @stats = {
    processed:             pipe1_res[0].to_i,
    failed:                pipe1_res[1].to_i,
    scheduled_size:        pipe1_res[2],
    retry_size:            pipe1_res[3],
    dead_size:             pipe1_res[4],
    processes_size:        pipe1_res[5],

    default_queue_latency: default_queue_latency,
    workers_size:          workers_size,
    enqueued:              enqueued
  }
end

#processedObject


11
12
13
# File 'lib/sidekiq/api.rb', line 11

def processed
  stat :processed
end

#processes_sizeObject


35
36
37
# File 'lib/sidekiq/api.rb', line 35

def processes_size
  stat :processes_size
end

#queuesObject


47
48
49
# File 'lib/sidekiq/api.rb', line 47

def queues
  Sidekiq::Stats::Queues.new.lengths
end

#reset(*stats) ⇒ Object


99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/sidekiq/api.rb', line 99

def reset(*stats)
  all   = %w(failed processed)
  stats = stats.empty? ? all : all & stats.flatten.compact.map(&:to_s)

  mset_args = []
  stats.each do |stat|
    mset_args << "stat:#{stat}"
    mset_args << 0
  end
  Sidekiq.redis do |conn|
    conn.mset(*mset_args)
  end
end

#retry_sizeObject


23
24
25
# File 'lib/sidekiq/api.rb', line 23

def retry_size
  stat :retry_size
end

#scheduled_sizeObject


19
20
21
# File 'lib/sidekiq/api.rb', line 19

def scheduled_size
  stat :scheduled_size
end

#workers_sizeObject


39
40
41
# File 'lib/sidekiq/api.rb', line 39

def workers_size
  stat :workers_size
end