Skip to content

Commit 09e42a3

Browse files
dandavisontconley1428
authored andcommitted
Use links from StartWorkflowExecutionResponse if present (#963)
1 parent 96ebdb8 commit 09e42a3

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
@@ -18,6 +18,7 @@
1818
from typing_extensions import Concatenate
1919

2020
import temporalio.api.common.v1
21+
import temporalio.api.workflowservice.v1
2122
import temporalio.client
2223
import temporalio.common
2324
from temporalio.nexus import _link_conversion
@@ -142,25 +143,30 @@ def _get_workflow_event_links(
142143
def _add_outbound_links(
143144
self, workflow_handle: temporalio.client.WorkflowHandle[Any, Any]
144145
):
146+
# If links were not sent in StartWorkflowExecutionResponse then construct them.
147+
wf_event_links: list[temporalio.api.common.v1.Link.WorkflowEvent] = []
145148
try:
146-
link = _link_conversion.workflow_event_to_nexus_link(
147-
_link_conversion.workflow_handle_to_workflow_execution_started_event_link(
148-
workflow_handle
149-
)
149+
if isinstance(
150+
workflow_handle._start_workflow_response,
151+
temporalio.api.workflowservice.v1.StartWorkflowExecutionResponse,
152+
):
153+
if workflow_handle._start_workflow_response.HasField("link"):
154+
if link := workflow_handle._start_workflow_response.link:
155+
if link.HasField("workflow_event"):
156+
wf_event_links.append(link.workflow_event)
157+
if not wf_event_links:
158+
wf_event_links = [
159+
_link_conversion.workflow_execution_started_event_link_from_workflow_handle(
160+
workflow_handle
161+
)
162+
]
163+
self.nexus_context.outbound_links.extend(
164+
_link_conversion.workflow_event_to_nexus_link(link)
165+
for link in wf_event_links
150166
)
151167
except Exception as e:
152168
logger.warning(
153-
f"Failed to create WorkflowExecutionStarted event link for workflow {id}: {e}"
154-
)
155-
else:
156-
self.nexus_context.outbound_links.append(
157-
# TODO(nexus-prerelease): Before, WorkflowRunOperation was generating an EventReference
158-
# link to send back to the caller. Now, it checks if the server returned
159-
# the link in the StartWorkflowExecutionResponse, and if so, send the link
160-
# from the response to the caller. Fallback to generating the link for
161-
# backwards compatibility. PR reference in Go SDK:
162-
# https://github.com/temporalio/sdk-go/pull/1934
163-
link
169+
f"Failed to create WorkflowExecutionStarted event links for workflow {workflow_handle}: {e}"
164170
)
165171
return workflow_handle
166172

0 commit comments

Comments
 (0)