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.



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

def initialize
  fetch_stats!
end

Instance Method Details

#dead_sizeObject



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

def dead_size
  stat :dead_size
end

#default_queue_latencyObject



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

def default_queue_latency
  stat :default_queue_latency
end

#enqueuedObject



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

def enqueued
  stat :enqueued
end

#failedObject



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

def failed
  stat :failed
end

#fetch_stats!Object



50
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
# File 'lib/sidekiq/api.rb', line 50

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)
                            Time.now.to_f - Sidekiq.load_json(entry)['enqueued_at'.freeze]
                          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



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

def processed
  stat :processed
end

#processes_sizeObject



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

def processes_size
  stat :processes_size
end

#queuesObject



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

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

#reset(*stats) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/sidekiq/api.rb', line 95

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



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

def retry_size
  stat :retry_size
end

#scheduled_sizeObject



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

def scheduled_size
  stat :scheduled_size
end

#workers_sizeObject



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

def workers_size
  stat :workers_size
end