Skip to content

Nexus cancellation types #981

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions temporalio/worker/_interceptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ class StartNexusOperationInput(Generic[InputT, OutputT]):
operation: Union[nexusrpc.Operation[InputT, OutputT], str, Callable[..., Any]]
input: InputT
schedule_to_close_timeout: Optional[timedelta]
cancellation_type: temporalio.workflow.NexusOperationCancellationType
headers: Optional[Mapping[str, str]]
output_type: Optional[type[OutputT]] = None

Expand Down
26 changes: 14 additions & 12 deletions temporalio/worker/_workflow_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@
import temporalio.bridge.proto.activity_result
import temporalio.bridge.proto.child_workflow
import temporalio.bridge.proto.common
import temporalio.bridge.proto.nexus
import temporalio.bridge.proto.workflow_activation
import temporalio.bridge.proto.workflow_commands
import temporalio.bridge.proto.workflow_completion
import temporalio.common
import temporalio.converter
import temporalio.exceptions
import temporalio.nexus
import temporalio.workflow
from temporalio.service import __version__

Expand Down Expand Up @@ -1502,9 +1502,10 @@ async def workflow_start_nexus_operation(
service: str,
operation: Union[nexusrpc.Operation[InputT, OutputT], str, Callable[..., Any]],
input: Any,
output_type: Optional[Type[OutputT]] = None,
schedule_to_close_timeout: Optional[timedelta] = None,
headers: Optional[Mapping[str, str]] = None,
output_type: Optional[Type[OutputT]],
schedule_to_close_timeout: Optional[timedelta],
cancellation_type: temporalio.workflow.NexusOperationCancellationType,
headers: Optional[Mapping[str, str]],
) -> temporalio.workflow.NexusOperationHandle[OutputT]:
# start_nexus_operation
return await self._outbound.start_nexus_operation(
Expand All @@ -1515,6 +1516,7 @@ async def workflow_start_nexus_operation(
input=input,
output_type=output_type,
schedule_to_close_timeout=schedule_to_close_timeout,
cancellation_type=cancellation_type,
headers=headers,
)
)
Expand Down Expand Up @@ -2757,7 +2759,7 @@ def _apply_schedule_command(
if self._input.retry_policy:
self._input.retry_policy.apply_to_proto(v.retry_policy)
v.cancellation_type = cast(
"temporalio.bridge.proto.workflow_commands.ActivityCancellationType.ValueType",
temporalio.bridge.proto.workflow_commands.ActivityCancellationType.ValueType,
int(self._input.cancellation_type),
)

Expand Down Expand Up @@ -2893,7 +2895,7 @@ def _apply_start_command(self) -> None:
if self._input.task_timeout:
v.workflow_task_timeout.FromTimedelta(self._input.task_timeout)
v.parent_close_policy = cast(
"temporalio.bridge.proto.child_workflow.ParentClosePolicy.ValueType",
temporalio.bridge.proto.child_workflow.ParentClosePolicy.ValueType,
int(self._input.parent_close_policy),
)
v.workflow_id_reuse_policy = cast(
Expand All @@ -2915,7 +2917,7 @@ def _apply_start_command(self) -> None:
self._input.search_attributes, v.search_attributes
)
v.cancellation_type = cast(
"temporalio.bridge.proto.child_workflow.ChildWorkflowCancellationType.ValueType",
temporalio.bridge.proto.child_workflow.ChildWorkflowCancellationType.ValueType,
int(self._input.cancellation_type),
)
if self._input.versioning_intent:
Expand Down Expand Up @@ -3011,11 +3013,6 @@ def __init__(

@property
def operation_token(self) -> Optional[str]:
# TODO(nexus-preview): How should this behave?
# Java has a separate class that only exists if the operation token exists:
# https://github.com/temporalio/sdk-java/blob/master/temporal-sdk/src/main/java/io/temporal/internal/sync/NexusOperationExecutionImpl.java#L26
# And Go similar:
# https://github.com/temporalio/sdk-go/blob/master/internal/workflow.go#L2770-L2771
try:
return self._start_fut.result()
except BaseException:
Expand Down Expand Up @@ -3064,6 +3061,11 @@ def _apply_schedule_command(self) -> None:
v.schedule_to_close_timeout.FromTimedelta(
self._input.schedule_to_close_timeout
)
v.cancellation_type = cast(
temporalio.bridge.proto.nexus.NexusOperationCancellationType.ValueType,
int(self._input.cancellation_type),
)

if self._input.headers:
for key, val in self._input.headers.items():
v.nexus_header[key] = val
Expand Down
49 changes: 46 additions & 3 deletions temporalio/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,9 +858,10 @@ async def workflow_start_nexus_operation(
service: str,
operation: Union[nexusrpc.Operation[InputT, OutputT], str, Callable[..., Any]],
input: Any,
output_type: Optional[Type[OutputT]] = None,
schedule_to_close_timeout: Optional[timedelta] = None,
headers: Optional[Mapping[str, str]] = None,
output_type: Optional[Type[OutputT]],
schedule_to_close_timeout: Optional[timedelta],
cancellation_type: temporalio.workflow.NexusOperationCancellationType,
headers: Optional[Mapping[str, str]],
) -> NexusOperationHandle[OutputT]: ...

@abstractmethod
Expand Down Expand Up @@ -5117,6 +5118,32 @@ def _to_proto(self) -> temporalio.bridge.proto.common.VersioningIntent.ValueType
ServiceT = TypeVar("ServiceT")


class NexusOperationCancellationType(IntEnum):
"""Defines behavior of the parent workflow when CancellationScope that wraps Nexus operation
is canceled. The result of the cancellation independently of the type is a
CanceledFailure thrown from the Nexus operation method. If the caller exits without waiting, the
cancellation request may not be delivered to the handler, regardless of indicated cancellation
type.
"""

WAIT_COMPLETED = 0
"""Wait for operation completion. Operation may or may not complete as cancelled. Default."""

ABANDON = 1
"""Do not request cancellation of the operation."""

TRY_CANCEL = 2
"""Initiate a cancellation request and immediately report cancellation to the caller. Note that it
doesn't guarantee that cancellation is delivered to the operation handler if the caller exits
before the delivery is done.
"""

WAIT_REQUESTED = 3
"""Request cancellation of the operation and wait for confirmation that the request was received.
Doesn't wait for actual cancellation.
"""


class NexusClient(ABC, Generic[ServiceT]):
"""A client for invoking Nexus operations.

Expand Down Expand Up @@ -5147,6 +5174,7 @@ async def start_operation(
*,
output_type: Optional[type[OutputT]] = None,
schedule_to_close_timeout: Optional[timedelta] = None,
cancellation_type: NexusOperationCancellationType = NexusOperationCancellationType.WAIT_COMPLETED,
headers: Optional[Mapping[str, str]] = None,
) -> NexusOperationHandle[OutputT]: ...

Expand All @@ -5160,6 +5188,7 @@ async def start_operation(
*,
output_type: Optional[type[OutputT]] = None,
schedule_to_close_timeout: Optional[timedelta] = None,
cancellation_type: NexusOperationCancellationType = NexusOperationCancellationType.WAIT_COMPLETED,
headers: Optional[Mapping[str, str]] = None,
) -> NexusOperationHandle[OutputT]: ...

Expand All @@ -5176,6 +5205,7 @@ async def start_operation(
*,
output_type: Optional[type[OutputT]] = None,
schedule_to_close_timeout: Optional[timedelta] = None,
cancellation_type: NexusOperationCancellationType = NexusOperationCancellationType.WAIT_COMPLETED,
headers: Optional[Mapping[str, str]] = None,
) -> NexusOperationHandle[OutputT]: ...

Expand All @@ -5192,6 +5222,7 @@ async def start_operation(
*,
output_type: Optional[type[OutputT]] = None,
schedule_to_close_timeout: Optional[timedelta] = None,
cancellation_type: NexusOperationCancellationType = NexusOperationCancellationType.WAIT_COMPLETED,
headers: Optional[Mapping[str, str]] = None,
) -> NexusOperationHandle[OutputT]: ...

Expand All @@ -5208,6 +5239,7 @@ async def start_operation(
*,
output_type: Optional[type[OutputT]] = None,
schedule_to_close_timeout: Optional[timedelta] = None,
cancellation_type: NexusOperationCancellationType = NexusOperationCancellationType.WAIT_COMPLETED,
headers: Optional[Mapping[str, str]] = None,
) -> NexusOperationHandle[OutputT]: ...

Expand All @@ -5219,6 +5251,7 @@ async def start_operation(
*,
output_type: Optional[type[OutputT]] = None,
schedule_to_close_timeout: Optional[timedelta] = None,
cancellation_type: NexusOperationCancellationType = NexusOperationCancellationType.WAIT_COMPLETED,
headers: Optional[Mapping[str, str]] = None,
) -> Any:
"""Start a Nexus operation and return its handle.
Expand Down Expand Up @@ -5248,6 +5281,7 @@ async def execute_operation(
*,
output_type: Optional[type[OutputT]] = None,
schedule_to_close_timeout: Optional[timedelta] = None,
cancellation_type: NexusOperationCancellationType = NexusOperationCancellationType.WAIT_COMPLETED,
headers: Optional[Mapping[str, str]] = None,
) -> OutputT: ...

Expand All @@ -5261,6 +5295,7 @@ async def execute_operation(
*,
output_type: Optional[type[OutputT]] = None,
schedule_to_close_timeout: Optional[timedelta] = None,
cancellation_type: NexusOperationCancellationType = NexusOperationCancellationType.WAIT_COMPLETED,
headers: Optional[Mapping[str, str]] = None,
) -> OutputT: ...

Expand All @@ -5277,6 +5312,7 @@ async def execute_operation(
*,
output_type: Optional[type[OutputT]] = None,
schedule_to_close_timeout: Optional[timedelta] = None,
cancellation_type: NexusOperationCancellationType = NexusOperationCancellationType.WAIT_COMPLETED,
headers: Optional[Mapping[str, str]] = None,
) -> OutputT: ...

Expand All @@ -5296,6 +5332,7 @@ async def execute_operation(
*,
output_type: Optional[type[OutputT]] = None,
schedule_to_close_timeout: Optional[timedelta] = None,
cancellation_type: NexusOperationCancellationType = NexusOperationCancellationType.WAIT_COMPLETED,
headers: Optional[Mapping[str, str]] = None,
) -> OutputT: ...

Expand All @@ -5312,6 +5349,7 @@ async def execute_operation(
*,
output_type: Optional[type[OutputT]] = None,
schedule_to_close_timeout: Optional[timedelta] = None,
cancellation_type: NexusOperationCancellationType = NexusOperationCancellationType.WAIT_COMPLETED,
headers: Optional[Mapping[str, str]] = None,
) -> OutputT: ...

Expand All @@ -5323,6 +5361,7 @@ async def execute_operation(
*,
output_type: Optional[type[OutputT]] = None,
schedule_to_close_timeout: Optional[timedelta] = None,
cancellation_type: NexusOperationCancellationType = NexusOperationCancellationType.WAIT_COMPLETED,
headers: Optional[Mapping[str, str]] = None,
) -> Any:
"""Execute a Nexus operation and return its result.
Expand Down Expand Up @@ -5374,6 +5413,7 @@ async def start_operation(
*,
output_type: Optional[type] = None,
schedule_to_close_timeout: Optional[timedelta] = None,
cancellation_type: NexusOperationCancellationType = NexusOperationCancellationType.WAIT_COMPLETED,
headers: Optional[Mapping[str, str]] = None,
) -> Any:
return (
Expand All @@ -5384,6 +5424,7 @@ async def start_operation(
input=input,
output_type=output_type,
schedule_to_close_timeout=schedule_to_close_timeout,
cancellation_type=cancellation_type,
headers=headers,
)
)
Expand All @@ -5395,13 +5436,15 @@ async def execute_operation(
*,
output_type: Optional[type] = None,
schedule_to_close_timeout: Optional[timedelta] = None,
cancellation_type: NexusOperationCancellationType = NexusOperationCancellationType.WAIT_COMPLETED,
headers: Optional[Mapping[str, str]] = None,
) -> Any:
handle = await self.start_operation(
operation,
input,
output_type=output_type,
schedule_to_close_timeout=schedule_to_close_timeout,
cancellation_type=cancellation_type,
headers=headers,
)
return await handle
Expand Down
Loading
Loading