Skip to content

Commit

Permalink
Added run error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lamsoa729 committed Feb 5, 2024
1 parent 7c5ad60 commit 2bf70aa
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
21 changes: 11 additions & 10 deletions chi_pet/chi_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@ def run_args(workdir, state, args):
if workdir.exists():
os.chdir(workdir)
action_file.touch() # Keeps track of current pipeline state
status = run(args)
print("Running: ", args)
status = run(args) #TODO capture output when an error occurs
print("status: ", status)
if status.returncode:
print(f"Run failed: State {state} did not succeed.")
raise OSError(f"Run failed: OSError occured at state {state}.")
action_file.unlink() # Remove once completed successfully
return status.returncode
else:
print(
f"Run failed: could not find work directory {workdir} to do action {state}.")
return 1
# error for not finding directory
raise FileNotFoundError(
f"Run failed: Work directory {workdir} not found to do action {state} .")
except:
print(f"Run failed: State {state} did not succeed.")
return 1
(workdir / '.error').touch()
raise
finally:
os.chdir(current_dir)

Expand All @@ -55,9 +57,8 @@ def run(self):

# If sim action file exists then run the state
if run_state in self._states:
if run_args(self._opts.workdir, run_state, args):
(self._opts.workdir / '.error').touch()
elif sim_state.exists():
run_args(self._opts.workdir, run_state, args)
if sim_state.exists():
sim_state.unlink()

def get_run_states(self):
Expand Down
3 changes: 3 additions & 0 deletions tests/test_chi_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,6 @@ def test_chi_node_matched_creation_runtime_error(mock_param_yaml_dict, mock_crea
cnode.make_node_dir(root_path)
with pytest.raises(RuntimeError):
cnode.make_subnodes()


FileNotFoundError
18 changes: 17 additions & 1 deletion tests/test_chi_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,20 @@ def test_chi_run_only_one_touch_with_opts(mock_leaf_dir, mock_run_opts):
assert not (mock_leaf_path / 'mock_output2.txt').exists()


# TODO NEXT TEST test run args to make sure it always returns you to the right directory
def test_chi_run_error(mock_leaf_dir, mock_run_opts):
"""Test chi_run."""
home_dir = Path.cwd()
mock_leaf_path = mock_leaf_dir
# Create and modify run optoins
mock_run_opts.args_dict = {'touch1': ['not_a_command', 'mock_output1.txt']}
mock_run_opts.states = mock_run_opts.args_dict.keys()
mock_run_opts.workdir = mock_leaf_path

# Make a ChiRun object
crun = ChiRun(mock_run_opts)
# Run the pipeline with failed state
with pytest.raises(OSError):
crun.run()
assert (mock_leaf_path / '.error').exists()
assert Path.cwd() == home_dir

0 comments on commit 2bf70aa

Please sign in to comment.