Class: Sidekiq::Stats

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

Defined Under Namespace

Classes: History, Queues

Instance Method Summary collapse

Methods included from RedisScanner

#sscan

Constructor Details

#initializeStats

Returns a new instance of Stats.


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

def initialize
  fetch_stats!
end

Instance Method Details

#dead_sizeObject


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

def dead_size
  stat :dead_size
end

#default_queue_latencyObject


58
59
60
# File 'lib/sidekiq/api.rb', line 58

def default_queue_latency
  stat :default_queue_latency
end

#enqueuedObject


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

def enqueued
  stat :enqueued
end

#failedObject


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

def failed
  stat :failed
end

#fetch_stats!Object


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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/sidekiq/api.rb', line 66

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

  processes = Sidekiq.redis do |conn|
    sscan(conn, 'processes')
  end

  queues = Sidekiq.redis do |conn|
    sscan(conn, 'queues')
  end

  pipe2_res = Sidekiq.redis do |conn|
    conn.pipelined do
      processes.each {|key| conn.hget(key, 'busy') }
      queues.each {|queue| conn.llen("queue:#{queue}") }
    end
  end

  s = processes.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'] || 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


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

def processed
  stat :processed
end

#processes_sizeObject


50
51
52
# File 'lib/sidekiq/api.rb', line 50

def processes_size
  stat :processes_size
end

#queuesObject


62
63
64
# File 'lib/sidekiq/api.rb', line 62

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

#reset(*stats) ⇒ Object


120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/sidekiq/api.rb', line 120

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


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

def retry_size
  stat :retry_size
end

#scheduled_sizeObject


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

def scheduled_size
  stat :scheduled_size
end

#workers_sizeObject


54
55
56
# File 'lib/sidekiq/api.rb', line 54

def workers_size
  stat :workers_size
end