@@ -859,9 +859,10 @@ async def workflow_start_nexus_operation(
859859 service : str ,
860860 operation : Union [nexusrpc .Operation [InputT , OutputT ], str , Callable [..., Any ]],
861861 input : Any ,
862- output_type : Optional [Type [OutputT ]] = None ,
863- schedule_to_close_timeout : Optional [timedelta ] = None ,
864- headers : Optional [Mapping [str , str ]] = None ,
862+ output_type : Optional [Type [OutputT ]],
863+ schedule_to_close_timeout : Optional [timedelta ],
864+ cancellation_type : temporalio .workflow .NexusOperationCancellationType ,
865+ headers : Optional [Mapping [str , str ]],
865866 ) -> NexusOperationHandle [OutputT ]: ...
866867
867868 @abstractmethod
@@ -1322,9 +1323,9 @@ async def sleep(
13221323 This can be in single-line Temporal markdown format.
13231324 """
13241325 await _Runtime .current ().workflow_sleep (
1325- duration = duration . total_seconds ()
1326- if isinstance (duration , timedelta )
1327- else duration ,
1326+ duration = (
1327+ duration . total_seconds () if isinstance (duration , timedelta ) else duration
1328+ ) ,
13281329 summary = summary ,
13291330 )
13301331
@@ -4413,6 +4414,8 @@ class NexusOperationHandle(Generic[OutputT]):
44134414 This API is experimental and unstable.
44144415 """
44154416
4417+ # TODO(nexus-preview): should attempts to instantiate directly throw?
4418+
44164419 def cancel (self ) -> bool :
44174420 """Request cancellation of the operation."""
44184421 raise NotImplementedError
@@ -5138,6 +5141,43 @@ def _to_proto(self) -> temporalio.bridge.proto.common.VersioningIntent.ValueType
51385141ServiceT = TypeVar ("ServiceT" )
51395142
51405143
5144+ class NexusOperationCancellationType (IntEnum ):
5145+ """Defines behavior of a Nexus operation when the caller workflow initiates cancellation.
5146+
5147+ Pass one of these values to :py:meth:`NexusClient.start_operation` to define cancellation
5148+ behavior.
5149+
5150+ To initiate cancellation, use :py:meth:`NexusOperationHandle.cancel` and then `await` the
5151+ operation handle. This will result in a :py:class:`exceptions.NexusOperationError`. The values
5152+ of this enum define what is guaranteed to have happened by that point.
5153+ """
5154+
5155+ ABANDON = int (temporalio .bridge .proto .nexus .NexusOperationCancellationType .ABANDON )
5156+ """Do not send any cancellation request to the operation handler; just report cancellation to the caller"""
5157+
5158+ TRY_CANCEL = int (
5159+ temporalio .bridge .proto .nexus .NexusOperationCancellationType .TRY_CANCEL
5160+ )
5161+ """Send a cancellation request but immediately report cancellation to the caller. Note that this
5162+ does not guarantee that cancellation is delivered to the operation handler if the caller exits
5163+ before the delivery is done.
5164+ """
5165+
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+
51415181class NexusClient (ABC , Generic [ServiceT ]):
51425182 """A client for invoking Nexus operations.
51435183
@@ -5168,6 +5208,7 @@ async def start_operation(
51685208 * ,
51695209 output_type : Optional [Type [OutputT ]] = None ,
51705210 schedule_to_close_timeout : Optional [timedelta ] = None ,
5211+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
51715212 headers : Optional [Mapping [str , str ]] = None ,
51725213 ) -> NexusOperationHandle [OutputT ]: ...
51735214
@@ -5181,6 +5222,7 @@ async def start_operation(
51815222 * ,
51825223 output_type : Optional [Type [OutputT ]] = None ,
51835224 schedule_to_close_timeout : Optional [timedelta ] = None ,
5225+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
51845226 headers : Optional [Mapping [str , str ]] = None ,
51855227 ) -> NexusOperationHandle [OutputT ]: ...
51865228
@@ -5197,6 +5239,7 @@ async def start_operation(
51975239 * ,
51985240 output_type : Optional [Type [OutputT ]] = None ,
51995241 schedule_to_close_timeout : Optional [timedelta ] = None ,
5242+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
52005243 headers : Optional [Mapping [str , str ]] = None ,
52015244 ) -> NexusOperationHandle [OutputT ]: ...
52025245
@@ -5213,6 +5256,7 @@ async def start_operation(
52135256 * ,
52145257 output_type : Optional [Type [OutputT ]] = None ,
52155258 schedule_to_close_timeout : Optional [timedelta ] = None ,
5259+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
52165260 headers : Optional [Mapping [str , str ]] = None ,
52175261 ) -> NexusOperationHandle [OutputT ]: ...
52185262
@@ -5229,6 +5273,7 @@ async def start_operation(
52295273 * ,
52305274 output_type : Optional [Type [OutputT ]] = None ,
52315275 schedule_to_close_timeout : Optional [timedelta ] = None ,
5276+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
52325277 headers : Optional [Mapping [str , str ]] = None ,
52335278 ) -> NexusOperationHandle [OutputT ]: ...
52345279
@@ -5240,6 +5285,7 @@ async def start_operation(
52405285 * ,
52415286 output_type : Optional [Type [OutputT ]] = None ,
52425287 schedule_to_close_timeout : Optional [timedelta ] = None ,
5288+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
52435289 headers : Optional [Mapping [str , str ]] = None ,
52445290 ) -> Any :
52455291 """Start a Nexus operation and return its handle.
@@ -5269,6 +5315,7 @@ async def execute_operation(
52695315 * ,
52705316 output_type : Optional [Type [OutputT ]] = None ,
52715317 schedule_to_close_timeout : Optional [timedelta ] = None ,
5318+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
52725319 headers : Optional [Mapping [str , str ]] = None ,
52735320 ) -> OutputT : ...
52745321
@@ -5282,6 +5329,7 @@ async def execute_operation(
52825329 * ,
52835330 output_type : Optional [Type [OutputT ]] = None ,
52845331 schedule_to_close_timeout : Optional [timedelta ] = None ,
5332+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
52855333 headers : Optional [Mapping [str , str ]] = None ,
52865334 ) -> OutputT : ...
52875335
@@ -5298,6 +5346,7 @@ async def execute_operation(
52985346 * ,
52995347 output_type : Optional [Type [OutputT ]] = None ,
53005348 schedule_to_close_timeout : Optional [timedelta ] = None ,
5349+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
53015350 headers : Optional [Mapping [str , str ]] = None ,
53025351 ) -> OutputT : ...
53035352
@@ -5317,6 +5366,7 @@ async def execute_operation(
53175366 * ,
53185367 output_type : Optional [Type [OutputT ]] = None ,
53195368 schedule_to_close_timeout : Optional [timedelta ] = None ,
5369+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
53205370 headers : Optional [Mapping [str , str ]] = None ,
53215371 ) -> OutputT : ...
53225372
@@ -5333,6 +5383,7 @@ async def execute_operation(
53335383 * ,
53345384 output_type : Optional [Type [OutputT ]] = None ,
53355385 schedule_to_close_timeout : Optional [timedelta ] = None ,
5386+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
53365387 headers : Optional [Mapping [str , str ]] = None ,
53375388 ) -> OutputT : ...
53385389
@@ -5344,6 +5395,7 @@ async def execute_operation(
53445395 * ,
53455396 output_type : Optional [Type [OutputT ]] = None ,
53465397 schedule_to_close_timeout : Optional [timedelta ] = None ,
5398+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
53475399 headers : Optional [Mapping [str , str ]] = None ,
53485400 ) -> Any :
53495401 """Execute a Nexus operation and return its result.
@@ -5395,6 +5447,7 @@ async def start_operation(
53955447 * ,
53965448 output_type : Optional [Type ] = None ,
53975449 schedule_to_close_timeout : Optional [timedelta ] = None ,
5450+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
53985451 headers : Optional [Mapping [str , str ]] = None ,
53995452 ) -> Any :
54005453 return (
@@ -5405,6 +5458,7 @@ async def start_operation(
54055458 input = input ,
54065459 output_type = output_type ,
54075460 schedule_to_close_timeout = schedule_to_close_timeout ,
5461+ cancellation_type = cancellation_type ,
54085462 headers = headers ,
54095463 )
54105464 )
@@ -5416,13 +5470,15 @@ async def execute_operation(
54165470 * ,
54175471 output_type : Optional [Type ] = None ,
54185472 schedule_to_close_timeout : Optional [timedelta ] = None ,
5473+ cancellation_type : NexusOperationCancellationType = NexusOperationCancellationType .WAIT_COMPLETED ,
54195474 headers : Optional [Mapping [str , str ]] = None ,
54205475 ) -> Any :
54215476 handle = await self .start_operation (
54225477 operation ,
54235478 input ,
54245479 output_type = output_type ,
54255480 schedule_to_close_timeout = schedule_to_close_timeout ,
5481+ cancellation_type = cancellation_type ,
54265482 headers = headers ,
54275483 )
54285484 return await handle
0 commit comments