Skip to content

Commit 00a6238

Browse files
Merge pull request #2 from MorphoSource/ms_updates2
Query jobs by job class or exception, retry jobs by job class
2 parents 1f44b2a + dc2de9f commit 00a6238

File tree

5 files changed

+98
-10
lines changed

5 files changed

+98
-10
lines changed

app/controllers/resque_web/failures_controller.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,24 @@ def retry_all_for_exception
5656
redirect_to failures_path(redirect_params)
5757
end
5858

59+
def retry_all_for_job_class
60+
if params[:job_class].present?
61+
job_class = params[:job_class]
62+
job_ids = []
63+
64+
Resque::Failure.each(0, Resque::Failure.count, params[:queue] || 'failed') do |id, item|
65+
if item['payload'] && item['payload']['args'] && item['payload']['args']&.first['job_class'] == job_class
66+
job_ids << id
67+
end
68+
end
69+
70+
job_ids.sort.reverse.map { |id| reque_single_job(id) }
71+
else
72+
flash[:error] = "No job class specified for retrying failures."
73+
end
74+
redirect_to failures_path(redirect_params)
75+
end
76+
5977
private
6078

6179
#API agnostic for Resque 2 with duck typing on requeue_and_remove

app/helpers/resque_web/failures_helper.rb

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,46 @@
11
module ResqueWeb
22
module FailuresHelper
3+
# If job_class or exception params present, need to set up data differently
4+
def initialize_failure_data
5+
if params[:job_class].present? || params[:exception].present?
6+
query_matching_failures
7+
@failure_size = @failures.count
8+
end
9+
end
10+
11+
def query_matching_failures
12+
@failures = []
13+
Resque::Failure.each(0, Resque::Failure.count, params[:queue] || 'failed', params[:class]) do |id, item|
14+
matches_conditions = true
15+
16+
if params[:exception].present?
17+
if item['exception'] != params[:exception]
18+
matches_conditions = false
19+
end
20+
end
21+
22+
if params[:job_class].present?
23+
item_job_class = ( item.dig('payload', 'args')&.first || {} )['job_class']
24+
if item_job_class.present? && item_job_class != params[:job_class]
25+
matches_conditions = false
26+
end
27+
end
28+
29+
if matches_conditions == true
30+
@failures << [id, item]
31+
end
32+
end
33+
end
34+
335
def each_failure(&block)
4-
Resque::Failure.each(failure_start_at, failure_per_page, params[:queue], params[:class], 'asc', &block)
36+
if params[:job_class].present? || params[:exception].present?
37+
query_matching_failures if @failures.nil?
38+
failures_page = @failures.slice(failure_start_at, failure_per_page) || []
39+
failures_page.each &block
40+
else
41+
# default case, use standard Resque library
42+
Resque::Failure.each(failure_start_at, failure_per_page, params[:queue], params[:class], &block)
43+
end
544
end
645

746
def failure_date_format
@@ -29,14 +68,16 @@ def failure_per_page
2968
end
3069

3170
def failure_start_at
32-
params[:start].to_i
71+
@failure_start_at ||= params[:start].to_i
3372
end
3473

3574
def failure_end_at
36-
if failure_start_at + failure_per_page > failure_size
37-
failure_size
38-
else
39-
failure_start_at + failure_per_page
75+
@failure_end_at ||= begin
76+
if failure_start_at + failure_per_page > failure_size
77+
failure_size
78+
else
79+
failure_start_at + failure_per_page
80+
end
4081
end
4182
end
4283

app/views/resque_web/failures/_failed_job.html.erb

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,39 @@
2626
<dt>Class</dt>
2727
<dd>
2828
<% if job['payload'] && job['payload']['class'] %>
29-
<%= link_to failures_path(:class=>job['payload']['class'],:queue=>params[:queue]) do %>
29+
<%= link_to failures_path(:class=>job['payload']['class'], :queue=>params[:queue]) do %>
3030
<code><%= job['payload']['class'] %></code>
3131
<% end %>
3232
<% else %>
3333
<code>nil</code>
3434
<% end %>
3535
</dd>
36+
<dt>Job Class</dt>
37+
<dd>
38+
<% if job['payload'] && job['payload']['args'] && job['payload']['args']&.first['job_class'] %>
39+
<%= link_to failures_path(:class=>job['payload']['class'], :queue=>params[:queue], :job_class=>job['payload']['args']&.first['job_class']) do %>
40+
<code><%= job['payload']['args']&.first['job_class'] %></code>
41+
<% end %>
42+
<% else %>
43+
<code>nil</code>
44+
<% end %>
45+
<%= form_tag(retry_all_for_job_class_failures_path(job_class: job['payload']['args']&.first['job_class'].to_s), method: :put, style: 'margin-top: 0;') do %>
46+
<%= submit_tag "Retry All Jobs Of This Class", data: { confirm: "Are you sure you want to retry ALL #{failure_queue_name.downcase} jobs with this class?" } %>
47+
<% end %>
48+
</dd>
3649
<dt>Arguments</dt>
3750
<dd><pre><%= job_arguments(job) %></pre></dd>
3851
<dt>Exception</dt>
3952
<dd>
40-
<code><%= job['exception'] %></code>
53+
<% if job['exception'] %>
54+
<%= link_to failures_path(:class=>job['payload']['class'], :queue=>params[:queue], :exception=>job['exception']) do %>
55+
<code><%= job['exception'] %></code>
56+
<% end %>
57+
<% else %>
58+
<code>nil</code>
59+
<% end %>
4160
<%= form_tag(retry_all_for_exception_failures_path(exception: job['exception'].to_s), method: :put, style: 'margin-top: 0;') do %>
42-
<%= submit_tag "Retry All Jobs with Exception", data: { confirm: "Are you sure you want to retry ALL #{failure_queue_name.downcase} jobs with this exception?" } %>
61+
<%= submit_tag "Retry All Jobs With Exception", data: { confirm: "Are you sure you want to retry ALL #{failure_queue_name.downcase} jobs with this exception?" } %>
4362
<% end %>
4463
</dd>
4564
<dt>Error</dt>

app/views/resque_web/failures/index.html.erb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1+
<% initialize_failure_data %>
12
<% if multiple_failure_queues? && !params[:queue] %>
23
<h1>All Failure Queues</h1>
34
<% else %>
4-
<h1>Failed Jobs <%= "on '#{params[:queue]}'" if params[:queue] %> <%= "with class '#{params[:class]}'" if params[:class] %></h1>
5+
<% if params[:job_class] %>
6+
<h1>Failed Jobs <%= "on '#{params[:queue]}'" if params[:queue] %> <%= "with job class '#{params[:job_class]}'" %></h1>
7+
<% elsif params[:exception] %>
8+
<h1>Failed Jobs <%= "on '#{params[:queue]}'" if params[:queue] %> <%= "with exception '#{params[:exception]}'" %></h1>
9+
<% elsif params[:class] %>
10+
<h1>Failed Jobs <%= "on '#{params[:queue]}'" if params[:queue] %> <%= "with class '#{params[:class]}'" %></h1>
11+
<% else %>
12+
<h1>Failed Jobs <%= "on '#{params[:queue]}'" if params[:queue] %></h1>
13+
<% end %>
514
<% end %>
615

716
<% unless failure_size.zero? %>

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
collection do
2323
put 'retry_all'
2424
put 'retry_all_for_exception'
25+
put 'retry_all_for_job_class'
2526
delete 'destroy_all'
2627
end
2728
end

0 commit comments

Comments
 (0)