Skip to content

Commit

Permalink
Fix order of things when finishing tool
Browse files Browse the repository at this point in the history
  • Loading branch information
maxnoe committed Nov 28, 2024
1 parent 055b62b commit cecb79d
Showing 1 changed file with 13 additions and 23 deletions.
36 changes: 13 additions & 23 deletions src/ctapipe/core/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ def run(self, argv=None, raises=False):
# return codes are taken from:
# https://tldp.org/LDP/abs/html/exitcodes.html

status = "success"
exit_status = 0
current_exception = None

Expand All @@ -430,51 +431,40 @@ def run(self, argv=None, raises=False):

self.start()
self.finish()
self.log.info("Finished: %s", self.name)
Provenance().finish_activity(activity_name=self.name)
except (ToolConfigurationError, TraitError) as err:
current_exception = err
self.log.error("%s", err)
self.log.error("Use --help for more info")
exit_status = 2 # wrong cmd line parameter
Provenance().finish_activity(
activity_name=self.name, status="error", exit_code=exit_status
)
status = "error"
except KeyboardInterrupt:
self.log.warning("WAS INTERRUPTED BY CTRL-C")
exit_status = 130 # Script terminated by Control-C
Provenance().finish_activity(
activity_name=self.name, status="interrupted", exit_code=exit_status
)
status = "interrupted"
except Exception as err:
current_exception = err
exit_status = getattr(err, "exit_code", 1)
status = "error"
self.log.exception("Caught unexpected exception: %s", err)
Provenance().finish_activity(
activity_name=self.name, status="error", exit_code=exit_status
)
except SystemExit as err:
exit_status = err.code
if exit_status == 0:
# Finish normally
Provenance().finish_activity(activity_name=self.name)
else:
# Finish with error
status = "error"
if exit_status != 0:
current_exception = err
self.log.critical(
"Caught SystemExit with exit code %s", exit_status
)
Provenance().finish_activity(
activity_name=self.name,
status="error",
exit_code=exit_status,
)
finally:
if not {"-h", "--help", "--help-all"}.intersection(self.argv):
self.write_provenance()
if raises and current_exception:
self.write_provenance()
raise current_exception

Provenance().finish_activity(activity_name=self.name, status=status, exit_code=exit_status)

if not {"-h", "--help", "--help-all"}.intersection(self.argv):
self.write_provenance()

self.log.info("Finished %s", self.name)
self.exit(exit_status)

def write_provenance(self):
Expand Down

0 comments on commit cecb79d

Please sign in to comment.