Browse Source

Moved listing of failed jobs to own method and added tests.

Thorsten Eckel 7 years ago
parent
commit
be730c08d9
3 changed files with 25 additions and 5 deletions
  1. 2 2
      app/controllers/monitoring_controller.rb
  2. 12 2
      app/models/scheduler.rb
  3. 11 1
      spec/models/scheduler_spec.rb

+ 2 - 2
app/controllers/monitoring_controller.rb

@@ -82,8 +82,8 @@ curl http://localhost/api/v1/monitoring/health_check?token=XXX
       issues.push 'scheduler not running'
     end
 
-    Scheduler.where(status: 'error', active: false).each do |scheduler|
-      issues.push "Failed to run scheduled job '#{scheduler.name}'. Cause: #{scheduler.error_message}"
+    Scheduler.failed_jobs.each do |job|
+      issues.push "Failed to run scheduled job '#{job.name}'. Cause: #{job.error_message}"
       actions.add(:restart_failed_jobs)
     end
 

+ 12 - 2
app/models/scheduler.rb

@@ -267,6 +267,16 @@ class Scheduler < ApplicationModel
 
   end
 
+  # This function returns a list of failed jobs
+  #
+  # @example
+  #   Scheduler.failed_jobs
+  #
+  # return [Array]
+  def self.failed_jobs
+    where(status: 'error', active: false)
+  end
+
   # This function restarts failed jobs to retry them
   #
   # @example
@@ -274,8 +284,8 @@ class Scheduler < ApplicationModel
   #
   # return [true]
   def self.restart_failed_jobs
-    Scheduler.where(status: 'error', active: false).each do |scheduler|
-      scheduler.update(active: true)
+    failed_jobs.each do |job|
+      job.update(active: true)
     end
 
     true

+ 11 - 1
spec/models/scheduler_spec.rb

@@ -26,6 +26,17 @@ RSpec.describe Scheduler do
     SpecSpace.send(:remove_const, :DelayedJobBackend)
   end
 
+  describe '.failed_jobs' do
+
+    it 'does list failed jobs' do
+      job = create(:scheduler, status: 'error', active: false)
+      failed_list = described_class.failed_jobs
+      expect(failed_list).to be_present
+      expect(failed_list).to include(job)
+    end
+
+  end
+
   describe '.restart_failed_jobs' do
 
     it 'does restart failed jobs' do
@@ -34,7 +45,6 @@ RSpec.describe Scheduler do
       job.reload
       expect(job.active).to be true
     end
-
   end
 
   describe '._start_job' do