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.


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

def initialize
  fetch_stats!
end

Instance Method Details

#dead_sizeObject


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

def dead_size
  stat :dead_size
end

#default_queue_latencyObject


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

def default_queue_latency
  stat :default_queue_latency
end

#enqueuedObject


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

def enqueued
  stat :enqueued
end

#failedObject


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

def failed
  stat :failed
end

#fetch_stats!Object


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
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/sidekiq/api.rb', line 54

def fetch_stats!
  pipe1_res = Sidekiq.redis { |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
  }

  processes = Sidekiq.redis { |conn|
    conn.sscan_each("processes").to_a
  }

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

  pipe2_res = Sidekiq.redis { |conn|
    conn.pipelined do
      processes.each { |key| conn.hget(key, "busy") }
      queues.each { |queue| conn.llen("queue:#{queue}") }
    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 = 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,
    workers_size: workers_size,
    enqueued: enqueued,
  }
end

#processedObject


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

def processed
  stat :processed
end

#processes_sizeObject


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

def processes_size
  stat :processes_size
end

#queuesObject


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

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

#reset(*stats) ⇒ Object


112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/sidekiq/api.rb', line 112

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


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

def retry_size
  stat :retry_size
end

#scheduled_sizeObject


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

def scheduled_size
  stat :scheduled_size
end

#workers_sizeObject


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

def workers_size
  stat :workers_size
end