Skip to content

Commit

Permalink
Ajaxify retry and remove links in Failure queue. Prevent expensive pa…
Browse files Browse the repository at this point in the history
…ge reloads when removing or retrying links.
  • Loading branch information
Tyler Knappe committed May 21, 2015
1 parent 10f2d2e commit 9b3acc7
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/controllers/resque_web/failures_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def index
# remove an individual job from the failure queue
def destroy
Resque::Failure.remove(params[:id])
redirect_to failures_path(redirect_params)
render json: {:success => true}, status: 200
end

# destroy all jobs from the failure queue
Expand Down
6 changes: 3 additions & 3 deletions app/views/resque_web/failures/_failed_job.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<li>
<li class="failed-<%= id %>">
<dl>
<% if job.nil? %>
<dt>Error</dt>
Expand All @@ -17,9 +17,9 @@
</div>
<% else %>
<div class="controls">
<%= link_to "Retry", retry_failure_path(:queue=>failure_queue,:id=>id), :method => :put %>
<%= link_to "Retry", retry_failure_path(:queue=>failure_queue,:id=>id), :data => {'job-id' => id }, :method => :put, :class => 'retry' %>
or
<%= link_to "Remove", failure_path(:queue=>failure_queue,:id=>id), :method => :delete %>
<%= link_to "Remove", failure_path(:queue=>failure_queue,:id=>id), :data => {'job-id' => id }, :method => :delete, :class => 'remove' %>
</div>
<% end %>
</dd>
Expand Down
44 changes: 42 additions & 2 deletions app/views/resque_web/failures/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<% else %>
<h1>Failed Jobs <%= "on '#{params[:queue]}'" if params[:queue] %> <%= "with class '#{params[:class]}'" if params[:class] %></h1>
<% end %>
<% unless failure_size.zero? %>
<%= form_tag(destroy_all_failures_path(queue: params[:queue]), method: :delete) do %>
<%= submit_tag "Clear #{failure_queue_name} Jobs", class: 'btn btn-danger', data: { confirm: "Are you sure you want to clear ALL #{failure_queue_name.downcase} jobs?" } %>
Expand All @@ -21,7 +20,6 @@
<%= render partial: 'overview' %>
<% else %>
<p class="sub">Showing <%= failure_start_at %> to <%= failure_end_at %> of <b><%= failure_size %></b> jobs</p>

<ul class="failed">
<% each_failure do |id, job| %>
<%= render partial: 'failed_job', locals: { id: id, job: job } %>
Expand All @@ -30,3 +28,45 @@

<%= pagination(start: failure_start_at, total: failure_size) unless params[:class] %>
<% end %>

<script type="text/javascript">
$('.controls a').on('click', function(e) {
e.preventDefault();
e.stopImmediatePropagation();

var eventType = $(this).attr('class');
var method = '';
var queue = '<%= params[:queue] %>';

if (queue === '') {
queue = 'failure';
}

if (eventType) {
if (eventType == 'remove') {
method = 'DELETE';
} else {
method = 'PUT';
}

var url = $(this).attr('href');
var jobId = $(this).attr('data-job-id');

var container = $('.failed-' + jobId);
if (url) {
$.ajax({
url: url,
method: method,
data: {
id: jobId,
queue: queue
}
}).done(function(data) {
$(container).hide();
}).fail(function() {
console.log('failed to delete job: ' + jobId);
});
}
}
});
</script>
2 changes: 1 addition & 1 deletion test/functional/failures_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class FailuresControllerTest < ActionController::TestCase
it "deletes the failure" do
Resque::Failure.expects(:remove).with('123')
visit(:destroy, {:id => 123}, :method => :delete)
assert_redirected_to failures_path
assert_response(:success, '{success: true}')
end
end

Expand Down

0 comments on commit 9b3acc7

Please sign in to comment.