Skip to content
Open
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
1 change: 1 addition & 0 deletions changelog/added/punctual-ticket-puffin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `activity.Info.workflow_task` so Local Activities can distinguish executions across Workflow Task attempts.
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
"""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?"""
Expand Down
9 changes: 9 additions & 0 deletions temporalio/worker/_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
51 changes: 51 additions & 0 deletions tests/worker/test_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading