@@ -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
@@ -5137,6 +5138,46 @@ def _to_proto(self) -> temporalio.bridge.proto.common.VersioningIntent.ValueType
51375138ServiceT = TypeVar ("ServiceT" )
51385139
51395140
5141+ class NexusOperationCancellationType (IntEnum ):
5142+ """Defines behavior of a Nexus operation when the caller workflow initiates cancellation.
5143+
5144+ Pass one of these values to :py:meth:`NexusClient.start_operation` to define cancellation
5145+ behavior.
5146+
5147+ To initiate cancellation, use :py:meth:`NexusOperationHandle.cancel` and then `await` the
5148+ operation handle. This will result in a :py:class:`exceptions.NexusOperationError`. The values
5149+ of this enum define what is guaranteed to have happened by that point.
5150+ """
5151+
5152+ ABANDON = int (temporalio .bridge .proto .nexus .NexusOperationCancellationType .ABANDON )
5153+ """Do not send any cancellation request to the operation handler; just report cancellation to the caller"""
5154+
5155+ TRY_CANCEL = int (
5156+ temporalio .bridge .proto .nexus .NexusOperationCancellationType .TRY_CANCEL
5157+ )
5158+ """Send a cancellation request but immediately report cancellation to the caller. Note that this
5159+ does not guarantee that cancellation is delivered to the operation handler if the caller exits
5160+ before the delivery is done.
5161+ """
5162+
5163+ # TODO(nexus-preview): core needs to be updated to handle
5164+ # NexusOperationCancelRequestCompleted and NexusOperationCancelRequestFailed
5165+ # see https://github.com/temporalio/sdk-core/issues/911
5166+ # WAIT_REQUESTED = int(
5167+ # temporalio.bridge.proto.nexus.NexusOperationCancellationType.WAIT_CANCELLATION_REQUESTED
5168+ # )
5169+ # """Send a cancellation request and wait for confirmation that the request was received.
5170+ # Does not wait for the operation to complete.
5171+ # """
5172+
5173+ WAIT_COMPLETED = int (
5174+ temporalio .bridge .proto .nexus .NexusOperationCancellationType .WAIT_CANCELLATION_COMPLETED
5175+ )
5176+ """Send a cancellation request and wait for the operation to complete.
5177+ Note that the operation may not complete as cancelled (for example, if it catches the
5178+ :py:exc:`asyncio.CancelledError` resulting from the cancellation request)."""
5179+
5180+
51405181class NexusClient (ABC , Generic [ServiceT ]):
51415182 """A client for invoking Nexus operations.
51425183
@@ -5167,6 +5208,7 @@ async def start_operation(
51675208 * ,
51685209 output_type : Optional [Type [OutputT ]] = None ,
51695210 schedule_to_close_timeout : Optional [timedelta ] = None ,
5211+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
51705212 headers : Optional [Mapping [str , str ]] = None ,
51715213 ) -> NexusOperationHandle [OutputT ]: ...
51725214
@@ -5180,6 +5222,7 @@ async def start_operation(
51805222 * ,
51815223 output_type : Optional [Type [OutputT ]] = None ,
51825224 schedule_to_close_timeout : Optional [timedelta ] = None ,
5225+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
51835226 headers : Optional [Mapping [str , str ]] = None ,
51845227 ) -> NexusOperationHandle [OutputT ]: ...
51855228
@@ -5196,6 +5239,7 @@ async def start_operation(
51965239 * ,
51975240 output_type : Optional [Type [OutputT ]] = None ,
51985241 schedule_to_close_timeout : Optional [timedelta ] = None ,
5242+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
51995243 headers : Optional [Mapping [str , str ]] = None ,
52005244 ) -> NexusOperationHandle [OutputT ]: ...
52015245
@@ -5212,6 +5256,7 @@ async def start_operation(
52125256 * ,
52135257 output_type : Optional [Type [OutputT ]] = None ,
52145258 schedule_to_close_timeout : Optional [timedelta ] = None ,
5259+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
52155260 headers : Optional [Mapping [str , str ]] = None ,
52165261 ) -> NexusOperationHandle [OutputT ]: ...
52175262
@@ -5228,6 +5273,7 @@ async def start_operation(
52285273 * ,
52295274 output_type : Optional [Type [OutputT ]] = None ,
52305275 schedule_to_close_timeout : Optional [timedelta ] = None ,
5276+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
52315277 headers : Optional [Mapping [str , str ]] = None ,
52325278 ) -> NexusOperationHandle [OutputT ]: ...
52335279
@@ -5239,6 +5285,7 @@ async def start_operation(
52395285 * ,
52405286 output_type : Optional [Type [OutputT ]] = None ,
52415287 schedule_to_close_timeout : Optional [timedelta ] = None ,
5288+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
52425289 headers : Optional [Mapping [str , str ]] = None ,
52435290 ) -> Any :
52445291 """Start a Nexus operation and return its handle.
@@ -5268,6 +5315,7 @@ async def execute_operation(
52685315 * ,
52695316 output_type : Optional [Type [OutputT ]] = None ,
52705317 schedule_to_close_timeout : Optional [timedelta ] = None ,
5318+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
52715319 headers : Optional [Mapping [str , str ]] = None ,
52725320 ) -> OutputT : ...
52735321
@@ -5281,6 +5329,7 @@ async def execute_operation(
52815329 * ,
52825330 output_type : Optional [Type [OutputT ]] = None ,
52835331 schedule_to_close_timeout : Optional [timedelta ] = None ,
5332+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
52845333 headers : Optional [Mapping [str , str ]] = None ,
52855334 ) -> OutputT : ...
52865335
@@ -5297,6 +5346,7 @@ async def execute_operation(
52975346 * ,
52985347 output_type : Optional [Type [OutputT ]] = None ,
52995348 schedule_to_close_timeout : Optional [timedelta ] = None ,
5349+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
53005350 headers : Optional [Mapping [str , str ]] = None ,
53015351 ) -> OutputT : ...
53025352
@@ -5316,6 +5366,7 @@ async def execute_operation(
53165366 * ,
53175367 output_type : Optional [Type [OutputT ]] = None ,
53185368 schedule_to_close_timeout : Optional [timedelta ] = None ,
5369+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
53195370 headers : Optional [Mapping [str , str ]] = None ,
53205371 ) -> OutputT : ...
53215372
@@ -5332,6 +5383,7 @@ async def execute_operation(
53325383 * ,
53335384 output_type : Optional [Type [OutputT ]] = None ,
53345385 schedule_to_close_timeout : Optional [timedelta ] = None ,
5386+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
53355387 headers : Optional [Mapping [str , str ]] = None ,
53365388 ) -> OutputT : ...
53375389
@@ -5343,6 +5395,7 @@ async def execute_operation(
53435395 * ,
53445396 output_type : Optional [Type [OutputT ]] = None ,
53455397 schedule_to_close_timeout : Optional [timedelta ] = None ,
5398+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
53465399 headers : Optional [Mapping [str , str ]] = None ,
53475400 ) -> Any :
53485401 """Execute a Nexus operation and return its result.
@@ -5394,6 +5447,7 @@ async def start_operation(
53945447 * ,
53955448 output_type : Optional [Type ] = None ,
53965449 schedule_to_close_timeout : Optional [timedelta ] = None ,
5450+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
53975451 headers : Optional [Mapping [str , str ]] = None ,
53985452 ) -> Any :
53995453 return (
@@ -5404,6 +5458,7 @@ async def start_operation(
54045458 input = input ,
54055459 output_type = output_type ,
54065460 schedule_to_close_timeout = schedule_to_close_timeout ,
5461+ cancellation_type = cancellation_type ,
54075462 headers = headers ,
54085463 )
54095464 )
@@ -5415,13 +5470,15 @@ async def execute_operation(
54155470 * ,
54165471 output_type : Optional [Type ] = None ,
54175472 schedule_to_close_timeout : Optional [timedelta ] = None ,
5473+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
54185474 headers : Optional [Mapping [str , str ]] = None ,
54195475 ) -> Any :
54205476 handle = await self .start_operation (
54215477 operation ,
54225478 input ,
54235479 output_type = output_type ,
54245480 schedule_to_close_timeout = schedule_to_close_timeout ,
5481+ cancellation_type = cancellation_type ,
54255482 headers = headers ,
54265483 )
54275484 return await handle
0 commit comments