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

257 lines
6.6 KiB
Ruby
Raw Normal View History

require 'sinatra/base'
require 'slim'
require 'sprockets'
2012-07-18 01:14:15 -04:00
require 'sidekiq/paginator'
module Sidekiq
class SprocketsMiddleware
def initialize(app, options={})
@app = app
@root = options[:root]
path = options[:path] || 'assets'
@matcher = /^\/#{path}\/*/
@environment = ::Sprockets::Environment.new(@root)
@environment.append_path 'assets/javascripts'
@environment.append_path 'assets/javascripts/vendor'
@environment.append_path 'assets/stylesheets'
@environment.append_path 'assets/stylesheets/vendor'
@environment.append_path 'assets/images'
end
def call(env)
# Solve the problem of people requesting /sidekiq when they need to request /sidekiq/ so
# that relative links in templates resolve correctly.
2012-04-27 17:10:47 -04:00
return [301, { 'Location' => "#{env['SCRIPT_NAME']}/", 'Content-Type' => 'text/html' }, ['redirecting']] if env['SCRIPT_NAME'] == env['REQUEST_PATH']
return @app.call(env) unless @matcher =~ env["PATH_INFO"]
env['PATH_INFO'].sub!(@matcher,'')
@environment.call(env)
end
end
class Web < Sinatra::Base
2012-07-18 01:14:15 -04:00
include Sidekiq::Paginator
dir = File.expand_path(File.dirname(__FILE__) + "/../../web")
set :views, "#{dir}/views"
set :root, "#{dir}/public"
set :slim, :pretty => true
use SprocketsMiddleware, :root => dir
helpers do
2012-06-05 10:51:18 -04:00
def reset_worker_list
Sidekiq.redis do |conn|
workers = conn.smembers('workers')
workers.each do |name|
conn.srem('workers', name)
end
end
end
def workers
@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}")
msg ? [w, Sidekiq.load_json(msg)] : nil
end.compact.sort { |x| x[1] ? -1 : 1 }
2012-03-07 23:22:21 -05:00
end
end
end
2012-09-04 12:45:35 -04:00
def info
@info ||= Sidekiq.info
end
2012-03-07 23:22:21 -05:00
def processed
2012-09-04 12:45:35 -04:00
info[:processed]
2012-03-07 23:22:21 -05:00
end
def failed
2012-09-04 12:45:35 -04:00
info[:failed]
2012-03-07 23:22:21 -05:00
end
def zcard(name)
Sidekiq.redis { |conn| conn.zcard(name) }
2012-03-18 15:29:09 -04:00
end
def queues
2012-09-04 01:41:32 -04:00
@queues ||= Sidekiq.info[:queues_with_sizes]
end
2012-05-22 10:16:08 -04:00
def backlog
2012-09-04 12:45:35 -04:00
info[:backlog]
2012-05-22 10:16:08 -04: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 root_path
"#{env['SCRIPT_NAME']}/"
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 display_args(args, count=100)
args.map { |arg| a = arg.inspect; a.size > count ? "#{a[0..count]}..." : a }.join(", ")
end
def 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
end
get "/" do
slim :index
end
get "/poll" do
slim :poll, layout: false
end
2012-06-08 20:20:11 -04:00
get "/queues" do
@queues = queues
slim :queues
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) }
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
post "/queues/:name" do
Sidekiq.redis do |conn|
conn.del("queue:#{params[:name]}")
conn.srem("queues", params[:name])
end
2012-06-08 20:20:11 -04:00
redirect "#{root_path}queues"
end
post "/queues/:name/delete" do
Sidekiq.redis do |conn|
conn.lrem("queue:#{params[:name]}", 0, params[:key_val])
end
redirect "#{root_path}queues/#{params[:name]}"
end
2012-03-29 13:48:06 -04:00
get "/retries/:score" do
halt 404 unless params[:score]
@score = params[:score].to_f
@retries = retries_with_score(@score)
redirect "#{root_path}retries" if @retries.empty?
2012-03-29 13:48:06 -04:00
slim :retry
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] }
slim :retries
end
get '/scheduled' do
2012-07-18 01:14:15 -04:00
@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
halt 404 unless params[:score]
halt 404 unless params['delete']
params[:score].each do |score|
s = score.to_f
process_score('schedule', s, :delete)
end
redirect "#{root_path}scheduled"
end
post '/retries' do
halt 404 unless params[:score]
params[:score].each do |score|
s = score.to_f
if params['retry']
process_score('retry', s, :retry)
elsif params['delete']
process_score('retry', s, :delete)
end
end
redirect "#{root_path}retries"
end
post "/retries/:score" do
halt 404 unless params[:score]
score = params[:score].to_f
if params['retry']
2012-06-12 21:59:43 -04:00
process_score('retry', score, :retry)
elsif params['delete']
2012-06-12 21:59:43 -04:00
process_score('retry', score, :delete)
end
redirect "#{root_path}retries"
end
def process_score(set, score, operation)
case operation
when :retry
Sidekiq.redis do |conn|
results = conn.zrangebyscore(set, score, score)
conn.zremrangebyscore(set, score, score)
results.map do |message|
msg = Sidekiq.load_json(message)
msg['retry_count'] = msg['retry_count'] - 1
conn.rpush("queue:#{msg['queue']}", Sidekiq.dump_json(msg))
end
end
when :delete
Sidekiq.redis do |conn|
conn.zremrangebyscore(set, score, score)
end
end
end
def self.tabs
@tabs ||= ["Queues", "Retries", "Scheduled"]
end
end
end