1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00
mperham--sidekiq/lib/sidekiq/web.rb

341 lines
8.7 KiB
Ruby
Raw Normal View History

2013-08-22 03:32:42 -04:00
require 'erb'
2013-04-29 17:56:05 -04:00
require 'yaml'
require 'sinatra/base'
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'
module Sidekiq
class Web < Sinatra::Base
2012-07-18 01:14:15 -04:00
include Sidekiq::Paginator
set :root, File.expand_path(File.dirname(__FILE__) + "/../../web")
set :public_folder, Proc.new { "#{root}/assets" }
set :views, Proc.new { "#{root}/views" }
set :locales, Proc.new { "#{root}/locales" }
2012-09-30 08:57:51 -04:00
helpers do
def strings
@strings ||= begin
Dir["#{settings.locales}/*.yml"].inject({}) do |memo, file|
memo.merge(YAML.load(File.open(file)))
end
end
2013-04-15 13:43:31 -04:00
end
def locale
lang = (request.env["HTTP_ACCEPT_LANGUAGE"] || 'en')[0,2]
strings[lang] ? lang : 'en'
end
def get_locale
strings[locale]
end
2013-04-04 23:51:59 -04:00
def t(msg, options={})
string = get_locale[msg] || msg
2013-04-15 13:43:31 -04:00
string % options
end
2012-06-05 10:51:18 -04:00
def reset_worker_list
Sidekiq.redis do |conn|
workers = conn.smembers('workers')
2013-06-24 16:38:03 -04:00
conn.srem('workers', workers) if !workers.empty?
2012-06-05 10:51:18 -04:00
end
end
def workers_size
2013-07-28 12:17:33 -04:00
@workers_size ||= Sidekiq.redis do |conn|
conn.scard('workers')
end
end
def workers
@workers ||= begin
to_rem = []
workers = Sidekiq.redis do |conn|
2012-03-07 23:22:21 -05:00
conn.smembers('workers').map do |w|
msg = conn.get("worker:#{w}")
msg ? [w, Sidekiq.load_json(msg)] : (to_rem << w; nil)
end.compact.sort { |x| x[1] ? -1 : 1 }
2012-03-07 23:22:21 -05:00
end
# Detect and clear out any orphaned worker records.
# These can be left in Redis if Sidekiq crashes hard
# while processing jobs.
if to_rem.size > 0
Sidekiq.redis { |conn| conn.srem('workers', to_rem) }
end
workers
end
end
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|
results = conn.zrangebyscore('retry', score, score)
results.map { |msg| Sidekiq.load_json(msg) }
2012-03-29 13:48:06 -04:00
end
end
def location
2012-03-17 12:41:24 -04:00
Sidekiq.redis { |conn| conn.client.location }
end
def namespace
2013-03-09 00:31:57 -05:00
@@ns ||= Sidekiq.redis {|conn| conn.respond_to?(:namespace) ? conn.namespace : nil }
end
def root_path
"#{env['SCRIPT_NAME']}/"
end
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
return 'idle' if workers_size == 0
return 'active'
end
def relative_time(time)
2012-03-30 01:24:22 -04:00
%{<time datetime="#{time.getutc.iso8601}">#{time}</time>}
end
def job_params(job, score)
"#{score}-#{job['jid']}"
end
def parse_params(params)
score, jid = params.split("-")
[score.to_f, jid]
end
2013-09-19 06:10:14 -04:00
def truncate(text, truncate_after_chars = 2000)
truncate_after_chars && text.size > truncate_after_chars ? "#{text[0..truncate_after_chars]}..." : text
end
def display_args(args, truncate_after_chars = 2000)
args.map do |arg|
a = arg.inspect
2013-09-19 06:10:14 -04:00
truncate(a)
end.join(", ")
end
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
error_backtrace enqueued_at retry
2013-05-31 00:13:59 -04:00
))
def retry_extra_items(retry_job)
@retry_extra_items ||= {}.tap do |extra|
retry_job.item.each do |key, value|
extra[key] = value unless RETRY_JOB_KEYS.include?(key)
end
end
end
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
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
def redis_keys
["redis_stats", "uptime_in_days", "connected_clients", "used_memory_human", "used_memory_peak_human"]
end
2013-08-22 03:32:42 -04:00
def h(text)
ERB::Util.h(text)
end
end
get "/workers" do
2013-08-21 18:38:52 -04:00
erb :index
end
2012-06-08 20:20:11 -04:00
get "/queues" do
@queues = Sidekiq::Queue.all
2013-08-21 18:38:52 -04:00
erb :queues
2012-06-08 20:20:11 -04:00
end
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
@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) }
2013-08-21 18:38:52 -04:00
erb :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
post "/queues/:name" do
Sidekiq::Queue.new(params[:name]).clear
2012-06-08 20:20:11 -04:00
redirect "#{root_path}queues"
end
post "/queues/:name/delete" do
Sidekiq::Job.new(params[:key_val], params[:name]).delete
redirect "#{root_path}queues/#{params[:name]}"
end
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] }
2013-08-21 18:38:52 -04:00
erb :retries
end
get "/retries/:key" do
halt 404 unless params['key']
@retry = Sidekiq::RetrySet.new.fetch(*parse_params(params['key'])).first
redirect "#{root_path}retries" if @retry.nil?
2013-08-21 18:38:52 -04:00
erb :retry
end
post '/retries' do
halt 404 unless params['key']
params['key'].each do |key|
job = Sidekiq::RetrySet.new.fetch(*parse_params(key)).first
next unless job
if params['retry']
job.retry
elsif params['delete']
job.delete
end
end
redirect "#{root_path}retries"
end
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
redirect "#{root_path}retries"
end
post "/retries/:key" do
halt 404 unless params['key']
job = Sidekiq::RetrySet.new.fetch(*parse_params(params['key'])).first
2013-08-15 22:52:50 -04:00
if job
if params['retry']
job.retry
elsif params['delete']
job.delete
end
end
redirect "#{root_path}retries"
end
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] }
2013-08-21 18:38:52 -04:00
erb :scheduled
end
get "/scheduled/:key" do
halt 404 unless params['key']
@job = Sidekiq::ScheduledSet.new.fetch(*parse_params(params['key'])).first
redirect "#{root_path}scheduled" if @job.nil?
2013-08-21 18:38:52 -04:00
erb :scheduled_job_info
end
post '/scheduled' do
halt 404 unless params['key']
params['key'].each do |key|
job = Sidekiq::ScheduledSet.new.fetch(*parse_params(key)).first
2013-08-15 22:52:50 -04:00
if job
if params['delete']
job.delete
elsif params['add_to_queue']
job.add_to_queue
end
end
end
redirect "#{root_path}scheduled"
end
post "/scheduled/:key" do
halt 404 unless params['key']
job = Sidekiq::ScheduledSet.new.fetch(*parse_params(params['key'])).first
2013-08-15 22:52:50 -04:00
if job
if params['add_to_queue']
job.add_to_queue
elsif params['delete']
job.delete
end
end
redirect "#{root_path}scheduled"
end
get '/' do
@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
2013-08-21 18:38:52 -04:00
erb :dashboard
2012-12-05 11:28:55 -05:00
end
get '/dashboard/stats' do
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
Sidekiq.dump_json({
sidekiq: {
processed: sidekiq_stats.processed,
failed: sidekiq_stats.failed,
busy: workers_size,
enqueued: sidekiq_stats.enqueued,
scheduled: sidekiq_stats.scheduled_size,
retries: sidekiq_stats.retry_size,
},
redis: redis_stats
})
2012-12-05 11:28:55 -05:00
end
def self.tabs
2013-04-15 13:43:31 -04:00
@custom_tabs ||= {}
end
end
end