Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/custom exit codes in tools #2566

Merged
merged 9 commits into from
Jun 21, 2024
Merged

Conversation

mexanick
Copy link
Contributor

Following a need in custom exit codes in ctapipe.core.Tool, I've implemented an extra catch of SystemExit that can be generated during the tool's execution phase that also performs a clean up in the same fashion as in general Exception interception and propagates the exit code further down.

@mexanick mexanick requested review from maxnoe, kosack and Hckjs June 19, 2024 15:08
@mexanick mexanick self-assigned this Jun 19, 2024
exit_status = err.code
self.log.exception("Caught SystemExit with exit code %s", exit_status)
Provenance().finish_activity(
activity_name=self.name, status="SystemExit"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the original plan was to not allow any random exit code, only a pre-defined set. If we need to have arbitrary exit codes (I guess the use case here is for workflow handling?), at the very least the code that was used should be stored in the provenance. Just recording "SystemExit" loses the reason for the exit happening. So we should either add a field to the provenance, or include the code in the status. The latter is less ideal since it would require parsing, and means the sets of possible status values is not finite

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the use case here is workflow handling. And the exit codes should not be completely randomly, but rather controlled by the corresponding ICD in our case. However, given that ctapipe is a community product, I'm a bit hesitant to introduce such a dependency here. On the other hand, I fully agree with your point that the exit code should be stored in provenance. Here before making a move, I'd like to discuss options.

So far, the possible statuses are ["completed", "interrupted", "error"] plus "SystemExit" I've added. I'm not sure what the subsequent purpose of this field. Is it just a human-readable label? If so, I would add another field, exit_code or just code where an actual exit code will be stored and change the list of possible statuses to ["success", "interrupted", "error", "partial_success"] or similar. On the other hand, if the status field is supposed to be machine-readable, shall it just keep the exit code value?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So far, the possible statuses are ["completed", "interrupted", "error"] plus "SystemExit"

I think SystemExit is not needed. The only reason to have a non-zero exit code is by definition some kind of "error".

If so, I would add another field, exit_code

Yes, I think adding the exit_code field is the way to go.

The problem is a bit that in the current scheme, Acitivity is not mapped one-to-one to a command-line tool.

"partial_success"

I think that's a bit hard to define at the Tool logic-level, however, what we could do is to let the implementation set this status already

Provenance().current_acitvity.status = "partial_success" and in that case we don't override it in Tool.run

Copy link
Contributor Author

@mexanick mexanick Jun 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps, this should be default in the _ActivityProvenance helper class constructor:

def __init__(self, activity_name=sys.executable):
self._prov = {
"activity_name": activity_name,
"activity_uuid": str(uuid.uuid4()),
"start": {},
"stop": {},
"system": {},
"input": [],
"output": [],
}
self.name = activity_name

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sys.executable

That's always the python interpreter, right? It won't be the cli tool name even if used via an entry point installed script

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the default value in the current code. It is overridden in the Tool.run() function:

Provenance().start_activity(self.name)

- Add "exit_code" field to provenance dictionary
- Update the possible provenance status values to
  ["success", "interrupted", "error", "partial_success"]
- Making the "partial_success" status default
  - It is overridden in finish_activity with a default "success"

This comment has been minimized.

@mexanick mexanick requested a review from kosack June 20, 2024 12:05
src/ctapipe/core/tool.py Outdated Show resolved Hide resolved

This comment has been minimized.

This comment has been minimized.

1 similar comment
Copy link

Passed

Analysis Details

1 Issue

  • Bug 0 Bugs
  • Vulnerability 0 Vulnerabilities
  • Code Smell 1 Code Smell

Coverage and Duplications

  • Coverage 96.20% Coverage (93.60% Estimated after merge)
  • Duplications 0.00% Duplicated Code (0.60% Estimated after merge)

Project ID: cta-observatory_ctapipe_AY52EYhuvuGcMFidNyUs

View in SonarQube

@maxnoe maxnoe merged commit f6202a8 into main Jun 21, 2024
12 checks passed
@maxnoe maxnoe deleted the feature/custom-exit-codes-in-tools branch June 21, 2024 13:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants