2012-03-04 15:58:16 -05:00
|
|
|
require 'sinatra/base'
|
|
|
|
require 'slim'
|
|
|
|
require 'sprockets'
|
|
|
|
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)
|
2012-03-05 22:27:27 -05:00
|
|
|
# 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']
|
2012-03-05 22:27:27 -05:00
|
|
|
|
2012-03-04 15:58:16 -05:00
|
|
|
return @app.call(env) unless @matcher =~ env["PATH_INFO"]
|
|
|
|
env['PATH_INFO'].sub!(@matcher,'')
|
|
|
|
@environment.call(env)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Web < Sinatra::Base
|
2012-03-05 22:27:27 -05:00
|
|
|
dir = File.expand_path(File.dirname(__FILE__) + "/../../web")
|
2012-03-04 15:58:16 -05:00
|
|
|
set :views, "#{dir}/views"
|
|
|
|
set :root, "#{dir}/public"
|
2012-03-05 23:53:14 -05:00
|
|
|
set :slim, :pretty => true
|
2012-03-05 22:27:27 -05:00
|
|
|
use SprocketsMiddleware, :root => dir
|
2012-03-04 15:58:16 -05:00
|
|
|
|
2012-03-05 23:53:14 -05:00
|
|
|
helpers do
|
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')
|
|
|
|
workers.each do |name|
|
|
|
|
conn.srem('workers', name)
|
|
|
|
end
|
|
|
|
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-03-07 23:22:21 -05:00
|
|
|
def processed
|
2012-03-17 12:41:24 -04:00
|
|
|
Sidekiq.redis { |conn| conn.get('stat:processed') } || 0
|
2012-03-07 23:22:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def failed
|
2012-03-17 12:41:24 -04:00
|
|
|
Sidekiq.redis { |conn| conn.get('stat:failed') } || 0
|
2012-03-07 23:22:21 -05:00
|
|
|
end
|
|
|
|
|
2012-05-25 23:21:42 -04:00
|
|
|
def zcard(name)
|
|
|
|
Sidekiq.redis { |conn| conn.zcard(name) }
|
2012-03-18 15:29:09 -04:00
|
|
|
end
|
|
|
|
|
2012-05-11 19:48:03 -04:00
|
|
|
def retries(count=50)
|
2012-05-26 21:38:55 -04:00
|
|
|
zcontents('retry', count)
|
2012-03-18 15:29:09 -04:00
|
|
|
end
|
|
|
|
|
2012-05-25 23:21:42 -04:00
|
|
|
def scheduled(count=50)
|
2012-05-26 21:38:55 -04:00
|
|
|
zcontents('schedule', count)
|
|
|
|
end
|
|
|
|
|
|
|
|
def zcontents(name, count)
|
2012-05-25 23:21:42 -04:00
|
|
|
Sidekiq.redis do |conn|
|
2012-05-26 21:38:55 -04:00
|
|
|
results = conn.zrange(name, 0, count, :withscores => true)
|
|
|
|
results.map { |msg, score| [Sidekiq.load_json(msg), score] }
|
2012-05-25 23:21:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-05 23:53:14 -05:00
|
|
|
def queues
|
2012-05-11 19:48:03 -04:00
|
|
|
@queues ||= Sidekiq.redis do |conn|
|
2012-03-07 23:22:21 -05:00
|
|
|
conn.smembers('queues').map do |q|
|
|
|
|
[q, conn.llen("queue:#{q}") || 0]
|
|
|
|
end.sort { |x,y| x[1] <=> y[1] }
|
|
|
|
end
|
2012-03-05 23:53:14 -05:00
|
|
|
end
|
2012-03-06 23:17:42 -05:00
|
|
|
|
2012-05-22 10:16:08 -04:00
|
|
|
def backlog
|
|
|
|
queues.map {|name, size| size }.inject(0) {|memo, val| memo + val }
|
|
|
|
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
|
|
|
|
2012-03-05 23:53:14 -05:00
|
|
|
def root_path
|
|
|
|
"#{env['SCRIPT_NAME']}/"
|
|
|
|
end
|
2012-03-06 23:17:42 -05:00
|
|
|
|
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
|
|
|
|
|
|
|
def display_args(args, count=100)
|
|
|
|
args.map { |arg| a = arg.inspect; a.size > count ? "#{a[0..count]}..." : a }.join(", ")
|
|
|
|
end
|
2012-03-05 23:53:14 -05:00
|
|
|
end
|
|
|
|
|
2012-03-04 15:58:16 -05:00
|
|
|
get "/" do
|
|
|
|
slim :index
|
|
|
|
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-05-06 23:15:34 -04:00
|
|
|
count = (params[:count] || 10).to_i
|
2012-03-05 23:53:14 -05:00
|
|
|
@name = params[:name]
|
2012-05-06 23:15:34 -04:00
|
|
|
@messages = Sidekiq.redis {|conn| conn.lrange("queue:#{@name}", 0, count) }.map { |str| Sidekiq.load_json(str) }
|
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
|
|
|
|
Sidekiq.redis do |conn|
|
|
|
|
conn.del("queue:#{params[:name]}")
|
|
|
|
conn.srem("queues", params[:name])
|
|
|
|
end
|
|
|
|
redirect root_path
|
|
|
|
end
|
|
|
|
|
2012-03-29 13:48:06 -04:00
|
|
|
get "/retries/:score" do
|
|
|
|
halt 404 unless params[:score]
|
2012-03-29 23:55:16 -04:00
|
|
|
@score = params[:score].to_f
|
2012-05-05 00:00:48 -04:00
|
|
|
@retries = retries_with_score(@score)
|
2012-05-11 19:48:03 -04:00
|
|
|
redirect "#{root_path}retries" if @retries.empty?
|
2012-03-29 13:48:06 -04:00
|
|
|
slim :retry
|
|
|
|
end
|
2012-03-29 23:55:16 -04:00
|
|
|
|
2012-05-11 19:48:03 -04:00
|
|
|
get '/retries' do
|
|
|
|
@retries = retries
|
|
|
|
slim :retries
|
|
|
|
end
|
|
|
|
|
2012-05-25 23:21:42 -04:00
|
|
|
get '/scheduled' do
|
|
|
|
@scheduled = scheduled
|
|
|
|
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
|
|
|
|
end
|
|
|
|
|
2012-05-11 19:48:03 -04:00
|
|
|
post '/retries' do
|
|
|
|
halt 404 unless params[:score]
|
|
|
|
params[:score].each do |score|
|
|
|
|
s = score.to_f
|
|
|
|
if params['retry']
|
2012-05-25 23:21:42 -04:00
|
|
|
process_score('retry', s, :retry)
|
2012-05-11 19:48:03 -04:00
|
|
|
elsif params['delete']
|
2012-05-25 23:21:42 -04:00
|
|
|
process_score('retry', s, :delete)
|
2012-05-11 19:48:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
redirect root_path
|
|
|
|
end
|
|
|
|
|
2012-03-29 23:55:16 -04:00
|
|
|
post "/retries/:score" do
|
|
|
|
halt 404 unless params[:score]
|
|
|
|
score = params[:score].to_f
|
|
|
|
if params['retry']
|
2012-05-11 19:48:03 -04:00
|
|
|
process_score(score, :retry)
|
|
|
|
elsif params['delete']
|
|
|
|
process_score(score, :delete)
|
|
|
|
end
|
|
|
|
redirect root_path
|
|
|
|
end
|
|
|
|
|
2012-05-25 23:21:42 -04:00
|
|
|
def process_score(set, score, operation)
|
2012-05-11 19:48:03 -04:00
|
|
|
case operation
|
|
|
|
when :retry
|
2012-03-29 23:55:16 -04:00
|
|
|
Sidekiq.redis do |conn|
|
2012-05-25 23:21:42 -04:00
|
|
|
results = conn.zrangebyscore(set, score, score)
|
|
|
|
conn.zremrangebyscore(set, score, score)
|
2012-03-29 23:55:16 -04:00
|
|
|
results.map do |message|
|
2012-04-22 17:02:35 -04:00
|
|
|
msg = Sidekiq.load_json(message)
|
2012-05-25 18:30:15 -04:00
|
|
|
msg['retry_count'] = msg['retry_count'] - 1
|
|
|
|
conn.rpush("queue:#{msg['queue']}", Sidekiq.dump_json(msg))
|
2012-03-29 23:55:16 -04:00
|
|
|
end
|
|
|
|
end
|
2012-05-11 19:48:03 -04:00
|
|
|
when :delete
|
2012-03-29 23:55:16 -04:00
|
|
|
Sidekiq.redis do |conn|
|
2012-05-25 23:21:42 -04:00
|
|
|
conn.zremrangebyscore(set, score, score)
|
2012-03-29 23:55:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-05-11 19:48:03 -04:00
|
|
|
|
2012-03-04 15:58:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|