Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions temporalio/activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -155,6 +173,14 @@ class Info:
This API is experimental and unstable.
"""

workflow_task: WorkflowTaskAttempt | None = None
"""For a Local Activity, the Workflow Task in which the Worker dispatched this 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?"""
Expand Down
9 changes: 9 additions & 0 deletions temporalio/worker/_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,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:
Expand Down
39 changes: 39 additions & 0 deletions tests/worker/test_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -2208,3 +2208,42 @@ 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() -> list[int] | None:
task = activity.info().workflow_task
return None if task is None else [task.started_event_id, task.attempt]


@workflow.defn(sandboxed=False)
class WorkflowTaskInfoWorkflow:
@workflow.run
async def run(self) -> list[list[int] | 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],
):
local, regular = await client.execute_workflow(
WorkflowTaskInfoWorkflow.run,
id=f"workflow-{uuid.uuid4()}",
task_queue=task_queue,
)
assert local is not None
started_event_id, attempt = local
assert started_event_id > 0
assert attempt == 1
assert regular is None