2019-03-01 16:25:56 -05:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
2019-04-01 12:20:41 -04:00
|
|
|
require "fileutils"
|
|
|
|
require "sidekiq/api"
|
2019-03-01 16:25:56 -05:00
|
|
|
|
2019-08-02 14:07:06 -04:00
|
|
|
class Sidekiq::Monitor
|
2019-03-01 16:25:56 -05:00
|
|
|
class Status
|
|
|
|
VALID_SECTIONS = %w[all version overview processes queues]
|
|
|
|
COL_PAD = 2
|
|
|
|
|
|
|
|
def display(section = nil)
|
2019-04-01 12:20:41 -04:00
|
|
|
section ||= "all"
|
2019-03-01 16:25:56 -05:00
|
|
|
unless VALID_SECTIONS.include? section
|
|
|
|
puts "I don't know how to check the status of '#{section}'!"
|
2019-04-01 12:20:41 -04:00
|
|
|
puts "Try one of these: #{VALID_SECTIONS.join(", ")}"
|
2019-03-01 16:25:56 -05:00
|
|
|
return
|
|
|
|
end
|
|
|
|
send(section)
|
2019-04-01 12:20:41 -04:00
|
|
|
rescue => e
|
2022-02-15 10:05:07 -05:00
|
|
|
abort "Couldn't get status: #{e}"
|
2019-03-01 16:25:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def all
|
|
|
|
version
|
|
|
|
puts
|
|
|
|
overview
|
|
|
|
puts
|
|
|
|
processes
|
|
|
|
puts
|
|
|
|
queues
|
|
|
|
end
|
|
|
|
|
|
|
|
def version
|
|
|
|
puts "Sidekiq #{Sidekiq::VERSION}"
|
2019-09-04 09:25:52 -04:00
|
|
|
puts Time.now.utc
|
2019-03-01 16:25:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def overview
|
2019-04-01 12:20:41 -04:00
|
|
|
puts "---- Overview ----"
|
2019-03-01 16:25:56 -05:00
|
|
|
puts " Processed: #{delimit stats.processed}"
|
|
|
|
puts " Failed: #{delimit stats.failed}"
|
|
|
|
puts " Busy: #{delimit stats.workers_size}"
|
|
|
|
puts " Enqueued: #{delimit stats.enqueued}"
|
|
|
|
puts " Retries: #{delimit stats.retry_size}"
|
|
|
|
puts " Scheduled: #{delimit stats.scheduled_size}"
|
|
|
|
puts " Dead: #{delimit stats.dead_size}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def processes
|
|
|
|
puts "---- Processes (#{process_set.size}) ----"
|
|
|
|
process_set.each_with_index do |process, index|
|
2019-04-01 12:20:41 -04:00
|
|
|
puts "#{process["identity"]} #{tags_for(process)}"
|
|
|
|
puts " Started: #{Time.at(process["started_at"])} (#{time_ago(process["started_at"])})"
|
|
|
|
puts " Threads: #{process["concurrency"]} (#{process["busy"]} busy)"
|
|
|
|
puts " Queues: #{split_multiline(process["queues"].sort, pad: 11)}"
|
|
|
|
puts "" unless (index + 1) == process_set.size
|
2019-03-01 16:25:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def queues
|
|
|
|
puts "---- Queues (#{queue_data.size}) ----"
|
|
|
|
columns = {
|
2019-04-01 12:20:41 -04:00
|
|
|
name: [:ljust, (["name"] + queue_data.map(&:name)).map(&:length).max + COL_PAD],
|
|
|
|
size: [:rjust, (["size"] + queue_data.map(&:size)).map(&:length).max + COL_PAD],
|
2020-03-17 16:38:48 -04:00
|
|
|
latency: [:rjust, (["latency"] + queue_data.map(&:latency)).map(&:length).max + COL_PAD]
|
2019-03-01 16:25:56 -05:00
|
|
|
}
|
|
|
|
columns.each { |col, (dir, width)| print col.to_s.upcase.public_send(dir, width) }
|
|
|
|
puts
|
|
|
|
queue_data.each do |q|
|
|
|
|
columns.each do |col, (dir, width)|
|
|
|
|
print q.send(col).public_send(dir, width)
|
|
|
|
end
|
|
|
|
puts
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def delimit(number)
|
2019-04-01 12:20:41 -04:00
|
|
|
number.to_s.reverse.scan(/.{1,3}/).join(",").reverse
|
2019-03-01 16:25:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def split_multiline(values, opts = {})
|
2019-04-01 12:20:41 -04:00
|
|
|
return "none" unless values
|
2019-03-01 16:25:56 -05:00
|
|
|
pad = opts[:pad] || 0
|
|
|
|
max_length = opts[:max_length] || (80 - pad)
|
|
|
|
out = []
|
2019-04-01 12:20:41 -04:00
|
|
|
line = ""
|
2019-03-01 16:25:56 -05:00
|
|
|
values.each do |value|
|
|
|
|
if (line.length + value.length) > max_length
|
|
|
|
out << line
|
2019-04-01 12:20:41 -04:00
|
|
|
line = " " * pad
|
2019-03-01 16:25:56 -05:00
|
|
|
end
|
2019-04-01 12:20:41 -04:00
|
|
|
line << value + ", "
|
2019-03-01 16:25:56 -05:00
|
|
|
end
|
|
|
|
out << line[0..-3]
|
|
|
|
out.join("\n")
|
|
|
|
end
|
|
|
|
|
|
|
|
def tags_for(process)
|
|
|
|
tags = [
|
2019-04-01 12:20:41 -04:00
|
|
|
process["tag"],
|
|
|
|
process["labels"],
|
2020-03-17 16:38:48 -04:00
|
|
|
(process["quiet"] == "true" ? "quiet" : nil)
|
2019-03-01 16:25:56 -05:00
|
|
|
].flatten.compact
|
2019-04-01 12:20:41 -04:00
|
|
|
tags.any? ? "[#{tags.join("] [")}]" : nil
|
2019-03-01 16:25:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def time_ago(timestamp)
|
|
|
|
seconds = Time.now - Time.at(timestamp)
|
2019-04-01 12:20:41 -04:00
|
|
|
return "just now" if seconds < 60
|
|
|
|
return "a minute ago" if seconds < 120
|
2019-03-01 16:25:56 -05:00
|
|
|
return "#{seconds.floor / 60} minutes ago" if seconds < 3600
|
2019-04-01 12:20:41 -04:00
|
|
|
return "an hour ago" if seconds < 7200
|
2019-03-01 16:25:56 -05:00
|
|
|
"#{seconds.floor / 60 / 60} hours ago"
|
|
|
|
end
|
|
|
|
|
|
|
|
QUEUE_STRUCT = Struct.new(:name, :size, :latency)
|
|
|
|
def queue_data
|
2019-04-01 12:20:41 -04:00
|
|
|
@queue_data ||= Sidekiq::Queue.all.map { |q|
|
|
|
|
QUEUE_STRUCT.new(q.name, q.size.to_s, sprintf("%#.2f", q.latency))
|
|
|
|
}
|
2019-03-01 16:25:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def process_set
|
|
|
|
@process_set ||= Sidekiq::ProcessSet.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def stats
|
|
|
|
@stats ||= Sidekiq::Stats.new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|