Commit 3720f9af by John Doe

added service_status module

parent 0148696b
...@@ -2,12 +2,11 @@ ...@@ -2,12 +2,11 @@
# Handles construction of database objects # Handles construction of database objects
class UpdateDb class UpdateDb
attr_accessor :warnings, :errors include ServiceStatus
def initialize(db:) def initialize(db:)
@db = db @db = db
@warnings = [] super()
@errors = []
end end
def run(db_adapter:) def run(db_adapter:)
...@@ -19,16 +18,10 @@ class UpdateDb ...@@ -19,16 +18,10 @@ class UpdateDb
entries = __create_entries(db_adapter.schema) entries = __create_entries(db_adapter.schema)
updater = UpdateFolder.new(@root_folder, entries) updater = UpdateFolder.new(@root_folder, entries)
updater.run absorb_status(updater.run)
@errors << updater.errors
@warnings << updater.warnings
# parse the entries array
# Note: @root_folder gets linked in on
# the first call to __build_folder
# Don't save the result if there were errors
return false unless @errors.empty?
@db.save @db.save
self
end end
protected protected
......
...@@ -2,18 +2,18 @@ ...@@ -2,18 +2,18 @@
# Handles construction of DbFolder objects # Handles construction of DbFolder objects
class UpdateFile class UpdateFile
attr_accessor :warnings, :errors include ServiceStatus
def initialize(file, base_entry, decimation_entries) def initialize(file, base_entry, decimation_entries)
@file = file @file = file
@base_entry = base_entry @base_entry = base_entry
@decimation_entries = decimation_entries @decimation_entries = decimation_entries
@warnings = [] super()
@errors = []
end end
def run def run
__update_file(@file, @base_entry, @decimation_entries) __update_file(@file, @base_entry, @decimation_entries)
self
end end
# regex matching the ~decimXX ending on a stream path # regex matching the ~decimXX ending on a stream path
......
...@@ -2,13 +2,12 @@ ...@@ -2,13 +2,12 @@
# Handles construction of DbFolder objects # Handles construction of DbFolder objects
class UpdateFolder class UpdateFolder
attr_accessor :warnings, :errors include ServiceStatus
def initialize(folder, entries) def initialize(folder, entries)
@folder = folder @folder = folder
@entries = entries @entries = entries
@warnings = [] super()
@errors = []
end end
# returns the updated DbFolder object # returns the updated DbFolder object
...@@ -21,6 +20,7 @@ class UpdateFolder ...@@ -21,6 +20,7 @@ class UpdateFolder
__parse_folder_entries(@folder, @entries) __parse_folder_entries(@folder, @entries)
# save the result # save the result
@folder.save! @folder.save!
self
end end
protected protected
...@@ -85,9 +85,7 @@ class UpdateFolder ...@@ -85,9 +85,7 @@ class UpdateFolder
else # its a folder else # its a folder
updater = __build_folder(folder, entry_group, name) updater = __build_folder(folder, entry_group, name)
end end
updater.run absorb_status(updater.run)
@warnings << updater.warnings
@errors << updater.errors
end end
end end
......
...@@ -9,7 +9,7 @@ class CreateNilm ...@@ -9,7 +9,7 @@ class CreateNilm
@warnings = [] @warnings = []
end end
def build(name:, url:, description: '') def run(name:, url:, description: '')
# create the NILM object # create the NILM object
@nilm = Nilm.new(name: name, url: url, @nilm = Nilm.new(name: name, url: url,
description: description) description: description)
......
# frozen_string_literal: true
# Handles service errors and warnings. Design pattern:
# All service action should occur in the run() function
# Within run, call add_error or add_warning with string
# messages. At the end of run() return the service object itself
# For nested services, call absorb_service(child.run()) to nest
# the errors and warnings of the child into the parent, set
# the action parameter to NEVER_FAIL, FAIL_ON_WARNING, or
# FAIL_ON_ERROR to determine when (if ever), absorb_status
# returns false
module ServiceStatus
attr_reader :errors, :warnings
FAIL_ON_ERROR = 0
FAIL_ON_WARNING = 1
NEVER_FAIL = 2
def initialize
@errors = []
@warnings = []
end
def add_error(message)
@errors << String(message)
end
def errors?
!@errors.empty?
end
def add_warning(message)
@warnings << String(message)
end
def warnings?
!@warnings.empty?
end
def run
raise 'Implement in client, return service object'
end
def absorb_status(service, action: FAIL_ON_ERROR)
@warnings += service.warnings
@errors += service.errors
case action
when FAIL_ON_WARNING
return false if warnings? || errors?
when FAIL_ON_ERROR
return false if errors?
end
true
end
end
...@@ -11,7 +11,7 @@ RSpec.describe 'CreateNilm' do ...@@ -11,7 +11,7 @@ RSpec.describe 'CreateNilm' do
allow(UpdateDb).to receive(:new).and_return(service) allow(UpdateDb).to receive(:new).and_return(service)
# run the NILM creation # run the NILM creation
nilm_creator = CreateNilm.new nilm_creator = CreateNilm.new
nilm_creator.build(name: 'test', url: test_nilm_url) nilm_creator.run(name: 'test', url: test_nilm_url)
# verify NILM components are present # verify NILM components are present
expect(nilm_creator.nilm).to be_present expect(nilm_creator.nilm).to be_present
expect(nilm_creator.nilm.db).to be_present expect(nilm_creator.nilm.db).to be_present
......
# frozen_string_literal: true
require 'rails_helper'
class ModuleTester
include ServiceStatus
def initialize
@x = 2
super
end
end
describe 'ServiceStatus' do
it 'tracks errors' do
x = ModuleTester.new
x.add_error('message')
expect(x.errors?).to be true
expect(x.errors.length).to eq(1)
end
it 'tracks warnings' do
x = ModuleTester.new
x.add_warning('message')
expect(x.warnings?).to be true
expect(x.warnings.length).to eq(1)
end
it 'raises error if *run* is not implemented' do
x = ModuleTester.new
expect { x.run }.to raise_error(RuntimeError)
end
describe 'absorb_status' do
let(:parent) { ModuleTester.new }
let(:child) { ModuleTester.new }
it 'adds errors/warnings from service into itself' do
child.add_error('test')
child.add_warning('test')
parent.absorb_status(child)
expect(parent.errors?).to be true
expect(parent.warnings?).to be true
end
it 'it always returns true if action==NEVER_FAIL' do
child.add_error('test')
child.add_warning('test')
expect(
parent.absorb_status(child,
action: ServiceStatus::NEVER_FAIL))
.to be true
end
it 'fails on errors and warnings with FAIL_ON_WARNING' do
child.add_warning('test')
expect(
parent.absorb_status(child,
action: ServiceStatus::FAIL_ON_WARNING))
.to be false
end
it 'fails on errors with FAIL_ON_ERROR' do
child.add_error('test')
# default action
expect(parent.absorb_status(child)).to be false
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment