From be730c08d9c78436f395d9f63c3d22d212c818c2 Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Thu, 7 Sep 2017 18:07:48 +0200 Subject: [PATCH] Moved listing of failed jobs to own method and added tests. --- app/controllers/monitoring_controller.rb | 4 ++-- app/models/scheduler.rb | 14 ++++++++++++-- spec/models/scheduler_spec.rb | 12 +++++++++++- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/app/controllers/monitoring_controller.rb b/app/controllers/monitoring_controller.rb index aab7f5ccb..248e7b1e8 100644 --- a/app/controllers/monitoring_controller.rb +++ b/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 diff --git a/app/models/scheduler.rb b/app/models/scheduler.rb index a94a777a2..22581ba9c 100644 --- a/app/models/scheduler.rb +++ b/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 diff --git a/spec/models/scheduler_spec.rb b/spec/models/scheduler_spec.rb index 087501aca..67412c29d 100644 --- a/spec/models/scheduler_spec.rb +++ b/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