Module: Resque::StatusServer

Defined in:
lib/resque/status_server.rb

Constant Summary

VIEW_PATH =
File.join(File.dirname(__FILE__), 'server', 'views')

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/resque/status_server.rb', line 9

def self.registered(app)

  app.get '/statuses' do
    @start = params[:start].to_i
    @end = @start + (params[:per_page] || 50)
    @statuses = Resque::Plugins::Status::Hash.statuses(@start, @end)
    @size = @statuses.size
    status_view(:statuses)
  end

  app.get '/statuses/:id.js' do
    @status = Resque::Plugins::Status::Hash.get(params[:id])
    content_type :js
    @status.json
  end

  app.get '/statuses/:id' do
    @status = Resque::Plugins::Status::Hash.get(params[:id])
    status_view(:status)
  end

  app.post '/statuses/:id/kill' do
    Resque::Plugins::Status::Hash.kill(params[:id])
    redirect u(:statuses)
  end

  app.post '/statuses/clear' do
    Resque::Plugins::Status::Hash.clear
    redirect u(:statuses)
  end

  app.post '/statuses/clear/completed' do
    Resque::Plugins::Status::Hash.clear_completed
    redirect u(:statuses)
  end

  app.post '/statuses/clear/failed' do
    Resque::Plugins::Status::Hash.clear_failed
    redirect u(: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::Plugins::Status::Hash.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='#{u(request.path_info)}.poll?start=#{start}' rel='poll'>Live Poll</a>"
      end
      "<p class='poll'>#{text}</p>"
    end
  end

  app.tabs << "Statuses"

end