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

157 lines
4.2 KiB
Ruby
Raw Normal View History

require 'sinatra/base'
require 'slim'
require 'sprockets'
2012-03-31 15:45:24 -04:00
require 'multi_json'
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
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
def reset_worker_list
Sidekiq.redis do |conn|
workers = conn.smembers('workers')
workers.each do |name|
2012-03-30 01:24:22 -04:00
conn.srem('workers', name)
end
2012-03-28 22:16:54 -04:00
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-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)
results.each_slice(2).map { |msg, score| [Sidekiq.load_json(msg), Float(score)] }
2012-03-18 15:29:09 -04:00
end
end
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
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
end
get "/" do
slim :index
end
post "/reset" do
2012-03-28 22:16:54 -04:00
reset_worker_list
redirect root_path
end
get "/queues/:name" do
2012-03-17 12:41:24 -04:00
halt 404 unless params[:name]
@name = params[:name]
@messages = Sidekiq.redis {|conn| conn.lrange("queue:#{@name}", 0, 10) }.map { |str| Sidekiq.load_json(str) }
slim :queue
end
2012-03-29 13:48:06 -04:00
get "/retries/:score" do
halt 404 unless params[:score]
@score = params[:score].to_f
2012-03-29 13:48:06 -04:00
slim :retry
end
post "/retries/:score" do
halt 404 unless params[:score]
score = params[:score].to_f
if params['retry']
Sidekiq.redis do |conn|
results = conn.zrangebyscore('retry', score, score)
conn.zremrangebyscore('retry', score, score)
results.map do |message|
msg = Sidekiq.load_json(message)
conn.rpush("queue:#{msg['queue']}", message)
end
end
elsif params['delete']
Sidekiq.redis do |conn|
conn.zremrangebyscore('retry', score, score)
end
end
redirect root_path
end
end
end