Class: Sidekiq::Stats

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

Overview

Retrieve runtime statistics from Redis regarding this Sidekiq cluster.

stat = Sidekiq::Stats.new
stat.processed

Defined Under Namespace

Classes: History

Instance Method Summary collapse

Constructor Details

#initializeStats

Returns a new instance of Stats.



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

def initialize
  fetch_stats_fast!
end

Instance Method Details

#dead_sizeObject



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

def dead_size
  stat :dead_size
end

#default_queue_latencyObject



65
66
67
# File 'lib/sidekiq/api.rb', line 65

def default_queue_latency
  stat :default_queue_latency
end

#enqueuedObject



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

def enqueued
  stat :enqueued
end

#failedObject



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

def failed
  stat :failed
end

#fetch_stats!Object

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.



152
153
154
155
# File 'lib/sidekiq/api.rb', line 152

def fetch_stats!
  fetch_stats_fast!
  fetch_stats_slow!
end

#fetch_stats_fast!Object

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.

O(1) redis calls



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
119
120
121
122
# File 'lib/sidekiq/api.rb', line 86

def fetch_stats_fast!
  pipe1_res = Sidekiq.redis { |conn|
    conn.pipelined do |pipeline|
      pipeline.get("stat:processed")
      pipeline.get("stat:failed")
      pipeline.zcard("schedule")
      pipeline.zcard("retry")
      pipeline.zcard("dead")
      pipeline.scard("processes")
      pipeline.lindex("queue:default", -1)
    end
  }

  default_queue_latency = if (entry = pipe1_res[6])
    job = begin
      Sidekiq.load_json(entry)
    rescue
      {}
    end
    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
  }
end

#fetch_stats_slow!Object

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.

O(number of processes + number of queues) redis calls



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/sidekiq/api.rb', line 126

def fetch_stats_slow!
  processes = Sidekiq.redis { |conn|
    conn.sscan("processes").to_a
  }

  queues = Sidekiq.redis { |conn|
    conn.sscan("queues").to_a
  }

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

  s = processes.size
  workers_size = pipe2_res[0...s].sum(&:to_i)
  enqueued = pipe2_res[s..].sum(&:to_i)

  @stats[:workers_size] = workers_size
  @stats[:enqueued] = enqueued
  @stats
end

#processedObject



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

def processed
  stat :processed
end

#processes_sizeObject



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

def processes_size
  stat :processes_size
end

#queuesObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sidekiq/api.rb', line 69

def queues
  Sidekiq.redis do |conn|
    queues = conn.sscan("queues").to_a

    lengths = conn.pipelined { |pipeline|
      queues.each do |queue|
        pipeline.llen("queue:#{queue}")
      end
    }

    array_of_arrays = queues.zip(lengths).sort_by { |_, size| -size }
    array_of_arrays.to_h
  end
end

#reset(*stats) ⇒ Object

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.



158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/sidekiq/api.rb', line 158

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



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

def retry_size
  stat :retry_size
end

#scheduled_sizeObject



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

def scheduled_size
  stat :scheduled_size
end

#workers_sizeObject



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

def workers_size
  stat :workers_size
end