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
202
203
204
205
206
207
|
# File 'lib/resque_cleaner/server.rb', line 65
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|
klass = job["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
get /cleaner\/public\/([a-z]+\.[a-z]+)/ do
send_file ResqueCleaner::Server.public_path(params[:captures].first)
end
end
end
|