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

This commit is contained in:
Thorsten Eckel 2017-09-07 18:07:48 +02:00
parent 70ce1be8c9
commit be730c08d9
3 changed files with 25 additions and 5 deletions

View file

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

View file

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

View file

@ -26,6 +26,17 @@ RSpec.describe Scheduler do
SpecSpace.send(:remove_const, :DelayedJobBackend) SpecSpace.send(:remove_const, :DelayedJobBackend)
end 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 describe '.restart_failed_jobs' do
it 'does restart failed jobs' do it 'does restart failed jobs' do
@ -34,7 +45,6 @@ RSpec.describe Scheduler do
job.reload job.reload
expect(job.active).to be true expect(job.active).to be true
end end
end end
describe '._start_job' do describe '._start_job' do