Skip to content

Conversation

zanieb
Copy link
Contributor

@zanieb zanieb commented Nov 28, 2022

Example

Flow:

import prefect
import time

@prefect.flow
def foo():
    print("hi!")
    time.sleep(1000)

if __name__ == "__main__":
    from prefect.deployments import Deployment

    d = Deployment.build_from_flow(foo, name="test")
    d.apply()

Trigger:

❯ prefect deployment run foo/test
❯ prefect flow-run cancel 9344f984-f5b9-46c6-936c-2f6e44d2d291
16:18:49.350 | DEBUG   | prefect.client - Connecting to API at https://api.prefect.cloud/api/accounts/f6f508a4-f8a7-49a1-bb4c-4970a9d8662e/workspaces/897bd37e-a9aa-49ea-9f5e-75c6f6b2622d/
Flow run '9344f984-f5b9-46c6-936c-2f6e44d2d291' was succcessfully scheduled for cancellation.

Agent:

16:18:51.147 | DEBUG   | prefect.agent - Checking for cancelled flow runs...
16:18:51.330 | INFO    | prefect.agent - Found 1 flow runs awaiting cancellation.
16:18:51.597 | INFO    | prefect.agent - Killing process Michaels-MacBook-Pro.local:45276 for flow run '9344f984-f5b9-46c6-936c-2f6e44d2d291'...
16:18:53.086 | ERROR   | prefect.infrastructure.process - Process 'hospitable-coucal' exited with status code: -15; This indicates that the process exited due to a SIGTERM signal. Typically, this is caused by manual cancellation.

Checklist

  • This pull request references any related issue by including "closes <link to issue>"
    • If no issue exists and your change is not a small fix, please create an issue first.
  • This pull request includes tests or only affects documentation.
  • This pull request includes a label categorizing the change e.g. fix, feature, enhancement

@zanieb zanieb force-pushed the feature/flow-run-cancellation branch from e2bbe58 to 886f129 Compare November 28, 2022 22:08
@zanieb zanieb force-pushed the cancel/agent-loop branch 2 times, most recently from 85000e5 to b6728e9 Compare November 28, 2022 22:36
f"Killing {infrastructure.type} {flow_run.infrastructure_pid} for flow run '{flow_run.id}'..."
)
try:
await infrastructure.kill(flow_run.infrastructure_pid)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we handle the case that infrastructure_pid is null?

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 let's handle that and do a hasattr check for kill.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

On second thought, we should maybe handle this during the proposal of a cancelled state? We can add a kill-infrastructure capability to infrastructure blocks that support killing and block cancellation when it is not supported?

Copy link
Contributor

Choose a reason for hiding this comment

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

That works for me. We do still have the, very small, edge case that infrastructure_pid is null because the flow_run update failed. Checking that here probably still makes sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added these in 52f5565 and we mark the run as cancelled so we don't pick it up forever. Kind of an awkward situation.

@peytonrunyan peytonrunyan self-requested a review November 29, 2022 16:24
Copy link
Contributor

@peytonrunyan peytonrunyan left a comment

Choose a reason for hiding this comment

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

Left a couple small comments, otherwise lgtm!

@zanieb zanieb marked this pull request as ready for review November 30, 2022 18:39
@zanieb zanieb force-pushed the feature/flow-run-cancellation branch from 9daea9a to 11fe0a0 Compare November 30, 2022 19:33
@zanieb zanieb force-pushed the feature/flow-run-cancellation branch from 11fe0a0 to 39e2bc7 Compare November 30, 2022 19:54
@zanieb zanieb requested a review from pleek91 as a code owner November 30, 2022 19:54
@zanieb zanieb force-pushed the feature/flow-run-cancellation branch from 39e2bc7 to e3caa3f Compare November 30, 2022 19:58
@zanieb zanieb force-pushed the feature/flow-run-cancellation branch from e3caa3f to d5e6209 Compare November 30, 2022 19:59
@zanieb zanieb merged commit e6c4297 into feature/flow-run-cancellation Nov 30, 2022
@zanieb zanieb deleted the cancel/agent-loop branch November 30, 2022 22:21
zanieb added a commit that referenced this pull request Dec 1, 2022
zanieb added a commit that referenced this pull request Dec 1, 2022
github-actions bot pushed a commit that referenced this pull request Dec 1, 2022
github-actions bot pushed a commit to ddelange/prefect that referenced this pull request Dec 15, 2022
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.

4 participants