Skip to content

Commit 62c2533

Browse files
committed
Use links from StartWorkflowExecutionResponse if present
1 parent 28f43f1 commit 62c2533

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

temporalio/client.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,6 +1542,12 @@ def __init__(
15421542
result_run_id: Optional[str] = None,
15431543
first_execution_run_id: Optional[str] = None,
15441544
result_type: Optional[Type] = None,
1545+
start_workflow_response: Optional[
1546+
Union[
1547+
temporalio.api.workflowservice.v1.StartWorkflowExecutionResponse,
1548+
temporalio.api.workflowservice.v1.SignalWithStartWorkflowExecutionResponse,
1549+
]
1550+
] = None,
15451551
) -> None:
15461552
"""Create workflow handle."""
15471553
self._client = client
@@ -1550,6 +1556,7 @@ def __init__(
15501556
self._result_run_id = result_run_id
15511557
self._first_execution_run_id = first_execution_run_id
15521558
self._result_type = result_type
1559+
self._start_workflow_response = start_workflow_response
15531560
self.__temporal_eagerly_started = False
15541561

15551562
@property
@@ -5832,6 +5839,7 @@ async def start_workflow(
58325839
result_run_id=resp.run_id,
58335840
first_execution_run_id=first_execution_run_id,
58345841
result_type=input.ret_type,
5842+
start_workflow_response=resp,
58355843
)
58365844
setattr(handle, "__temporal_eagerly_started", eagerly_started)
58375845
return handle

temporalio/nexus/_link_conversion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
LINK_EVENT_TYPE_PARAM_NAME = "eventType"
2424

2525

26-
def workflow_handle_to_workflow_execution_started_event_link(
26+
def workflow_execution_started_event_link_from_workflow_handle(
2727
handle: temporalio.client.WorkflowHandle[Any, Any],
2828
) -> temporalio.api.common.v1.Link.WorkflowEvent:
2929
"""Create a WorkflowEvent link corresponding to a started workflow"""

temporalio/nexus/_operation_context.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from typing_extensions import Concatenate
2323

2424
import temporalio.api.common.v1
25+
import temporalio.api.workflowservice.v1
2526
import temporalio.client
2627
import temporalio.common
2728
from temporalio.nexus import _link_conversion
@@ -146,25 +147,30 @@ def _get_workflow_event_links(
146147
def _add_outbound_links(
147148
self, workflow_handle: temporalio.client.WorkflowHandle[Any, Any]
148149
):
150+
# If links were not sent in StartWorkflowExecutionResponse then construct them.
151+
wf_event_links: list[temporalio.api.common.v1.Link.WorkflowEvent] = []
149152
try:
150-
link = _link_conversion.workflow_event_to_nexus_link(
151-
_link_conversion.workflow_handle_to_workflow_execution_started_event_link(
152-
workflow_handle
153-
)
153+
if isinstance(
154+
workflow_handle._start_workflow_response,
155+
temporalio.api.workflowservice.v1.StartWorkflowExecutionResponse,
156+
):
157+
if workflow_handle._start_workflow_response.HasField("link"):
158+
if link := workflow_handle._start_workflow_response.link:
159+
if link.HasField("workflow_event"):
160+
wf_event_links.append(link.workflow_event)
161+
if not wf_event_links:
162+
wf_event_links = [
163+
_link_conversion.workflow_execution_started_event_link_from_workflow_handle(
164+
workflow_handle
165+
)
166+
]
167+
self.nexus_context.outbound_links.extend(
168+
_link_conversion.workflow_event_to_nexus_link(link)
169+
for link in wf_event_links
154170
)
155171
except Exception as e:
156172
logger.warning(
157-
f"Failed to create WorkflowExecutionStarted event link for workflow {id}: {e}"
158-
)
159-
else:
160-
self.nexus_context.outbound_links.append(
161-
# TODO(nexus-prerelease): Before, WorkflowRunOperation was generating an EventReference
162-
# link to send back to the caller. Now, it checks if the server returned
163-
# the link in the StartWorkflowExecutionResponse, and if so, send the link
164-
# from the response to the caller. Fallback to generating the link for
165-
# backwards compatibility. PR reference in Go SDK:
166-
# https://github.com/temporalio/sdk-go/pull/1934
167-
link
173+
f"Failed to create WorkflowExecutionStarted event links for workflow {workflow_handle}: {e}"
168174
)
169175
return workflow_handle
170176

0 commit comments

Comments
 (0)