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

249 lines
6.6 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'
require 'sidekiq/web_helpers'
2012-07-18 01:14:15 -04:00
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, ["#{root}/locales"]
2012-09-30 08:57:51 -04:00
helpers WebHelpers
DEFAULT_TABS = {
"Dashboard" => '',
"Busy" => 'busy',
"Queues" => 'queues',
"Retries" => 'retries',
"Scheduled" => 'scheduled',
2014-02-09 17:56:01 -05:00
"Dead" => 'morgue',
}
class << self
def default_tabs
DEFAULT_TABS
2013-04-15 13:43:31 -04:00
end
def custom_tabs
@custom_tabs ||= {}
2013-08-22 03:32:42 -04:00
end
alias_method :tabs, :custom_tabs
end
get "/busy" do
erb :busy
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
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_with_query("#{root_path}queues/#{params[:name]}")
end
2014-02-09 17:56:01 -05:00
get '/morgue' do
@count = (params[:count] || 25).to_i
(@current_page, @total_size, @dead) = page("dead", params[:page], @count)
@dead = @dead.map {|msg, score| Sidekiq::SortedEntry.new(nil, score, msg) }
erb :morgue
end
get "/morgue/:key" do
halt 404 unless params['key']
@dead = Sidekiq::DeadSet.new.fetch(*parse_params(params['key'])).first
redirect "#{root_path}morgue" if @dead.nil?
erb :dead
end
post '/morgue' do
halt 404 unless params['key']
params['key'].each do |key|
job = Sidekiq::DeadSet.new.fetch(*parse_params(key)).first
next unless job
if params['retry']
job.retry
elsif params['delete']
job.delete
end
end
redirect_with_query("#{root_path}morgue")
end
post "/morgue/all/delete" do
Sidekiq::DeadSet.new.clear
redirect "#{root_path}morgue"
end
post "/morgue/all/retry" do
Sidekiq::DeadSet.new.retry_all
redirect "#{root_path}morgue"
end
post "/morgue/:key" do
halt 404 unless params['key']
job = Sidekiq::DeadSet.new.fetch(*parse_params(params['key'])).first
if job
if params['retry']
job.retry
elsif params['delete']
job.delete
end
end
redirect_with_query("#{root_path}morgue")
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::SortedEntry.new(nil, score, msg) }
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_with_query("#{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_with_query("#{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::SortedEntry.new(nil, score, msg) }
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_with_query("#{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_with_query("#{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
REDIS_KEYS = %w(redis_stats uptime_in_days connected_clients used_memory_human used_memory_peak_human)
get '/dashboard/stats' do
sidekiq_stats = Sidekiq::Stats.new
2013-10-04 03:18:04 -04:00
queue = Sidekiq::Queue.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,
default_latency: queue.latency,
},
redis: redis_stats
})
2012-12-05 11:28:55 -05:00
end
end
end