diff --git a/changelog/added/punctual-ticket-puffin.md b/changelog/added/punctual-ticket-puffin.md new file mode 100644 index 000000000..cf1dc9299 --- /dev/null +++ b/changelog/added/punctual-ticket-puffin.md @@ -0,0 +1 @@ +Add `activity.Info.workflow_task` so Local Activities can distinguish executions across Workflow Task attempts. diff --git a/temporalio/activity.py b/temporalio/activity.py index bf1b4cb73..b2afae017 100644 --- a/temporalio/activity.py +++ b/temporalio/activity.py @@ -91,6 +91,24 @@ def decorator(fn: CallableType) -> CallableType: return decorator +@dataclass(frozen=True) +class WorkflowTaskAttempt: + """One attempt of a Workflow Task. + + .. warning:: + This API is experimental and unstable. + """ + + started_event_id: int + """ID of the task's ``WorkflowTaskStarted`` event.""" + + attempt: int + """Attempt of the task.""" + + started_time: datetime | None + """When the server started the task. None if the server did not send it.""" + + @dataclass(frozen=True) class Info: """Information about the running activity. @@ -155,6 +173,14 @@ class Info: This API is experimental and unstable. """ + workflow_task: WorkflowTaskAttempt | None = None + """Workflow Task attempt that dispatched this Local Activity execution. + ``None`` for other Activities. + + .. warning:: + This API is experimental and unstable. + """ + @property def in_workflow(self) -> bool: """Was this activity started by a workflow?""" diff --git a/temporalio/worker/_activity.py b/temporalio/worker/_activity.py index 79af99070..7c95449df 100644 --- a/temporalio/worker/_activity.py +++ b/temporalio/worker/_activity.py @@ -700,6 +700,15 @@ async def _execute_activity( if not started_by_workflow else None, nexus_serialization_context=nexus_context, + workflow_task=temporalio.activity.WorkflowTaskAttempt( + started_event_id=start.workflow_task.started_event_id, + attempt=start.workflow_task.attempt, + started_time=_proto_to_datetime(start.workflow_task.started_time) + if start.workflow_task.HasField("started_time") + else None, + ) + if start.HasField("workflow_task") + else None, ) if self._encode_headers: diff --git a/tests/worker/test_activity.py b/tests/worker/test_activity.py index 5b21c3d0a..01e7e88c0 100644 --- a/tests/worker/test_activity.py +++ b/tests/worker/test_activity.py @@ -2208,3 +2208,54 @@ async def test_local_activities_with_no_remote_activities_option(client: Client) ) assert result == "Hello from local activity, Temporal!" + + +@activity.defn +async def capture_workflow_task() -> activity.WorkflowTaskAttempt | None: + return activity.info().workflow_task + + +@workflow.defn(sandboxed=False) +class WorkflowTaskInfoWorkflow: + @workflow.run + async def run(self) -> list[activity.WorkflowTaskAttempt | None]: + local = await workflow.execute_local_activity( + capture_workflow_task, start_to_close_timeout=timedelta(seconds=5) + ) + regular = await workflow.execute_activity( + capture_workflow_task, start_to_close_timeout=timedelta(seconds=5) + ) + return [local, regular] + + +async def test_activity_info_workflow_task(client: Client): + task_queue = f"tq-{uuid.uuid4()}" + async with Worker( + client, + task_queue=task_queue, + activities=[capture_workflow_task], + workflows=[WorkflowTaskInfoWorkflow], + ): + handle = await client.start_workflow( + WorkflowTaskInfoWorkflow.run, + id=f"workflow-{uuid.uuid4()}", + task_queue=task_queue, + ) + local, regular = await handle.result() + history = await handle.fetch_history() + started = next( + event + for event in history.events + if event.HasField("workflow_task_started_event_attributes") + ) + scheduled = next( + event + for event in history.events + if event.event_id + == started.workflow_task_started_event_attributes.scheduled_event_id + ) + assert local is not None + assert local.started_event_id == started.event_id + assert local.attempt == scheduled.workflow_task_scheduled_event_attributes.attempt + assert local.started_time == started.event_time.ToDatetime(tzinfo=timezone.utc) + assert regular is None