Module: ResqueCleaner::Server

Defined in:
lib/resque_cleaner/server.rb

Defined Under Namespace

Classes: Paginate

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.erb_path(filename) ⇒ Object



6
7
8
# File 'lib/resque_cleaner/server.rb', line 6

def self.erb_path(filename)
  File.join(File.dirname(__FILE__), 'server', 'views', filename)
end

.included(base) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/resque_cleaner/server.rb', line 63

def self.included(base)

  base.class_eval do
    helpers do
      def time_filter(id, name, value)
        html = "<select id=\"#{id}\" name=\"#{name}\">"
        html += "<option value=\"\">-</option>"
        [1, 3, 6, 12, 24].each do |h|
          selected = h.to_s == value ? 'selected="selected"' : ''
          html += "<option #{selected} value=\"#{h}\">#{h} #{h==1 ? "hour" : "hours"} ago</option>"
        end
        [3, 7, 14, 28].each do |d|
          selected = (d*24).to_s == value ? 'selected="selected"' : ''
          html += "<option #{selected} value=\"#{d*24}\">#{d} days ago</option>"
        end
        html += "</select>"
      end

      def class_filter(id, name, klasses, value)
        html = "<select id=\"#{id}\" name=\"#{name}\">"
        html += "<option value=\"\">-</option>"
        klasses.each do |k|
          selected = k == value ? 'selected="selected"' : ''
          html += "<option #{selected} value=\"#{k}\">#{k}</option>"
        end
        html += "</select>"
      end

      def exception_filter(id, name, exceptions, value)
        html = "<select id=\"#{id}\" name=\"#{name}\">"
        html += "<option value=\"\">-</option>"
        exceptions.each do |ex|
          selected = ex == value ? 'selected="selected"' : ''
          html += "<option #{selected} value=\"#{ex}\">#{ex}</option>"
        end
        html += "</select>"
      end
    end

    get "/cleaner" do
      load_library
      load_cleaner_filter

      @jobs = cleaner.select
      @stats, @total = {}, {"total" => 0, "1h" => 0, "3h" => 0, "1d" => 0, "3d" => 0, "7d" => 0}
      @jobs.each do |job|
        klass = job["payload"]["class"]
        failed_at = Time.parse job["failed_at"]

        @stats[klass] ||= {"total" => 0, "1h" => 0, "3h" => 0, "1d" => 0, "3d" => 0, "7d" => 0}
        items = [@stats[klass],@total]

        items.each{|a| a["total"] += 1}
        items.each{|a| a["1h"] += 1} if failed_at >= hours_ago(1)
        items.each{|a| a["3h"] += 1} if failed_at >= hours_ago(3)
        items.each{|a| a["1d"] += 1} if failed_at >= hours_ago(24)
        items.each{|a| a["3d"] += 1} if failed_at >= hours_ago(24*3)
        items.each{|a| a["7d"] += 1} if failed_at >= hours_ago(24*7)
      end

      erb File.read(ResqueCleaner::Server.erb_path('cleaner.erb'))
    end

    get "/cleaner_list" do
      load_library
      load_cleaner_filter

      block = filter_block

      @failed = cleaner.select(&block).reverse

      url = "cleaner_list?c=#{@klass}&ex=#{@exception}f=#{@from}&t=#{@to}"
      @paginate = Paginate.new(@failed, url, params[:p].to_i)

      @klasses = cleaner.stats_by_class.keys
      @exceptions = cleaner.stats_by_exception.keys
      @count = cleaner.select(&block).size

      erb File.read(ResqueCleaner::Server.erb_path('cleaner_list.erb'))
    end

    post "/cleaner_exec" do
      load_library
      load_cleaner_filter

      if params[:select_all_pages]!="1"
        @sha1 = {}
        params[:sha1].split(",").each {|s| @sha1[s] = true }
      end

      block = filter_block

      @count =
        case params[:action]
        when "clear" then cleaner.clear(&block)
        when "retry_and_clear" then cleaner.requeue(true,&block)
        when "retry" then cleaner.requeue(false,{},&block)
        end

      @url = "cleaner_list?c=#{@klass}&ex=#{@exception}&f=#{@from}&t=#{@to}"
      erb File.read(ResqueCleaner::Server.erb_path('cleaner_exec.erb'))
    end

    post "/cleaner_stale" do
      load_library
      cleaner.clear_stale
      redirect "/cleaner"
    end

    get /cleaner\/public\/([a-z]+\.[a-z]+)/ do
      send_file ResqueCleaner::Server.public_path(params[:captures].first)
    end
  end

end

.public_path(filename) ⇒ Object



9
10
11
# File 'lib/resque_cleaner/server.rb', line 9

def self.public_path(filename)
  File.join(File.dirname(__FILE__), 'server', 'public', filename)
end

Instance Method Details

#cleanerObject



179
180
181
182
183
# File 'lib/resque_cleaner/server.rb', line 179

def cleaner
  @cleaner ||= Resque::Plugins::ResqueCleaner.new
  @cleaner.print_message = false
  @cleaner
end

#filter_blockObject



201
202
203
204
205
206
207
208
209
# File 'lib/resque_cleaner/server.rb', line 201

def filter_block
  block = lambda{|j|
    (!@from || j.after?(hours_ago(@from))) &&
    (!@to || j.before?(hours_ago(@to))) &&
    (!@klass || j.klass?(@klass)) &&
    (!@exception || j.exception?(@exception)) &&
    (!@sha1 || @sha1[Digest::SHA1.hexdigest(j.to_json)])
  }
end

#hours_ago(h) ⇒ Object



211
212
213
# File 'lib/resque_cleaner/server.rb', line 211

def hours_ago(h)
  Time.now - h.to_i*60*60
end

#load_cleaner_filterObject



194
195
196
197
198
199
# File 'lib/resque_cleaner/server.rb', line 194

def load_cleaner_filter
  @from = params[:f]=="" ? nil : params[:f]
  @to = params[:t]=="" ? nil : params[:t]
  @klass = params[:c]=="" ? nil : params[:c]
  @exception = params[:ex]=="" ? nil : params[:ex]
end

#load_libraryObject



185
186
187
188
189
190
191
192
# File 'lib/resque_cleaner/server.rb', line 185

def load_library
  require 'digest/sha1'
  begin
    require 'yajl/json_gem' unless [].respond_to?(:to_json)
  rescue Exception
    require 'json'
  end
end