2013-04-29 17:56:05 -04:00
|
|
|
require 'yaml'
|
2012-03-04 15:58:16 -05:00
|
|
|
require 'sinatra/base'
|
|
|
|
require 'slim'
|
2013-04-29 17:56:05 -04:00
|
|
|
|
2013-06-01 17:15:14 -04:00
|
|
|
raise "The Sidekiq Web UI requires slim 1.1.0 or greater. You have slim v#{Slim::VERSION}" if Gem::Version.new(Slim::VERSION) < Gem::Version.new('1.1.0')
|
2013-05-16 11:27:10 -04:00
|
|
|
|
2013-04-29 17:56:05 -04:00
|
|
|
require 'sidekiq'
|
|
|
|
require 'sidekiq/api'
|
2012-07-18 01:14:15 -04:00
|
|
|
require 'sidekiq/paginator'
|
|
|
|
|
2012-03-04 15:58:16 -05:00
|
|
|
module Sidekiq
|
|
|
|
class Web < Sinatra::Base
|
2012-07-18 01:14:15 -04:00
|
|
|
include Sidekiq::Paginator
|
|
|
|
|
2012-03-05 22:27:27 -05:00
|
|
|
dir = File.expand_path(File.dirname(__FILE__) + "/../../web")
|
2013-04-04 00:26:44 -04:00
|
|
|
|
2012-11-02 14:11:34 -04:00
|
|
|
set :public_folder, "#{dir}/assets"
|
2012-03-04 15:58:16 -05:00
|
|
|
set :views, "#{dir}/views"
|
|
|
|
set :root, "#{dir}/public"
|
2013-04-15 13:43:31 -04:00
|
|
|
set :locales, "#{dir}/locales"
|
2012-03-05 23:53:14 -05:00
|
|
|
set :slim, :pretty => true
|
2012-09-30 08:57:51 -04:00
|
|
|
|
2012-03-05 23:53:14 -05:00
|
|
|
helpers do
|
2013-04-15 19:54:17 -04:00
|
|
|
def strings
|
|
|
|
@strings ||= begin
|
|
|
|
Dir["#{settings.locales}/*.yml"].inject({}) do |memo, file|
|
|
|
|
memo.merge(YAML.load(File.read(file)))
|
2013-04-15 15:54:17 -04:00
|
|
|
end
|
|
|
|
end
|
2013-04-15 13:43:31 -04:00
|
|
|
end
|
|
|
|
|
2013-04-04 00:26:44 -04:00
|
|
|
def get_locale
|
2013-04-25 13:29:10 -04:00
|
|
|
strings[(request.env["HTTP_ACCEPT_LANGUAGE"] || 'en')[0,2]] || strings['en']
|
2013-04-04 00:26:44 -04:00
|
|
|
end
|
|
|
|
|
2013-04-04 23:51:59 -04:00
|
|
|
def t(msg, options={})
|
2013-04-25 13:29:10 -04:00
|
|
|
string = get_locale[msg] || msg
|
2013-04-15 13:43:31 -04:00
|
|
|
string % options
|
2013-04-04 00:26:44 -04:00
|
|
|
end
|
2012-03-28 15:36:16 -04:00
|
|
|
|
2012-06-05 10:51:18 -04:00
|
|
|
def reset_worker_list
|
|
|
|
Sidekiq.redis do |conn|
|
|
|
|
workers = conn.smembers('workers')
|
2013-06-01 17:54:29 -04:00
|
|
|
conn.srem('workers', *workers)
|
2012-06-05 10:51:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-05 23:53:14 -05:00
|
|
|
def workers
|
2012-03-06 23:17:42 -05:00
|
|
|
@workers ||= begin
|
2012-03-17 12:41:24 -04:00
|
|
|
Sidekiq.redis do |conn|
|
2012-03-07 23:22:21 -05:00
|
|
|
conn.smembers('workers').map do |w|
|
|
|
|
msg = conn.get("worker:#{w}")
|
2012-04-24 10:15:29 -04:00
|
|
|
msg ? [w, Sidekiq.load_json(msg)] : nil
|
|
|
|
end.compact.sort { |x| x[1] ? -1 : 1 }
|
2012-03-07 23:22:21 -05:00
|
|
|
end
|
2012-03-06 23:17:42 -05:00
|
|
|
end
|
2012-03-05 23:53:14 -05:00
|
|
|
end
|
2012-03-06 23:17:42 -05:00
|
|
|
|
2012-12-05 11:46:55 -05:00
|
|
|
def stats
|
|
|
|
@stats ||= Sidekiq::Stats.new
|
2012-03-07 23:22:21 -05:00
|
|
|
end
|
|
|
|
|
2012-03-29 13:48:06 -04:00
|
|
|
def retries_with_score(score)
|
|
|
|
Sidekiq.redis do |conn|
|
2012-03-29 23:55:16 -04:00
|
|
|
results = conn.zrangebyscore('retry', score, score)
|
2012-04-22 17:02:35 -04:00
|
|
|
results.map { |msg| Sidekiq.load_json(msg) }
|
2012-03-29 13:48:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-05 23:53:14 -05:00
|
|
|
def location
|
2012-03-17 12:41:24 -04:00
|
|
|
Sidekiq.redis { |conn| conn.client.location }
|
2012-03-05 23:53:14 -05:00
|
|
|
end
|
2012-03-06 23:17:42 -05:00
|
|
|
|
2013-01-26 12:00:59 -05:00
|
|
|
def namespace
|
2013-03-09 00:31:57 -05:00
|
|
|
@@ns ||= Sidekiq.redis {|conn| conn.respond_to?(:namespace) ? conn.namespace : nil }
|
2013-01-26 12:00:59 -05:00
|
|
|
end
|
|
|
|
|
2012-03-05 23:53:14 -05:00
|
|
|
def root_path
|
|
|
|
"#{env['SCRIPT_NAME']}/"
|
|
|
|
end
|
2012-03-06 23:17:42 -05:00
|
|
|
|
2012-09-30 08:57:51 -04:00
|
|
|
def current_path
|
|
|
|
@current_path ||= request.path_info.gsub(/^\//,'')
|
|
|
|
end
|
|
|
|
|
2012-03-17 12:41:24 -04:00
|
|
|
def current_status
|
2012-04-24 10:15:29 -04:00
|
|
|
return 'idle' if workers.size == 0
|
2012-03-06 23:17:42 -05:00
|
|
|
return 'active'
|
|
|
|
end
|
2012-03-29 16:57:32 -04:00
|
|
|
|
|
|
|
def relative_time(time)
|
2012-03-30 01:24:22 -04:00
|
|
|
%{<time datetime="#{time.getutc.iso8601}">#{time}</time>}
|
2012-03-29 16:57:32 -04:00
|
|
|
end
|
2012-05-11 19:48:03 -04:00
|
|
|
|
2012-12-02 23:18:31 -05:00
|
|
|
def job_params(job, score)
|
|
|
|
"#{score}-#{job['jid']}"
|
|
|
|
end
|
|
|
|
|
2012-11-26 14:53:22 -05:00
|
|
|
def parse_params(params)
|
|
|
|
score, jid = params.split("-")
|
|
|
|
[score.to_f, jid]
|
|
|
|
end
|
|
|
|
|
2012-05-11 19:48:03 -04:00
|
|
|
def display_args(args, count=100)
|
2013-06-01 17:54:29 -04:00
|
|
|
args.map do |arg|
|
|
|
|
a = arg.inspect
|
|
|
|
a.size > count ? "#{a[0..count]}..." : a
|
|
|
|
end.join(", ")
|
2012-05-11 19:48:03 -04:00
|
|
|
end
|
2012-08-20 18:28:14 -04:00
|
|
|
|
2013-05-31 00:13:59 -04:00
|
|
|
RETRY_JOB_KEYS = Set.new(%w(
|
|
|
|
queue class args retry_count retried_at failed_at
|
|
|
|
jid error_message error_class backtrace
|
2013-06-01 17:54:29 -04:00
|
|
|
error_backtrace enqueued_at retry
|
2013-05-31 00:13:59 -04:00
|
|
|
))
|
2013-05-30 12:24:57 -04:00
|
|
|
|
|
|
|
def retry_extra_items(retry_job)
|
|
|
|
@retry_extra_items ||= {}.tap do |extra|
|
2013-05-30 00:07:49 -04:00
|
|
|
retry_job.item.each do |key, value|
|
2013-05-30 12:24:57 -04:00
|
|
|
extra[key] = value unless RETRY_JOB_KEYS.include?(key)
|
2013-05-30 00:07:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-20 18:28:14 -04:00
|
|
|
def tabs
|
2013-04-15 13:43:31 -04:00
|
|
|
@tabs ||= {
|
|
|
|
"Dashboard" => '',
|
|
|
|
"Workers" => 'workers',
|
|
|
|
"Queues" => 'queues',
|
|
|
|
"Retries" => 'retries',
|
|
|
|
"Scheduled" => 'scheduled',
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def custom_tabs
|
2012-08-21 11:06:20 -04:00
|
|
|
self.class.tabs
|
2012-08-20 18:28:14 -04:00
|
|
|
end
|
2012-08-31 10:53:08 -04:00
|
|
|
|
2012-09-01 00:50:19 -04:00
|
|
|
def number_with_delimiter(number)
|
2012-08-31 10:53:08 -04:00
|
|
|
begin
|
|
|
|
Float(number)
|
|
|
|
rescue ArgumentError, TypeError
|
2012-09-01 00:50:19 -04:00
|
|
|
return number
|
2012-08-31 10:53:08 -04:00
|
|
|
end
|
|
|
|
|
2012-09-01 00:50:19 -04:00
|
|
|
options = {:delimiter => ',', :separator => '.'}
|
2012-08-31 10:53:08 -04:00
|
|
|
parts = number.to_s.to_str.split('.')
|
|
|
|
parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{options[:delimiter]}")
|
2012-08-31 13:14:00 -04:00
|
|
|
parts.join(options[:separator])
|
2012-08-31 10:53:08 -04:00
|
|
|
end
|
2013-01-26 14:34:46 -05:00
|
|
|
|
|
|
|
def redis_keys
|
|
|
|
["redis_stats", "uptime_in_days", "connected_clients", "used_memory_human", "used_memory_peak_human"]
|
|
|
|
end
|
2012-03-05 23:53:14 -05:00
|
|
|
end
|
|
|
|
|
2013-02-20 11:56:53 -05:00
|
|
|
get "/workers" do
|
2012-03-04 15:58:16 -05:00
|
|
|
slim :index
|
|
|
|
end
|
2012-03-05 23:53:14 -05:00
|
|
|
|
2012-06-08 20:20:11 -04:00
|
|
|
get "/queues" do
|
2013-06-01 17:54:29 -04:00
|
|
|
@queues = Sidekiq::Queue.all
|
2012-06-08 20:20:11 -04:00
|
|
|
slim :queues
|
|
|
|
end
|
|
|
|
|
2012-03-05 23:53:14 -05:00
|
|
|
get "/queues/:name" do
|
2012-03-17 12:41:24 -04:00
|
|
|
halt 404 unless params[:name]
|
2012-07-18 01:14:15 -04:00
|
|
|
@count = (params[:count] || 25).to_i
|
2012-03-05 23:53:14 -05:00
|
|
|
@name = params[:name]
|
2012-07-18 01:14:15 -04:00
|
|
|
(@current_page, @total_size, @messages) = page("queue:#{@name}", params[:page], @count)
|
|
|
|
@messages = @messages.map {|msg| Sidekiq.load_json(msg) }
|
2012-03-05 23:53:14 -05:00
|
|
|
slim :queue
|
|
|
|
end
|
2012-03-29 13:48:06 -04:00
|
|
|
|
2012-06-05 10:51:18 -04:00
|
|
|
post "/reset" do
|
|
|
|
reset_worker_list
|
|
|
|
redirect root_path
|
|
|
|
end
|
|
|
|
|
2012-05-02 23:19:59 -04:00
|
|
|
post "/queues/:name" do
|
2012-11-25 21:43:48 -05:00
|
|
|
Sidekiq::Queue.new(params[:name]).clear
|
2012-06-08 20:20:11 -04:00
|
|
|
redirect "#{root_path}queues"
|
2012-05-02 23:19:59 -04:00
|
|
|
end
|
|
|
|
|
2012-10-23 19:11:14 -04:00
|
|
|
post "/queues/:name/delete" do
|
2012-12-02 16:23:05 -05:00
|
|
|
Sidekiq::Job.new(params[:key_val], params[:name]).delete
|
2012-10-23 19:11:14 -04:00
|
|
|
redirect "#{root_path}queues/#{params[:name]}"
|
|
|
|
end
|
|
|
|
|
2012-05-11 19:48:03 -04:00
|
|
|
get '/retries' do
|
2012-07-18 01:14:15 -04:00
|
|
|
@count = (params[:count] || 25).to_i
|
|
|
|
(@current_page, @total_size, @retries) = page("retry", params[:page], @count)
|
|
|
|
@retries = @retries.map {|msg, score| [Sidekiq.load_json(msg), score] }
|
2012-05-11 19:48:03 -04:00
|
|
|
slim :retries
|
|
|
|
end
|
|
|
|
|
2012-11-26 14:53:22 -05:00
|
|
|
get "/retries/:key" do
|
|
|
|
halt 404 unless params['key']
|
|
|
|
@retry = Sidekiq::RetrySet.new.fetch(*parse_params(params['key'])).first
|
2012-11-26 11:22:48 -05:00
|
|
|
redirect "#{root_path}retries" if @retry.nil?
|
|
|
|
slim :retry
|
2012-05-25 23:21:42 -04:00
|
|
|
end
|
|
|
|
|
2012-05-11 19:48:03 -04:00
|
|
|
post '/retries' do
|
2012-11-26 14:53:22 -05:00
|
|
|
halt 404 unless params['key']
|
|
|
|
|
|
|
|
params['key'].each do |key|
|
|
|
|
job = Sidekiq::RetrySet.new.fetch(*parse_params(key)).first
|
|
|
|
if params['retry']
|
|
|
|
job.retry
|
|
|
|
elsif params['delete']
|
|
|
|
job.delete
|
|
|
|
end
|
2012-05-11 19:48:03 -04:00
|
|
|
end
|
2012-06-18 07:59:25 -04:00
|
|
|
redirect "#{root_path}retries"
|
2012-05-11 19:48:03 -04:00
|
|
|
end
|
|
|
|
|
2012-11-25 21:43:48 -05:00
|
|
|
post "/retries/all/delete" do
|
|
|
|
Sidekiq::RetrySet.new.clear
|
|
|
|
redirect "#{root_path}retries"
|
|
|
|
end
|
|
|
|
|
|
|
|
post "/retries/all/retry" do
|
2013-02-19 23:36:59 -05:00
|
|
|
Sidekiq::RetrySet.new.retry_all
|
2012-11-25 21:43:48 -05:00
|
|
|
redirect "#{root_path}retries"
|
2012-05-11 19:48:03 -04:00
|
|
|
end
|
|
|
|
|
2012-11-26 14:53:22 -05:00
|
|
|
post "/retries/:key" do
|
|
|
|
halt 404 unless params['key']
|
|
|
|
job = Sidekiq::RetrySet.new.fetch(*parse_params(params['key'])).first
|
2012-03-29 23:55:16 -04:00
|
|
|
if params['retry']
|
2012-11-26 14:53:22 -05:00
|
|
|
job.retry
|
2012-05-11 19:48:03 -04:00
|
|
|
elsif params['delete']
|
2012-11-26 14:53:22 -05:00
|
|
|
job.delete
|
2012-05-11 19:48:03 -04:00
|
|
|
end
|
2012-06-18 07:59:25 -04:00
|
|
|
redirect "#{root_path}retries"
|
2012-05-11 19:48:03 -04:00
|
|
|
end
|
|
|
|
|
2012-11-26 11:22:48 -05:00
|
|
|
get '/scheduled' do
|
|
|
|
@count = (params[:count] || 25).to_i
|
|
|
|
(@current_page, @total_size, @scheduled) = page("schedule", params[:page], @count)
|
|
|
|
@scheduled = @scheduled.map {|msg, score| [Sidekiq.load_json(msg), score] }
|
|
|
|
slim :scheduled
|
|
|
|
end
|
|
|
|
|
|
|
|
post '/scheduled' do
|
2012-11-26 14:53:22 -05:00
|
|
|
halt 404 unless params['key']
|
2012-11-26 11:22:48 -05:00
|
|
|
halt 404 unless params['delete']
|
2012-11-26 14:53:22 -05:00
|
|
|
params['key'].each do |key|
|
|
|
|
Sidekiq::ScheduledSet.new.fetch(*parse_params(key)).first.delete
|
|
|
|
end
|
2012-11-26 11:22:48 -05:00
|
|
|
redirect "#{root_path}scheduled"
|
2012-03-29 23:55:16 -04:00
|
|
|
end
|
2012-05-11 19:48:03 -04:00
|
|
|
|
2013-02-20 11:56:53 -05:00
|
|
|
get '/' do
|
2013-01-26 14:34:46 -05:00
|
|
|
@redis_info = Sidekiq.redis { |conn| conn.info }.select{ |k, v| redis_keys.include? k }
|
2012-12-05 11:28:55 -05:00
|
|
|
stats_history = Sidekiq::Stats::History.new((params[:days] || 30).to_i)
|
|
|
|
@processed_history = stats_history.processed
|
|
|
|
@failed_history = stats_history.failed
|
|
|
|
slim :dashboard
|
|
|
|
end
|
|
|
|
|
2012-12-12 11:49:20 -05:00
|
|
|
get '/dashboard/stats' do
|
2013-01-26 14:34:46 -05:00
|
|
|
sidekiq_stats = Sidekiq::Stats.new
|
|
|
|
redis_stats = Sidekiq.redis { |conn| conn.info }.select{ |k, v| redis_keys.include? k }
|
|
|
|
|
2012-12-05 11:28:55 -05:00
|
|
|
content_type :json
|
2012-12-28 20:55:00 -05:00
|
|
|
Sidekiq.dump_json({
|
2013-01-26 14:34:46 -05:00
|
|
|
sidekiq: {
|
|
|
|
processed: sidekiq_stats.processed,
|
|
|
|
failed: sidekiq_stats.failed,
|
2013-02-18 10:15:51 -05:00
|
|
|
busy: workers.size,
|
2013-01-26 14:34:46 -05:00
|
|
|
enqueued: sidekiq_stats.enqueued,
|
|
|
|
scheduled: sidekiq_stats.scheduled_size,
|
|
|
|
retries: sidekiq_stats.retry_size,
|
|
|
|
},
|
|
|
|
redis: redis_stats
|
2012-12-28 20:55:00 -05:00
|
|
|
})
|
2012-12-05 11:28:55 -05:00
|
|
|
end
|
|
|
|
|
2012-08-20 18:28:14 -04:00
|
|
|
def self.tabs
|
2013-04-15 13:43:31 -04:00
|
|
|
@custom_tabs ||= {}
|
2012-08-20 18:28:14 -04:00
|
|
|
end
|
|
|
|
|
2012-03-04 15:58:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2012-11-02 14:11:34 -04:00
|
|
|
|