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

Fix cancellation bug #8315

Merged
merged 5 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/prefect/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@
FlowRunFilterWorkQueueName,
)
from prefect.settings import PREFECT_AGENT_PREFETCH_SECONDS
from prefect.states import Crashed, Pending, StateType, exception_to_failed_state
from prefect.states import (
Cancelled,
Crashed,
Pending,
StateType,
exception_to_failed_state,
)
Fixed Show fixed Hide fixed


class OrionAgent:
Expand Down Expand Up @@ -370,6 +376,7 @@ async def _mark_flow_run_as_cancelled(
) -> None:
state_updates = state_updates or {}
state_updates.setdefault("name", "Cancelled")
state_updates.setdefault("type", StateType.CANCELLED)
state = flow_run.state.copy(update=state_updates)

await self.client.set_flow_run_state(flow_run.id, state, force=True)
Expand Down
16 changes: 12 additions & 4 deletions tests/agent/test_agent_run_cancellation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@
from prefect.infrastructure.base import Infrastructure
from prefect.orion.database.orm_models import ORMDeployment
from prefect.orion.schemas.core import Deployment
from prefect.states import Cancelled, Cancelling, Completed, Pending, Running, Scheduled
from prefect.states import (
Cancelled,
Cancelling,
Completed,
Pending,
Running,
Scheduled,
StateType,
)
from prefect.testing.utilities import AsyncMock
from prefect.utilities.dispatch import get_registry_for_type

Expand Down Expand Up @@ -286,7 +294,7 @@ async def test_agent_cancel_run_with_missing_infrastructure_pid(
@pytest.mark.parametrize(
"cancelling_constructor", [legacy_named_cancelling_state, Cancelling]
)
async def test_agent_cancel_run_updates_state_name(
async def test_agent_cancel_run_updates_state_type(
orion_client: OrionClient,
deployment: ORMDeployment,
cancelling_constructor,
Expand All @@ -304,7 +312,7 @@ async def test_agent_cancel_run_updates_state_name(
await agent.check_for_cancelled_flow_runs()

post_flow_run = await orion_client.read_flow_run(flow_run.id)
assert post_flow_run.state.name == "Cancelled"
assert post_flow_run.state.type == StateType.CANCELLED


@pytest.mark.usefixtures("mock_infrastructure_kill")
Expand All @@ -316,7 +324,7 @@ async def test_agent_cancel_run_preserves_other_state_properties(
deployment: ORMDeployment,
cancelling_constructor,
):
expected_changed_fields = {"name", "timestamp", "id"}
expected_changed_fields = {"type", "name", "timestamp", "id"}

flow_run = await orion_client.create_flow_run_from_deployment(
deployment.id,
Expand Down