Sha256: deb34526fd4bc66a3a60d4212212060e2ab8b9e00ca4e8149f1a27b613825b7b

Contents?: true

Size: 1.87 KB

Versions: 3

Compression:

Stored size: 1.87 KB

Contents

require 'resque/status'

module Resque
  module StatusServer
    
    VIEW_PATH = File.join(File.dirname(__FILE__), 'server', 'views')
    
    def self.registered(app)
      
      app.get '/statuses' do
        @start = params[:start].to_i
        @end = @start + (params[:per_page] || 50)
        @statuses = Resque::Status.statuses(@start, @end)
        @size = @statuses.size
        status_view(:statuses)
      end
      
      app.get '/statuses/:id.js' do
        @status = Resque::Status.get(params[:id])
        content_type :js
        @status.json
      end
      
      app.get '/statuses/:id' do
        @status = Resque::Status.get(params[:id])
        status_view(:status)
      end
      
      app.post '/statuses/:id/kill' do
        Resque::Status.kill(params[:id])
        redirect url(:statuses)
      end
      
      app.post '/statuses/clear' do
        Resque::Status.clear
        redirect url(:statuses)
      end
      
      app.get "/statuses.poll" do
        content_type "text/plain"
        @polling = true

        @start = params[:start].to_i
        @end = @start + (params[:per_page] || 50)
        @statuses = Resque::Status.statuses(@start, @end)
        @size = @statuses.size

        status_view(:statuses, {:layout => false})
      end
      
      app.helpers do
        def status_view(filename, options = {}, locals = {})
          erb(File.read(File.join(::Resque::StatusServer::VIEW_PATH, "#{filename}.erb")), options, locals)
        end

        def status_poll(start)
          if @polling
            text = "Last Updated: #{Time.now.strftime("%H:%M:%S")}"
          else
            text = "<a href='#{url(request.path_info)}.poll?start=#{start}' rel='poll'>Live Poll</a>"
          end
          "<p class='poll'>#{text}</p>"
        end
      end
      
      app.tabs << "Statuses"
      
    end

  end
end

Resque::Server.register Resque::StatusServer

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
resque-status-0.2.3 lib/resque/status_server.rb
resque-status-0.2.2 lib/resque/status_server.rb
resque-status-0.2.1 lib/resque/status_server.rb