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

Refactor the error handler #302

Merged
merged 10 commits into from
Sep 13, 2024
Merged

Refactor the error handler #302

merged 10 commits into from
Sep 13, 2024

Conversation

superstar54
Copy link
Member

@superstar54 superstar54 commented Sep 11, 2024

This PR refactors the error handlers in three ways:
Engine

  1. pass task as args, and engine as kwargs
  2. return msg to report
  3. update the task in the engine

PythonJob
One can attach error_handler to the PythonJob task directly.

Store error handler
Save the module and name if the error handler is globally defined, otherwise, use pickle.

Example

For example, in the decorator's argument:

from aiida_workgraph import WorkGraph, task

def handle_negative_sum(task) -> str:
    """Handle the failure code 410 of the `add`.
    Simply make the inputs positive by taking the absolute value.
    """
    # modify task inputs
    task.set({"x": abs(task.inputs["x"].value),
              "y": abs(task.inputs["y"].value)})
    
    msg = "Run error handler: handle_negative_sum."
    return msg


@task.pythonjob(outputs=[{"name": "sum"}],
                error_handlers=[{"handler": handle_negative_sum,
                                "exit_codes": [410],
                                "max_retries": 5}])
def add(x, y):
    sum = x + y
    if sum < 0:
        exit_code = {"status": 410, "message": "Sum is negative"}
        return {"sum": sum, "exit_code": exit_code}
    return {"sum": sum}

wg = WorkGraph()
wg.add_task(add, name="add1", x=1, y=-2)
wg.submit(wait=True)
print("Task finished OK? ", wg.tasks["add1"].process.is_finished_ok)
print("Exit code: ", wg.tasks["add1"].process.exit_code)
print("Exit Message: ", wg.tasks["add1"].process.exit_message)

@superstar54 superstar54 changed the title Change the error handler's signature Refactor the error handler Sep 11, 2024
1) pass task as args, and engine as kwargs
2) return msg to report
3) update the task in the engine
@codecov-commenter
Copy link

codecov-commenter commented Sep 11, 2024

Codecov Report

Attention: Patch coverage is 89.74359% with 12 lines in your changes missing coverage. Please review.

Project coverage is 80.47%. Comparing base (5937b88) to head (9135e05).
Report is 69 commits behind head on main.

Files with missing lines Patch % Lines
aiida_workgraph/widget/src/widget/__init__.py 0.00% 4 Missing ⚠️
aiida_workgraph/engine/workgraph.py 89.28% 3 Missing ⚠️
aiida_workgraph/calculations/python.py 0.00% 2 Missing ⚠️
aiida_workgraph/task.py 94.73% 1 Missing ⚠️
aiida_workgraph/utils/__init__.py 92.30% 1 Missing ⚠️
aiida_workgraph/workgraph.py 93.33% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #302      +/-   ##
==========================================
+ Coverage   75.75%   80.47%   +4.72%     
==========================================
  Files          70       66       -4     
  Lines        4615     5040     +425     
==========================================
+ Hits         3496     4056     +560     
+ Misses       1119      984     -135     
Flag Coverage Δ
python-3.11 80.39% <89.74%> (+4.73%) ⬆️
python-3.12 80.39% <89.74%> (?)
python-3.9 80.43% <89.74%> (+4.69%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

This will either serialize the callable using cloudpickle if it's a local or lambda function, or store its module and name if it's a globally defined callable (function or class).
@superstar54 superstar54 merged commit feb42a6 into main Sep 13, 2024
8 checks passed
@superstar54 superstar54 deleted the rename_error_handler branch September 13, 2024 09:11
GeigerJ2 pushed a commit to GeigerJ2/aiida-workgraph that referenced this pull request Sep 13, 2024
This PR refactors the error handlers in three ways:
**Engine**
1) pass task as args, and engine as kwargs
2) return msg to report
3) update the task in the engine

**PythonJob**
One can attach `error_handler` to the PythonJob task directly. 

**Store error handler**
Save the module and name if the error handler is globally defined, otherwise, use pickle. Do not pickle AiiDA component if it is globally defined.

**Other**
* bump node-graph to 0.0.20
GeigerJ2 added a commit to GeigerJ2/aiida-workgraph that referenced this pull request Sep 13, 2024
This PR refactors the error handlers in three ways:
**Engine**
1) pass task as args, and engine as kwargs
2) return msg to report
3) update the task in the engine

**PythonJob**
One can attach `error_handler` to the PythonJob task directly.

**Store error handler**
Save the module and name if the error handler is globally defined, otherwise, use pickle. Do not pickle AiiDA component if it is globally defined.

**Other**
* bump node-graph to 0.0.20
agoscinski pushed a commit that referenced this pull request Sep 19, 2024
This PR refactors the error handlers in three ways:
**Engine**
1) pass task as args, and engine as kwargs
2) return msg to report
3) update the task in the engine

**PythonJob**
One can attach `error_handler` to the PythonJob task directly. 

**Store error handler**
Save the module and name if the error handler is globally defined, otherwise, use pickle. Do not pickle AiiDA component if it is globally defined.

**Other**
* bump node-graph to 0.0.20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants