فهرست منبع

Merge pull request #1423 from Jngai/queuedjobpatch

retry queued job patch
Andrew Cantino 9 سال پیش
والد
کامیت
35bbd2a492

+ 9 - 0
app/controllers/jobs_controller.rb

@@ -39,6 +39,15 @@ class JobsController < ApplicationController
     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
 

+ 4 - 0
app/views/jobs/index.html.erb

@@ -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 %>

+ 1 - 1
config/initializers/delayed_job.rb

@@ -12,7 +12,7 @@ Delayed::Worker.logger = Rails.logger
 
 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
 

+ 1 - 0
config/routes.rb

@@ -63,6 +63,7 @@ Huginn::Application.routes.draw do
     collection do
       delete :destroy_failed
       delete :destroy_all
+      post :retry_queued
     end
   end
 

+ 14 - 0
spec/controllers/jobs_controller_spec.rb

@@ -92,4 +92,18 @@ describe JobsController do
       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