require 'sinatra/base' require 'erb' require 'resque' require 'resque/version' module Resque class Server < Sinatra::Base dir = File.dirname(File.expand_path(__FILE__)) set :views, "#{dir}/server/views" set :public, "#{dir}/server/public" set :static, true helpers do include Rack::Utils alias_method :h, :escape_html def current_section url_path request.path_info.sub('/','').split('/')[0].downcase end def current_page url_path request.path_info.sub('/','') end def url_path(*path_parts) [ path_prefix, path_parts ].join("/").squeeze('/') end alias_method :u, :url_path def path_prefix request.env['SCRIPT_NAME'] end def class_if_current(path = '') 'class="current"' if current_page[0, path.size] == path end def tab(name) dname = name.to_s.downcase path = url_path(dname) "
#{text}
" end end def show(page, layout = true) begin erb page.to_sym, {:layout => layout}, :resque => Resque rescue Errno::ECONNREFUSED erb :error, {:layout => false}, :error => "Can't connect to Redis! (#{Resque.redis_id})" end end # to make things easier on ourselves get "/?" do redirect url_path(:overview) end %w( overview queues working workers key ).each do |page| get "/#{page}" do show page end get "/#{page}/:id" do show page end end post "/queues/:id/remove" do Resque.remove_queue(params[:id]) redirect u('queues') end %w( overview workers ).each do |page| get "/#{page}.poll" do content_type "text/html" @polling = true show(page.to_sym, false).gsub(/\s{1,}/, ' ') end end get "/failed" do if Resque::Failure.url redirect Resque::Failure.url else show :failed end end post "/failed/clear" do Resque::Failure.clear redirect u('failed') end get "/failed/requeue/:index" do Resque::Failure.requeue(params[:index]) if request.xhr? return Resque::Failure.all(params[:index])['retried_at'] else redirect u('failed') end end get "/failed/remove/:index" do Resque::Failure.remove(params[:index]) redirect u('failed') end get "/stats" do redirect url_path("/stats/resque") end get "/stats/:id" do show :stats end get "/stats/keys/:key" do show :stats end get "/stats.txt" do info = Resque.info stats = [] stats << "resque.pending=#{info[:pending]}" stats << "resque.processed+=#{info[:processed]}" stats << "resque.failed+=#{info[:failed]}" stats << "resque.workers=#{info[:workers]}" stats << "resque.working=#{info[:working]}" Resque.queues.each do |queue| stats << "queues.#{queue}=#{Resque.size(queue)}" end content_type 'text/html' stats.join "\n" end def resque Resque end def self.tabs @tabs ||= ["Overview", "Working", "Failed", "Queues", "Workers", "Stats"] end end end