Skip to content

Commit 3d908d6

Browse files
committed
Rearrange activity handle classes
1 parent 9d4dd83 commit 3d908d6

File tree

1 file changed

+126
-137
lines changed

1 file changed

+126
-137
lines changed

temporalio/client.py

Lines changed: 126 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -2849,134 +2849,6 @@ async def workflow_handle(self) -> WorkflowHandle[SelfType, ReturnType]:
28492849
return await self._workflow_handle
28502850

28512851

2852-
@dataclass(frozen=True)
2853-
# TODO: in the future when messages can be sent to activities, we will want the activity handle to
2854-
# be generic in the activity type in addition to the return type (as WorkflowHandle), to support
2855-
# static type inference for signal/query/update.
2856-
class ActivityHandle(Generic[ReturnType]):
2857-
"""Handle representing a activity execution."""
2858-
2859-
client: Client
2860-
"""The client that created the activity handle."""
2861-
2862-
id: str
2863-
"""The activity ID."""
2864-
2865-
run_id: Optional[str] = None
2866-
"""The run ID of the activity."""
2867-
2868-
# TODO: do we support something like `follow_runs: bool`?
2869-
async def result(
2870-
self,
2871-
*,
2872-
rpc_metadata: Mapping[str, Union[str, bytes]] = {},
2873-
rpc_timeout: Optional[timedelta] = None,
2874-
) -> ReturnType:
2875-
"""Wait for result of the activity.
2876-
2877-
Args:
2878-
rpc_metadata: Headers used on the RPC call. Keys here override
2879-
client-level RPC metadata keys.
2880-
rpc_timeout: Optional RPC deadline to set for each RPC call. Note,
2881-
this is the timeout for each history RPC call not this overall
2882-
function.
2883-
2884-
Returns:
2885-
The result of the activity.
2886-
2887-
Raises:
2888-
:py:class:`ActivityFailureError`: If the activity completed with a failure.
2889-
"""
2890-
# Repeatedly issues workflowservice GetActivityResult long-polls.
2891-
raise NotImplementedError
2892-
2893-
async def cancel(
2894-
self,
2895-
*,
2896-
reason: Optional[str] = None,
2897-
rpc_metadata: Mapping[str, Union[str, bytes]] = {},
2898-
rpc_timeout: Optional[timedelta] = None,
2899-
) -> None:
2900-
"""Request cancellation of the activity.
2901-
2902-
Requesting cancellation of an activity does not automatically transition the activity to
2903-
canceled status. If the activity is heartbeating, a :py:class:`exceptions.CancelledError`
2904-
exception will be raised when receiving the heartbeat response; if the activity allows this
2905-
exception to bubble out, the activity will transition to canceled status. If the activity it
2906-
is not heartbeating, this method will have no effect on activity status.
2907-
2908-
Args:
2909-
reason: Reason for the cancellation. Recorded and available via describe.
2910-
rpc_metadata: Headers used on the RPC call.
2911-
rpc_timeout: Optional RPC deadline to set for the RPC call.
2912-
"""
2913-
raise NotImplementedError
2914-
2915-
async def complete(
2916-
self,
2917-
result: Optional[Any] = temporalio.common._arg_unset,
2918-
*,
2919-
rpc_metadata: Mapping[str, Union[str, bytes]] = {},
2920-
rpc_timeout: Optional[timedelta] = None,
2921-
) -> None:
2922-
"""Complete the activity.
2923-
2924-
Args:
2925-
result: Result of the activity if any.
2926-
rpc_metadata: Headers used on the RPC call. Keys here override
2927-
client-level RPC metadata keys.
2928-
rpc_timeout: Optional RPC deadline to set for the RPC call.
2929-
"""
2930-
await self.client._impl.complete_async_activity(
2931-
CompleteAsyncActivityInput(
2932-
id_or_token=ActivityIDReference(
2933-
activity_id=self.id,
2934-
run_id=self.run_id,
2935-
),
2936-
result=result,
2937-
rpc_metadata=rpc_metadata,
2938-
rpc_timeout=rpc_timeout,
2939-
),
2940-
)
2941-
2942-
async def terminate(
2943-
self,
2944-
*,
2945-
reason: Optional[str] = None,
2946-
rpc_metadata: Mapping[str, Union[str, bytes]] = {},
2947-
rpc_timeout: Optional[timedelta] = None,
2948-
) -> None:
2949-
"""Terminate the activity execution immediately.
2950-
2951-
Termination does not reach the worker and the activity code cannot react to it.
2952-
A terminated activity may have a running attempt and will be requested to be
2953-
canceled by the server when it heartbeats.
2954-
2955-
Args:
2956-
reason: Reason for the termination.
2957-
rpc_metadata: Headers used on the RPC call.
2958-
rpc_timeout: Optional RPC deadline to set for the RPC call.
2959-
"""
2960-
raise NotImplementedError
2961-
2962-
async def describe(
2963-
self,
2964-
*,
2965-
rpc_metadata: Mapping[str, Union[str, bytes]] = {},
2966-
rpc_timeout: Optional[timedelta] = None,
2967-
) -> ActivityExecutionDescription:
2968-
"""Describe the activity execution.
2969-
2970-
Args:
2971-
rpc_metadata: Headers used on the RPC call.
2972-
rpc_timeout: Optional RPC deadline to set for the RPC call.
2973-
2974-
Returns:
2975-
Activity execution description.
2976-
"""
2977-
raise NotImplementedError
2978-
2979-
29802852
class ActivityExecutionAsyncIterator:
29812853
"""Asynchronous iterator for :py:class:`ActivityExecution` values."""
29822854

@@ -3124,15 +2996,11 @@ class AsyncActivityIDReference:
31242996
activity_id: str
31252997

31262998

3127-
class AsyncActivityHandle:
3128-
"""Handle representing an external activity for completion and heartbeat."""
2999+
class _BaseActivityHandle:
3000+
"""Handle representing an activity."""
31293001

3130-
def __init__(
3131-
self, client: Client, id_or_token: Union[AsyncActivityIDReference, bytes]
3132-
) -> None:
3133-
"""Create an async activity handle."""
3134-
self._client = client
3135-
self._id_or_token = id_or_token
3002+
_client: Client
3003+
_id_or_token: Union[ActivityIDReference, AsyncActivityIDReference, bytes]
31363004

31373005
async def heartbeat(
31383006
self,
@@ -3232,6 +3100,127 @@ async def report_cancellation(
32323100
)
32333101

32343102

3103+
# TODO: This name is suboptimal now. We could deprecate it and introduce WorkflowActivityHandle as a
3104+
# preferred alias.
3105+
class AsyncActivityHandle(_BaseActivityHandle):
3106+
"""Handle representing an activity started by a workflow."""
3107+
3108+
def __init__(
3109+
self, client: Client, id_or_token: Union[AsyncActivityIDReference, bytes]
3110+
) -> None:
3111+
"""Create an handle to an activity started by a workflow."""
3112+
self._client = client
3113+
self._id_or_token = id_or_token
3114+
3115+
3116+
# TODO: in the future when messages can be sent to activities, we will want the activity handle to
3117+
# be generic in the activity type in addition to the return type (as WorkflowHandle), to support
3118+
# static type inference for signal/query/update.
3119+
class ActivityHandle(Generic[ReturnType], _BaseActivityHandle):
3120+
"""Handle representing a standalone activity execution."""
3121+
3122+
def __init__(self, client: Client, id: str, run_id: Optional[str] = None) -> None:
3123+
"""Create an activity handle.
3124+
3125+
Args:
3126+
client: The client that created the activity handle.
3127+
id: The activity ID.
3128+
run_id: The run ID of the activity.
3129+
"""
3130+
self._client = client
3131+
self._id_or_token = ActivityIDReference(activity_id=id, run_id=run_id)
3132+
self.run_id = run_id
3133+
3134+
# TODO: do we support something like `follow_runs: bool`?
3135+
async def result(
3136+
self,
3137+
*,
3138+
rpc_metadata: Mapping[str, Union[str, bytes]] = {},
3139+
rpc_timeout: Optional[timedelta] = None,
3140+
) -> ReturnType:
3141+
"""Wait for result of the activity.
3142+
3143+
Args:
3144+
rpc_metadata: Headers used on the RPC call. Keys here override
3145+
client-level RPC metadata keys.
3146+
rpc_timeout: Optional RPC deadline to set for each RPC call. Note,
3147+
this is the timeout for each history RPC call not this overall
3148+
function.
3149+
3150+
Returns:
3151+
The result of the activity.
3152+
3153+
Raises:
3154+
:py:class:`ActivityFailureError`: If the activity completed with a failure.
3155+
"""
3156+
# Repeatedly issues workflowservice GetActivityResult long-polls.
3157+
raise NotImplementedError
3158+
3159+
async def cancel(
3160+
self,
3161+
*,
3162+
reason: Optional[str] = None,
3163+
rpc_metadata: Mapping[str, Union[str, bytes]] = {},
3164+
rpc_timeout: Optional[timedelta] = None,
3165+
) -> None:
3166+
"""Request cancellation of the activity.
3167+
3168+
Requesting cancellation of an activity does not automatically transition the activity to
3169+
canceled status. If the activity is heartbeating, a :py:class:`exceptions.CancelledError`
3170+
exception will be raised when receiving the heartbeat response; if the activity allows this
3171+
exception to bubble out, the activity will transition to canceled status. If the activity it
3172+
is not heartbeating, this method will have no effect on activity status.
3173+
3174+
Args:
3175+
reason: Reason for the cancellation. Recorded and available via describe.
3176+
rpc_metadata: Headers used on the RPC call.
3177+
rpc_timeout: Optional RPC deadline to set for the RPC call.
3178+
"""
3179+
raise NotImplementedError
3180+
3181+
async def terminate(
3182+
self,
3183+
*,
3184+
reason: Optional[str] = None,
3185+
rpc_metadata: Mapping[str, Union[str, bytes]] = {},
3186+
rpc_timeout: Optional[timedelta] = None,
3187+
) -> None:
3188+
"""Terminate the activity execution immediately.
3189+
3190+
Termination does not reach the worker and the activity code cannot react to it.
3191+
A terminated activity may have a running attempt and will be requested to be
3192+
canceled by the server when it heartbeats.
3193+
3194+
Args:
3195+
reason: Reason for the termination.
3196+
rpc_metadata: Headers used on the RPC call.
3197+
rpc_timeout: Optional RPC deadline to set for the RPC call.
3198+
"""
3199+
raise NotImplementedError
3200+
3201+
async def describe(
3202+
self,
3203+
*,
3204+
rpc_metadata: Mapping[str, Union[str, bytes]] = {},
3205+
rpc_timeout: Optional[timedelta] = None,
3206+
) -> ActivityExecutionDescription:
3207+
"""Describe the activity execution.
3208+
3209+
Args:
3210+
rpc_metadata: Headers used on the RPC call.
3211+
rpc_timeout: Optional RPC deadline to set for the RPC call.
3212+
3213+
Returns:
3214+
Activity execution description.
3215+
"""
3216+
raise NotImplementedError
3217+
3218+
# TODO:
3219+
# pause
3220+
# reset
3221+
# update_options
3222+
3223+
32353224
@dataclass
32363225
class WorkflowExecution:
32373226
"""Info for a single workflow execution run."""
@@ -5867,7 +5856,7 @@ class StartWorkflowUpdateWithStartInput:
58675856
class HeartbeatAsyncActivityInput:
58685857
"""Input for :py:meth:`OutboundInterceptor.heartbeat_async_activity`."""
58695858

5870-
id_or_token: Union[AsyncActivityIDReference, bytes]
5859+
id_or_token: Union[AsyncActivityIDReference, ActivityIDReference, bytes]
58715860
details: Sequence[Any]
58725861
rpc_metadata: Mapping[str, Union[str, bytes]]
58735862
rpc_timeout: Optional[timedelta]

0 commit comments

Comments
 (0)