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 Request] Ability to cancel workflow execution #16320

Merged
merged 4 commits into from
Oct 1, 2024

Conversation

nerdai
Copy link
Contributor

@nerdai nerdai commented Oct 1, 2024

Description

This PR provides the user the ability to cancel a workflow execution.

workflow = ...

# run
handler = workflow.run()

# cancel
await handler.cancel_run()

Under the hood, cancel_run() sets the Context._cancel_flag and there is a dedicated cancel_workflow_task that waits for this and raises the newly defined WorkflowCancelledByUser exception. Following that the rest of the workflow mechanics take care of the exception being raised and setting the exception on the handler.

Type of Change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Added new unit/integration tests
  • I stared at the code and made sure it makes sense

@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Oct 1, 2024

ctx._tasks.add(
asyncio.create_task(
_cancel_workflow_task(), name="cancel_workflow_task"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Interesting method! Basically, this allows us to reuse all our existing logic for canceling tasks 💪🏻

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah! And it also I think matches the current working relationship between Worfklow and WorkflowHandler.

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Oct 1, 2024
@nerdai nerdai merged commit 254c683 into main Oct 1, 2024
10 checks passed
@nerdai nerdai deleted the nerdai/ability-to-cancel-workflow-execution branch October 1, 2024 19:14
raspawar pushed a commit to raspawar/llama_index that referenced this pull request Oct 7, 2024
* cancel

* test run step raises error

* WorkflowCancelledByUser

* mypy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm This PR has been approved by a maintainer size:M This PR changes 30-99 lines, ignoring generated files.
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

As a user I want to be able to cancel the execution of the entire Workflow at any point in time.
2 participants