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



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

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

.included(base) ⇒ Object



62
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/resque_cleaner/server.rb', line 62

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

      def show_job_args(args)
        Array(args).map { |a| a.inspect }.join("\n")
      end

      def text_filter(id, name, value)
        html = "<input id=\"#{id}\"  type=\"text\" name=\"#{name}\" value=\"#{value}\">"
        html += "</input>"
      end
    end

    mime_type :json, 'application/json'

    get "/cleaner" do
      load_library
      load_cleaner_filter

      @jobs = cleaner.select
      @stats = { :klass => {}, :exception => {} }
      @total = Hash.new(0)
      @jobs.each do |job|
        payload = job["payload"] || {}
        klass = payload["class"] || 'UNKNOWN'
        exception = job["exception"] || 'UNKNOWN'
        failed_at = Time.parse job["failed_at"]
        @stats[:klass][klass] ||= Hash.new(0)
        @stats[:exception][exception] ||= Hash.new(0)

        [
          @stats[:klass][klass],
          @stats[:exception][exception],
          @total
        ].each do |stat|
          stat[:total] += 1
          stat[:h1] += 1 if failed_at >= hours_ago(1)
          stat[:h3] += 1 if failed_at >= hours_ago(3)
          stat[:d1] += 1 if failed_at >= hours_ago(24)
          stat[:d3] += 1 if failed_at >= hours_ago(24*3)
          stat[:d7] += 1 if failed_at >= hours_ago(24*7)
        end
      end

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

    get "/cleaner_list" do
      load_library
      load_cleaner_filter
      build_urls

      block = filter_block

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

      @paginate = Paginate.new(@failed, @list_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
      build_urls

      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

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

    get "/cleaner_dump" do
      load_library
      load_cleaner_filter

      block = filter_block

      content_type :json
      JSON.pretty_generate(cleaner.select(&block))
    end

    post "/cleaner_stale" do
      load_library
      cleaner.clear_stale
      redirect url_path(:cleaner)
    end
  end

end

Instance Method Details

#build_urlsObject



226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/resque_cleaner/server.rb', line 226

def build_urls
  params = {
    c: @klass,
    ex: @exception,
    f: @from,
    t: @to,
    regex: @regex
  }.map {|key,value| "#{key}=#{URI.encode(value.to_s)}"}.join("&")

  @list_url = "cleaner_list?#{params}"
  @dump_url = "cleaner_dump?#{params}"
end

#cleanerObject



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

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

#filter_blockObject



239
240
241
242
243
244
245
246
247
248
# File 'lib/resque_cleaner/server.rb', line 239

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)]) &&
    (!@regex || j.to_s =~ /#{@regex}/)
  }
end

#hours_ago(h) ⇒ Object



250
251
252
# File 'lib/resque_cleaner/server.rb', line 250

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

#load_cleaner_filterObject



218
219
220
221
222
223
224
# File 'lib/resque_cleaner/server.rb', line 218

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]
  @regex = params[:regex]=="" ? nil : params[:regex]
end

#load_libraryObject



209
210
211
212
213
214
215
216
# File 'lib/resque_cleaner/server.rb', line 209

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