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

279 lines
7.6 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
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
enable :sessions
use ::Rack::Protection, :use => :authenticity_token unless ENV['RACK_ENV'] == 'test'
set :root, File.expand_path(File.dirname(__FILE__) + "/../../web")
set :public_folder, proc { "#{root}/assets" }
set :views, proc { "#{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
2014-08-01 19:40:42 -04:00
attr_accessor :app_url
end
get "/busy" do
erb :busy
end
post "/busy" do
if params['identity']
p = Sidekiq::Process.new('identity' => params['identity'])
2014-06-25 19:59:18 -04:00
p.quiet! if params[:quiet]
p.stop! if params[:stop]
else
2015-01-16 13:59:41 -05:00
processes.each do |pro|
pro.quiet! if params[:quiet]
pro.stop! if params[:stop]
end
end
redirect "#{root_path}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]
@queue = Sidekiq::Queue.new(@name)
2012-07-18 01:14:15 -04:00
(@current_page, @total_size, @messages) = page("queue:#{@name}", params[:page], @count)
2014-12-17 15:09:20 -05:00
@messages = @messages.map { |msg| Sidekiq::Job.new(msg, @name) }
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
2014-12-17 15:09:20 -05:00
(@current_page, @total_size, @dead) = page("dead", params[:page], @count, reverse: true)
@dead = @dead.map { |msg, score| Sidekiq::SortedEntry.new(nil, score, msg) }
2014-02-09 17:56:01 -05:00
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
redirect request.path unless params['key']
2014-02-09 17:56:01 -05:00
params['key'].each do |key|
job = Sidekiq::DeadSet.new.fetch(*parse_params(key)).first
retry_or_delete_or_kill job, params if job
2014-02-09 17:56:01 -05:00
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
retry_or_delete_or_kill job, params if job
2014-02-09 17:56:01 -05:00
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)
2014-12-17 15:09:20 -05:00
@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
@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
redirect request.path unless params['key']
params['key'].each do |key|
job = Sidekiq::RetrySet.new.fetch(*parse_params(key)).first
retry_or_delete_or_kill job, params if job
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
job = Sidekiq::RetrySet.new.fetch(*parse_params(params['key'])).first
retry_or_delete_or_kill job, params if job
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)
2014-12-17 15:09:20 -05:00
@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
@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
redirect request.path unless params['key']
params['key'].each do |key|
job = Sidekiq::ScheduledSet.new.fetch(*parse_params(key)).first
2014-05-13 13:35:37 -04:00
delete_or_add_queue job, params if job
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
2014-05-13 13:35:37 -04:00
delete_or_add_queue job, params if job
redirect_with_query("#{root_path}scheduled")
end
get '/' do
@redis_info = redis_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_version uptime_in_days connected_clients used_memory_human used_memory_peak_human)
get '/dashboard/stats' do
redirect "#{root_path}stats"
end
get '/stats' do
sidekiq_stats = Sidekiq::Stats.new
redis_stats = redis_info.select { |k, v| REDIS_KEYS.include? k }
2012-12-05 11:28:55 -05:00
content_type :json
Sidekiq.dump_json(
sidekiq: {
2015-01-16 20:54:34 -05:00
processed: sidekiq_stats.processed,
failed: sidekiq_stats.failed,
busy: sidekiq_stats.workers_size,
processes: sidekiq_stats.processes_size,
enqueued: sidekiq_stats.enqueued,
scheduled: sidekiq_stats.scheduled_size,
retries: sidekiq_stats.retry_size,
dead: sidekiq_stats.dead_size,
default_latency: sidekiq_stats.default_queue_latency
},
redis: redis_stats
)
end
get '/stats/queues' do
2015-01-16 20:54:34 -05:00
queue_stats = Sidekiq::Stats::Queues.new
content_type :json
Sidekiq.dump_json(
2015-01-16 20:54:34 -05:00
queue_stats.lengths
)
end
2014-05-13 13:35:37 -04:00
private
def retry_or_delete_or_kill job, params
2014-05-13 13:35:37 -04:00
if params['retry']
job.retry
elsif params['delete']
job.delete
elsif params['kill']
job.kill
2014-05-13 13:35:37 -04:00
end
end
def delete_or_add_queue job, params
if params['delete']
job.delete
elsif params['add_to_queue']
job.add_to_queue
end
end
end
end
if defined?(::ActionDispatch::Request::Session) &&
!::ActionDispatch::Request::Session.respond_to?(:each)
# mperham/sidekiq#2460
# Rack apps can't reuse the Rails session store without
# this monkeypatch
class ActionDispatch::Request::Session
def each(&block)
hash = self.to_hash
hash.each(&block)
end
end
end