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.
|
|
|
|
return [301, { 'Location' => "#{env['SCRIPT_NAME']}/" }, []] if env['SCRIPT_NAME'] == env['REQUEST_PATH']
|
|
|
|
|
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
|
|
|
|
|
|
|
def reset_worker_list
|
|
|
|
Sidekiq.redis do |conn|
|
|
|
|
workers = conn.smembers('workers')
|
|
|
|
workers.each do |name|
|
|
|
|
conn.srem('workers', name) # if name =~ /:#{process_id}-/
|
|
|
|
end
|
2012-03-28 22:16:54 -04:00
|
|
|
end
|
2012-03-28 15:36:16 -04:00
|
|
|
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}")
|
|
|
|
msg = MultiJson.decode(msg) if msg
|
|
|
|
[w, msg]
|
|
|
|
end.sort { |x| x[1] ? -1 : 1 }
|
|
|
|
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-03-18 15:29:09 -04:00
|
|
|
def retry_count
|
|
|
|
Sidekiq.redis { |conn| conn.zcard('retry') }
|
|
|
|
end
|
|
|
|
|
|
|
|
def retries
|
|
|
|
Sidekiq.redis do |conn|
|
|
|
|
results = conn.zrange('retry', 0, 25, :withscores => true)
|
2012-03-18 17:14:43 -04:00
|
|
|
results.each_slice(2).map { |msg, score| [MultiJson.decode(msg), Float(score)] }
|
2012-03-18 15:29:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-05 23:53:14 -05:00
|
|
|
def queues
|
2012-03-17 12:41:24 -04:00
|
|
|
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-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-03-06 23:17:42 -05:00
|
|
|
return 'down' if workers.size == 0
|
|
|
|
return 'idle' if workers.size > 0 && workers.map { |x| x[1] }.compact.size == 0
|
|
|
|
return 'active'
|
|
|
|
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
|
|
|
|
2012-03-28 15:36:16 -04:00
|
|
|
post "/reset" do
|
2012-03-28 22:16:54 -04:00
|
|
|
reset_worker_list
|
|
|
|
redirect root_path
|
2012-03-28 15:36:16 -04:00
|
|
|
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-03-05 23:53:14 -05:00
|
|
|
@name = params[:name]
|
2012-03-17 12:41:24 -04:00
|
|
|
@messages = Sidekiq.redis {|conn| conn.lrange("queue:#{@name}", 0, 10) }.map { |str| MultiJson.decode(str) }
|
2012-03-05 23:53:14 -05:00
|
|
|
slim :queue
|
|
|
|
end
|
2012-03-04 15:58:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|