require 'sinatra/base' require 'tilt/erb' require 'resque_admin' require 'resque_admin/version' require 'time' require 'yaml' if defined? Encoding Encoding.default_external = Encoding::UTF_8 end module ResqueAdmin class Server < Sinatra::Base require 'resque_admin/server/helpers' dir = File.dirname(File.expand_path(__FILE__)) set :views, "#{dir}/server/views" if respond_to? :public_folder set :public_folder, "#{dir}/server/public" else set :public, "#{dir}/server/public" end 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) [ url_prefix, path_prefix, path_parts ].join("/").squeeze('/') end alias_method :u, :url_path def redirect_url_path(*path_parts) [ path_prefix, path_parts ].join("/").squeeze('/') end 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) response["Cache-Control"] = "max-age=0, private, must-revalidate" begin erb page.to_sym, {:layout => layout}, :resque_admin => ResqueAdmin rescue Errno::ECONNREFUSED erb :error, {:layout => false}, :error => "Can't connect to Redis! (#{ResqueAdmin.redis_id})" end end def show_for_polling(page) content_type "text/html" @polling = true show(page.to_sym, false).gsub(/\s{1,}/, ' ') end # to make things easier on ourselves get "/?" do redirect redirect_url_path(:overview) end %w( overview workers ).each do |page| get "/#{page}.poll/?" do show_for_polling(page) end get "/#{page}/:id.poll/?" do show_for_polling(page) end 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 ResqueAdmin.remove_queue(params[:id]) redirect u('queues') end get "/failed/?" do if ResqueAdmin::Failure.url redirect ResqueAdmin::Failure.url else show :failed end end get "/failed/:queue" do if ResqueAdmin::Failure.url redirect ResqueAdmin::Failure.url else show :failed end end post "/failed/clear" do ResqueAdmin::Failure.clear redirect u('failed') end post "/failed/:queue/clear" do ResqueAdmin::Failure.clear params[:queue] redirect u('failed') end post "/failed/requeue/all" do ResqueAdmin::Failure.requeue_all redirect u('failed') end post "/failed/:queue/requeue/all" do ResqueAdmin::Failure.requeue_queue ResqueAdmin::Failure.job_queue_name(params[:queue]) redirect redirect_url_path("/failed/#{params[:queue]}") end get "/failed/requeue/:index/?" do ResqueAdmin::Failure.requeue(params[:index]) if request.xhr? return ResqueAdmin::Failure.all(params[:index])['retried_at'] else redirect u('failed') end end get "/failed/:queue/requeue/:index/?" do ResqueAdmin::Failure.requeue(params[:index], params[:queue]) if request.xhr? return ResqueAdmin::Failure.all(params[:index],1,params[:queue])['retried_at'] else redirect url_path("/failed/#{params[:queue]}") end end get "/failed/remove/:index/?" do ResqueAdmin::Failure.remove(params[:index]) redirect u('failed') end get "/failed/:queue/remove/:index/?" do ResqueAdmin::Failure.remove(params[:index], params[:queue]) redirect url_path("/failed/#{params[:queue]}") end get "/stats/?" do redirect redirect_url_path("/stats/resque_admin") end get "/stats/:id/?" do show :stats end get "/stats/keys/:key/?" do show :stats end get "/stats.txt/?" do info = ResqueAdmin.info stats = [] stats << "resque_admin.pending=#{info[:pending]}" stats << "resque_admin.processed+=#{info[:processed]}" stats << "resque_admin.failed+=#{info[:failed]}" stats << "resque_admin.workers=#{info[:workers]}" stats << "resque_admin.working=#{info[:working]}" ResqueAdmin.queues.each do |queue| stats << "queues.#{queue}=#{ResqueAdmin.size(queue)}" end content_type 'text/html' stats.join "\n" end def resque_admin ResqueAdmin end def self.tabs @tabs ||= ["Overview", "Working", "Failed", "Queues", "Workers", "Stats"] end def self.url_prefix=(url_prefix) @url_prefix = url_prefix end def self.url_prefix (@url_prefix.nil? || @url_prefix.empty?) ? '' : @url_prefix + '/' end end end