Skip to content

Commit

Permalink
Fix NREL/OpenStudio#864 be allowing the reporting measure state to fa…
Browse files Browse the repository at this point in the history
…il and still run the rest
  • Loading branch information
jmarrec committed Nov 21, 2019
1 parent 0619aea commit 7848eca
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions lib/openstudio/workflow/run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def self.default_jobs
#
# @param [String] osw_path the path to the OSW file to run. It is highly recommended that this be an absolute
# path, however if not it will be made absolute relative to the current working directory
# @param [Hash] user_options ({}) A set of user-specified options that are used to override default behaviors.
# @param [Hash] user_options ({}) A set of user-specified options that are used to override default behaviors.
# @option user_options [Hash] :cleanup Remove unneccessary files during post processing, overrides OSW option if set, defaults to true
# @option user_options [Hash] :debug Print debugging messages, overrides OSW option if set, defaults to false
# @option user_options [Hash] :energyplus_path Specifies path to energyplus executable, defaults to empty
Expand All @@ -102,7 +102,7 @@ def initialize(osw_path, user_options = {})
@final_message = ''
@current_state = nil
@options = {}

# Registry is a large hash of objects that are populated during the run, the number of objects in the registry should be reduced over time, especially if the functionality can be added to the WorkflowJSON class
# - analysis - the current OSA parsed as a Ruby Hash
# - datapoint - the current OSD parsed as a Ruby Hash
Expand All @@ -122,7 +122,7 @@ def initialize(osw_path, user_options = {})
# - time_logger - logger for doing profiling - time to run each step will be captured in OSResult, deprecate
# - wf - the path to the current weather file as a string, updated after each step
# - workflow - the current OSW parsed as a Ruby Hash
# - workflow_json - the current WorkflowJSON object
# - workflow_json - the current WorkflowJSON object
@registry = Registry.new

openstudio_2 = false
Expand All @@ -136,19 +136,19 @@ def initialize(osw_path, user_options = {})

# get the input osw
@input_adapter = OpenStudio::Workflow::InputAdapter::Local.new(osw_path)

# DLM: need to check that we have correct permissions to all these paths
@registry.register(:osw_path) { Pathname.new(@input_adapter.osw_path).realpath }
@registry.register(:osw_dir) { Pathname.new(@input_adapter.osw_dir).realpath }
@registry.register(:run_dir) { Pathname.new(@input_adapter.run_dir).cleanpath } # run dir might not yet exist, calling realpath will throw

# get info to set up logging first in case of failures later
@options[:debug] = @input_adapter.debug(user_options, false)
@options[:preserve_run_dir] = @input_adapter.preserve_run_dir(user_options, false)
@options[:skip_expand_objects] = @input_adapter.skip_expand_objects(user_options, false)
@options[:skip_energyplus_preprocess] = @input_adapter.skip_energyplus_preprocess(user_options, false)
@options[:profile] = @input_adapter.profile(user_options, false)

# if running in osw dir, force preserve run dir
if @registry[:osw_dir] == @registry[:run_dir]
# force preserving the run directory
Expand All @@ -173,7 +173,7 @@ def initialize(osw_path, user_options = {})
@options[:targets] = user_options[:targets]
else
# don't create these files unless we want to use them
# DLM: TODO, make sure that run.log will be closed later
# DLM: TODO, make sure that run.log will be closed later
@options[:targets] = [STDOUT, File.open(File.join(@registry[:run_dir], 'run.log'), 'a')]
end

Expand All @@ -183,11 +183,11 @@ def initialize(osw_path, user_options = {})
# Initialize the MultiDelegator logger
logger_level = @options[:debug] ? ::Logger::DEBUG : ::Logger::WARN
@logger = ::Logger.new(MultiDelegator.delegate(:write, :close).to(*@options[:targets])) # * is the splat operator
@logger.level = logger_level
@logger.level = logger_level
@registry.register(:logger) { @logger }

@logger.info "openstudio_2 = #{@registry[:openstudio_2]}"

# get the output adapter
default_output_adapter = OpenStudio::Workflow::OutputAdapter::Local.new(output_directory: @input_adapter.run_dir)
@output_adapter = @input_adapter.output_adapter(user_options, default_output_adapter, @logger)
Expand All @@ -196,9 +196,9 @@ def initialize(osw_path, user_options = {})
default_jobs = OpenStudio::Workflow::Run.default_jobs
@jobs = @input_adapter.jobs(user_options, default_jobs, @logger)

# get other run options out of user_options and into permanent options
# get other run options out of user_options and into permanent options
@options[:cleanup] = @input_adapter.cleanup(user_options, true)
@options[:energyplus_path] = @input_adapter.energyplus_path(user_options, nil)
@options[:energyplus_path] = @input_adapter.energyplus_path(user_options, nil)
@options[:verify_osw] = @input_adapter.verify_osw(user_options, true)
@options[:weather_file] = @input_adapter.weather_file(user_options, nil)
@options[:fast] = @input_adapter.fast(user_options, false)
Expand Down Expand Up @@ -241,6 +241,12 @@ def run
ensure
@logger.info 'Workflow complete'

# If we let the :reporting_measures step fail to continue with
# postprocess, we still want to report and error
if @final_message != ''
@current_state = :errored
end

if @current_state == :errored
@registry[:workflow_json].setCompletedStatus('Fail') if @registry[:workflow_json]
else
Expand Down Expand Up @@ -298,11 +304,20 @@ def step
def step_error(*args)
# Make sure to set the instance variable @error to true in order to stop the :step
# event from being fired.
@final_message = "Found error in state '#{@current_state}' with message #{args}}"
@logger.error @final_message

# transition to an error state
@current_state = :errored
# Allow continuing anyways if it fails in reporting measures
if @current_state == :reporting_measures
@final_message = "Found error in state '#{@current_state}' with message #{args}}"
@logger.error @final_message

next_state
else
@final_message = "Found error in state '#{@current_state}' with message #{args}}"
@logger.error @final_message

# transition to an error state
@current_state = :errored
end
end

# Return the finished state and exit
Expand Down

0 comments on commit 7848eca

Please sign in to comment.