@@ -858,9 +858,10 @@ async def workflow_start_nexus_operation(
858858 service : str ,
859859 operation : Union [nexusrpc .Operation [InputT , OutputT ], str , Callable [..., Any ]],
860860 input : Any ,
861- output_type : Optional [Type [OutputT ]] = None ,
862- schedule_to_close_timeout : Optional [timedelta ] = None ,
863- headers : Optional [Mapping [str , str ]] = None ,
861+ output_type : Optional [Type [OutputT ]],
862+ schedule_to_close_timeout : Optional [timedelta ],
863+ cancellation_type : temporalio .workflow .NexusOperationCancellationType ,
864+ headers : Optional [Mapping [str , str ]],
864865 ) -> NexusOperationHandle [OutputT ]: ...
865866
866867 @abstractmethod
@@ -5117,6 +5118,46 @@ def _to_proto(self) -> temporalio.bridge.proto.common.VersioningIntent.ValueType
51175118ServiceT = TypeVar ("ServiceT" )
51185119
51195120
5121+ class NexusOperationCancellationType (IntEnum ):
5122+ """Defines behavior of a Nexus operation when the caller workflow initiates cancellation.
5123+
5124+ Pass one of these values to :py:meth:`NexusClient.start_operation` to define cancellation
5125+ behavior.
5126+
5127+ To initiate cancellation, use :py:meth:`NexusOperationHandle.cancel` and then `await` the
5128+ operation handle. This will result in a :py:class:`exceptions.NexusOperationError`. The values
5129+ of this enum define what is guaranteed to have happened by that point.
5130+ """
5131+
5132+ ABANDON = int (temporalio .bridge .proto .nexus .NexusOperationCancellationType .ABANDON )
5133+ """Do not send any cancellation request to the operation handler; just report cancellation to the caller"""
5134+
5135+ TRY_CANCEL = int (
5136+ temporalio .bridge .proto .nexus .NexusOperationCancellationType .TRY_CANCEL
5137+ )
5138+ """Send a cancellation request but immediately report cancellation to the caller. Note that this
5139+ does not guarantee that cancellation is delivered to the operation handler if the caller exits
5140+ before the delivery is done.
5141+ """
5142+
5143+ # TODO(nexus-preview): core needs to be updated to handle
5144+ # NexusOperationCancelRequestCompleted and NexusOperationCancelRequestFailed
5145+ # see https://github.com/temporalio/sdk-core/issues/911
5146+ # WAIT_REQUESTED = int(
5147+ # temporalio.bridge.proto.nexus.NexusOperationCancellationType.WAIT_CANCELLATION_REQUESTED
5148+ # )
5149+ # """Send a cancellation request and wait for confirmation that the request was received.
5150+ # Does not wait for the operation to complete.
5151+ # """
5152+
5153+ WAIT_COMPLETED = int (
5154+ temporalio .bridge .proto .nexus .NexusOperationCancellationType .WAIT_CANCELLATION_COMPLETED
5155+ )
5156+ """Send a cancellation request and wait for the operation to complete.
5157+ Note that the operation may not complete as cancelled (for example, if it catches the
5158+ :py:exc:`asyncio.CancelledError` resulting from the cancellation request)."""
5159+
5160+
51205161class NexusClient (ABC , Generic [ServiceT ]):
51215162 """A client for invoking Nexus operations.
51225163
@@ -5147,6 +5188,7 @@ async def start_operation(
51475188 * ,
51485189 output_type : Optional [Type [OutputT ]] = None ,
51495190 schedule_to_close_timeout : Optional [timedelta ] = None ,
5191+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
51505192 headers : Optional [Mapping [str , str ]] = None ,
51515193 ) -> NexusOperationHandle [OutputT ]: ...
51525194
@@ -5160,6 +5202,7 @@ async def start_operation(
51605202 * ,
51615203 output_type : Optional [Type [OutputT ]] = None ,
51625204 schedule_to_close_timeout : Optional [timedelta ] = None ,
5205+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
51635206 headers : Optional [Mapping [str , str ]] = None ,
51645207 ) -> NexusOperationHandle [OutputT ]: ...
51655208
@@ -5176,6 +5219,7 @@ async def start_operation(
51765219 * ,
51775220 output_type : Optional [Type [OutputT ]] = None ,
51785221 schedule_to_close_timeout : Optional [timedelta ] = None ,
5222+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
51795223 headers : Optional [Mapping [str , str ]] = None ,
51805224 ) -> NexusOperationHandle [OutputT ]: ...
51815225
@@ -5192,6 +5236,7 @@ async def start_operation(
51925236 * ,
51935237 output_type : Optional [Type [OutputT ]] = None ,
51945238 schedule_to_close_timeout : Optional [timedelta ] = None ,
5239+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
51955240 headers : Optional [Mapping [str , str ]] = None ,
51965241 ) -> NexusOperationHandle [OutputT ]: ...
51975242
@@ -5208,6 +5253,7 @@ async def start_operation(
52085253 * ,
52095254 output_type : Optional [Type [OutputT ]] = None ,
52105255 schedule_to_close_timeout : Optional [timedelta ] = None ,
5256+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
52115257 headers : Optional [Mapping [str , str ]] = None ,
52125258 ) -> NexusOperationHandle [OutputT ]: ...
52135259
@@ -5219,6 +5265,7 @@ async def start_operation(
52195265 * ,
52205266 output_type : Optional [Type [OutputT ]] = None ,
52215267 schedule_to_close_timeout : Optional [timedelta ] = None ,
5268+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
52225269 headers : Optional [Mapping [str , str ]] = None ,
52235270 ) -> Any :
52245271 """Start a Nexus operation and return its handle.
@@ -5248,6 +5295,7 @@ async def execute_operation(
52485295 * ,
52495296 output_type : Optional [Type [OutputT ]] = None ,
52505297 schedule_to_close_timeout : Optional [timedelta ] = None ,
5298+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
52515299 headers : Optional [Mapping [str , str ]] = None ,
52525300 ) -> OutputT : ...
52535301
@@ -5261,6 +5309,7 @@ async def execute_operation(
52615309 * ,
52625310 output_type : Optional [Type [OutputT ]] = None ,
52635311 schedule_to_close_timeout : Optional [timedelta ] = None ,
5312+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
52645313 headers : Optional [Mapping [str , str ]] = None ,
52655314 ) -> OutputT : ...
52665315
@@ -5277,6 +5326,7 @@ async def execute_operation(
52775326 * ,
52785327 output_type : Optional [Type [OutputT ]] = None ,
52795328 schedule_to_close_timeout : Optional [timedelta ] = None ,
5329+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
52805330 headers : Optional [Mapping [str , str ]] = None ,
52815331 ) -> OutputT : ...
52825332
@@ -5296,6 +5346,7 @@ async def execute_operation(
52965346 * ,
52975347 output_type : Optional [Type [OutputT ]] = None ,
52985348 schedule_to_close_timeout : Optional [timedelta ] = None ,
5349+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
52995350 headers : Optional [Mapping [str , str ]] = None ,
53005351 ) -> OutputT : ...
53015352
@@ -5312,6 +5363,7 @@ async def execute_operation(
53125363 * ,
53135364 output_type : Optional [Type [OutputT ]] = None ,
53145365 schedule_to_close_timeout : Optional [timedelta ] = None ,
5366+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
53155367 headers : Optional [Mapping [str , str ]] = None ,
53165368 ) -> OutputT : ...
53175369
@@ -5323,6 +5375,7 @@ async def execute_operation(
53235375 * ,
53245376 output_type : Optional [Type [OutputT ]] = None ,
53255377 schedule_to_close_timeout : Optional [timedelta ] = None ,
5378+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
53265379 headers : Optional [Mapping [str , str ]] = None ,
53275380 ) -> Any :
53285381 """Execute a Nexus operation and return its result.
@@ -5374,6 +5427,7 @@ async def start_operation(
53745427 * ,
53755428 output_type : Optional [Type ] = None ,
53765429 schedule_to_close_timeout : Optional [timedelta ] = None ,
5430+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
53775431 headers : Optional [Mapping [str , str ]] = None ,
53785432 ) -> Any :
53795433 return (
@@ -5384,6 +5438,7 @@ async def start_operation(
53845438 input = input ,
53855439 output_type = output_type ,
53865440 schedule_to_close_timeout = schedule_to_close_timeout ,
5441+ cancellation_type = cancellation_type ,
53875442 headers = headers ,
53885443 )
53895444 )
@@ -5395,13 +5450,15 @@ async def execute_operation(
53955450 * ,
53965451 output_type : Optional [Type ] = None ,
53975452 schedule_to_close_timeout : Optional [timedelta ] = None ,
5453+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
53985454 headers : Optional [Mapping [str , str ]] = None ,
53995455 ) -> Any :
54005456 handle = await self .start_operation (
54015457 operation ,
54025458 input ,
54035459 output_type = output_type ,
54045460 schedule_to_close_timeout = schedule_to_close_timeout ,
5461+ cancellation_type = cancellation_type ,
54055462 headers = headers ,
54065463 )
54075464 return await handle
0 commit comments