aiidateam / aiida-workgraph

Efficiently design and manage flexible workflows with AiiDA, featuring an interactive GUI, checkpoints, provenance tracking, and remote execution capabilities.
https://aiida-workgraph.readthedocs.io/en/latest/
MIT License
10 stars 5 forks source link

Refactor the error handler #302

Closed superstar54 closed 2 months ago

superstar54 commented 2 months ago

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)
codecov-commenter commented 2 months ago

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 :warning:
aiida_workgraph/engine/workgraph.py 89.28% 3 Missing :warning:
aiida_workgraph/calculations/python.py 0.00% 2 Missing :warning:
aiida_workgraph/task.py 94.73% 1 Missing :warning:
aiida_workgraph/utils/__init__.py 92.30% 1 Missing :warning:
aiida_workgraph/workgraph.py 93.33% 1 Missing :warning:
Additional details and impacted files ```diff @@ 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](https://app.codecov.io/gh/aiidateam/aiida-workgraph/pull/302/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aiidateam) | Coverage Δ | | |---|---|---| | [python-3.11](https://app.codecov.io/gh/aiidateam/aiida-workgraph/pull/302/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aiidateam) | `80.39% <89.74%> (+4.73%)` | :arrow_up: | | [python-3.12](https://app.codecov.io/gh/aiidateam/aiida-workgraph/pull/302/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aiidateam) | `80.39% <89.74%> (?)` | | | [python-3.9](https://app.codecov.io/gh/aiidateam/aiida-workgraph/pull/302/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aiidateam) | `80.43% <89.74%> (+4.69%)` | :arrow_up: | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aiidateam#carryforward-flags-in-the-pull-request-comment) to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.