@@ -858,9 +858,10 @@ async def workflow_start_nexus_operation(
858
858
service : str ,
859
859
operation : Union [nexusrpc .Operation [InputT , OutputT ], str , Callable [..., Any ]],
860
860
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 ]],
864
865
) -> NexusOperationHandle [OutputT ]: ...
865
866
866
867
@abstractmethod
@@ -5117,6 +5118,46 @@ def _to_proto(self) -> temporalio.bridge.proto.common.VersioningIntent.ValueType
5117
5118
ServiceT = TypeVar ("ServiceT" )
5118
5119
5119
5120
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
+
5120
5161
class NexusClient (ABC , Generic [ServiceT ]):
5121
5162
"""A client for invoking Nexus operations.
5122
5163
@@ -5147,6 +5188,7 @@ async def start_operation(
5147
5188
* ,
5148
5189
output_type : Optional [Type [OutputT ]] = None ,
5149
5190
schedule_to_close_timeout : Optional [timedelta ] = None ,
5191
+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
5150
5192
headers : Optional [Mapping [str , str ]] = None ,
5151
5193
) -> NexusOperationHandle [OutputT ]: ...
5152
5194
@@ -5160,6 +5202,7 @@ async def start_operation(
5160
5202
* ,
5161
5203
output_type : Optional [Type [OutputT ]] = None ,
5162
5204
schedule_to_close_timeout : Optional [timedelta ] = None ,
5205
+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
5163
5206
headers : Optional [Mapping [str , str ]] = None ,
5164
5207
) -> NexusOperationHandle [OutputT ]: ...
5165
5208
@@ -5176,6 +5219,7 @@ async def start_operation(
5176
5219
* ,
5177
5220
output_type : Optional [Type [OutputT ]] = None ,
5178
5221
schedule_to_close_timeout : Optional [timedelta ] = None ,
5222
+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
5179
5223
headers : Optional [Mapping [str , str ]] = None ,
5180
5224
) -> NexusOperationHandle [OutputT ]: ...
5181
5225
@@ -5192,6 +5236,7 @@ async def start_operation(
5192
5236
* ,
5193
5237
output_type : Optional [Type [OutputT ]] = None ,
5194
5238
schedule_to_close_timeout : Optional [timedelta ] = None ,
5239
+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
5195
5240
headers : Optional [Mapping [str , str ]] = None ,
5196
5241
) -> NexusOperationHandle [OutputT ]: ...
5197
5242
@@ -5208,6 +5253,7 @@ async def start_operation(
5208
5253
* ,
5209
5254
output_type : Optional [Type [OutputT ]] = None ,
5210
5255
schedule_to_close_timeout : Optional [timedelta ] = None ,
5256
+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
5211
5257
headers : Optional [Mapping [str , str ]] = None ,
5212
5258
) -> NexusOperationHandle [OutputT ]: ...
5213
5259
@@ -5219,6 +5265,7 @@ async def start_operation(
5219
5265
* ,
5220
5266
output_type : Optional [Type [OutputT ]] = None ,
5221
5267
schedule_to_close_timeout : Optional [timedelta ] = None ,
5268
+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
5222
5269
headers : Optional [Mapping [str , str ]] = None ,
5223
5270
) -> Any :
5224
5271
"""Start a Nexus operation and return its handle.
@@ -5248,6 +5295,7 @@ async def execute_operation(
5248
5295
* ,
5249
5296
output_type : Optional [Type [OutputT ]] = None ,
5250
5297
schedule_to_close_timeout : Optional [timedelta ] = None ,
5298
+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
5251
5299
headers : Optional [Mapping [str , str ]] = None ,
5252
5300
) -> OutputT : ...
5253
5301
@@ -5261,6 +5309,7 @@ async def execute_operation(
5261
5309
* ,
5262
5310
output_type : Optional [Type [OutputT ]] = None ,
5263
5311
schedule_to_close_timeout : Optional [timedelta ] = None ,
5312
+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
5264
5313
headers : Optional [Mapping [str , str ]] = None ,
5265
5314
) -> OutputT : ...
5266
5315
@@ -5277,6 +5326,7 @@ async def execute_operation(
5277
5326
* ,
5278
5327
output_type : Optional [Type [OutputT ]] = None ,
5279
5328
schedule_to_close_timeout : Optional [timedelta ] = None ,
5329
+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
5280
5330
headers : Optional [Mapping [str , str ]] = None ,
5281
5331
) -> OutputT : ...
5282
5332
@@ -5296,6 +5346,7 @@ async def execute_operation(
5296
5346
* ,
5297
5347
output_type : Optional [Type [OutputT ]] = None ,
5298
5348
schedule_to_close_timeout : Optional [timedelta ] = None ,
5349
+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
5299
5350
headers : Optional [Mapping [str , str ]] = None ,
5300
5351
) -> OutputT : ...
5301
5352
@@ -5312,6 +5363,7 @@ async def execute_operation(
5312
5363
* ,
5313
5364
output_type : Optional [Type [OutputT ]] = None ,
5314
5365
schedule_to_close_timeout : Optional [timedelta ] = None ,
5366
+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
5315
5367
headers : Optional [Mapping [str , str ]] = None ,
5316
5368
) -> OutputT : ...
5317
5369
@@ -5323,6 +5375,7 @@ async def execute_operation(
5323
5375
* ,
5324
5376
output_type : Optional [Type [OutputT ]] = None ,
5325
5377
schedule_to_close_timeout : Optional [timedelta ] = None ,
5378
+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
5326
5379
headers : Optional [Mapping [str , str ]] = None ,
5327
5380
) -> Any :
5328
5381
"""Execute a Nexus operation and return its result.
@@ -5374,6 +5427,7 @@ async def start_operation(
5374
5427
* ,
5375
5428
output_type : Optional [Type ] = None ,
5376
5429
schedule_to_close_timeout : Optional [timedelta ] = None ,
5430
+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
5377
5431
headers : Optional [Mapping [str , str ]] = None ,
5378
5432
) -> Any :
5379
5433
return (
@@ -5384,6 +5438,7 @@ async def start_operation(
5384
5438
input = input ,
5385
5439
output_type = output_type ,
5386
5440
schedule_to_close_timeout = schedule_to_close_timeout ,
5441
+ cancellation_type = cancellation_type ,
5387
5442
headers = headers ,
5388
5443
)
5389
5444
)
@@ -5395,13 +5450,15 @@ async def execute_operation(
5395
5450
* ,
5396
5451
output_type : Optional [Type ] = None ,
5397
5452
schedule_to_close_timeout : Optional [timedelta ] = None ,
5453
+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
5398
5454
headers : Optional [Mapping [str , str ]] = None ,
5399
5455
) -> Any :
5400
5456
handle = await self .start_operation (
5401
5457
operation ,
5402
5458
input ,
5403
5459
output_type = output_type ,
5404
5460
schedule_to_close_timeout = schedule_to_close_timeout ,
5461
+ cancellation_type = cancellation_type ,
5405
5462
headers = headers ,
5406
5463
)
5407
5464
return await handle
0 commit comments