Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

retry queued job patch #1423

Merged
merged 1 commit into from
Apr 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/controllers/jobs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ def run
end
end

def retry_queued
@jobs = Delayed::Job.awaiting_retry.update_all(run_at: Time.zone.now)

respond_to do |format|
format.html { redirect_to jobs_path, notice: "Queued jobs getting retried." }
format.json { head :no_content }
end
end

def destroy_failed
Delayed::Job.where.not(failed_at: nil).delete_all

Expand Down
4 changes: 4 additions & 0 deletions app/views/jobs/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@
<span class="glyphicon glyphicon-trash"></span> Remove failed jobs
<% end %>

<%= link_to retry_queued_jobs_path, class: "btn btn-default", method: :post do %>
<span class="glyphicon glyphicon-refresh"></span> Retry queued jobs
<% end %>

<%= link_to destroy_all_jobs_path, class: "btn btn-default", method: :delete, data: { confirm: "Are you sure you want to delete ALL pending jobs for all Huginn users?" } do %>
<span class="glyphicon glyphicon-remove"></span> Remove all jobs
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/delayed_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

class Delayed::Job
scope :pending, ->{ where("locked_at IS NULL AND attempts = 0") }
scope :awaiting_retry, ->{ where("failed_at IS NULL AND attempts > 0") }
scope :awaiting_retry, ->{ where("failed_at IS NULL AND attempts > 0 AND locked_at IS NULL") }
scope :failed, -> { where("failed_at IS NOT NULL") }
end

Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
collection do
delete :destroy_failed
delete :destroy_all
post :retry_queued
end
end

Expand Down
14 changes: 14 additions & 0 deletions spec/controllers/jobs_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,18 @@
expect(Delayed::Job.find(@running.id)).to be
end
end

describe "POST retry_queued" do
before do
@not_running = Delayed::Job.create(run_at: Time.zone.now - 1.hour)
@not_running.update_attribute(:attempts, 1)
sign_in users(:jane)
end

it "run the queued job" do
expect(Delayed::Job.last.run_at.to_s).not_to eq(Time.zone.now.to_s)
post :retry_queued
expect(Delayed::Job.last.run_at.to_s).to eq(Time.zone.now.to_s)
end
end
end