-
Notifications
You must be signed in to change notification settings - Fork 134
Support multiple nexus callers attaching to same workflow #1051
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dandavison
merged 3 commits into
main
from
dan-9995-nexus-callers-attach-to-same-workflow
Aug 25, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
from __future__ import annotations | ||
|
||
import asyncio | ||
import uuid | ||
from dataclasses import dataclass | ||
from typing import Optional | ||
|
||
import pytest | ||
from nexusrpc.handler import service_handler | ||
|
||
from temporalio import nexus, workflow | ||
from temporalio.client import Client | ||
from temporalio.common import WorkflowIDConflictPolicy | ||
from temporalio.testing import WorkflowEnvironment | ||
from temporalio.worker import Worker | ||
from tests.helpers.nexus import create_nexus_endpoint, make_nexus_endpoint_name | ||
|
||
|
||
@dataclass | ||
class OpInput: | ||
workflow_id: str | ||
conflict_policy: WorkflowIDConflictPolicy | ||
|
||
|
||
@workflow.defn | ||
class HandlerWorkflow: | ||
def __init__(self) -> None: | ||
self.result: Optional[str] = None | ||
|
||
@workflow.run | ||
async def run(self) -> str: | ||
await workflow.wait_condition(lambda: self.result is not None) | ||
assert self.result | ||
return self.result | ||
|
||
@workflow.signal | ||
def complete(self, result: str) -> None: | ||
self.result = result | ||
|
||
|
||
@service_handler | ||
class NexusService: | ||
@nexus.workflow_run_operation | ||
async def workflow_backed_operation( | ||
self, ctx: nexus.WorkflowRunOperationContext, input: OpInput | ||
) -> nexus.WorkflowHandle[str]: | ||
return await ctx.start_workflow( | ||
HandlerWorkflow.run, | ||
id=input.workflow_id, | ||
id_conflict_policy=input.conflict_policy, | ||
) | ||
|
||
|
||
@dataclass | ||
class CallerWorkflowInput: | ||
workflow_id: str | ||
task_queue: str | ||
num_operations: int | ||
|
||
|
||
@workflow.defn | ||
class CallerWorkflow: | ||
def __init__(self) -> None: | ||
self._nexus_operations_have_started = asyncio.Event() | ||
|
||
@workflow.run | ||
async def run(self, input: CallerWorkflowInput) -> list[str]: | ||
nexus_client = workflow.create_nexus_client( | ||
service=NexusService, endpoint=make_nexus_endpoint_name(input.task_queue) | ||
) | ||
|
||
op_input = OpInput( | ||
workflow_id=input.workflow_id, | ||
conflict_policy=WorkflowIDConflictPolicy.USE_EXISTING, | ||
) | ||
|
||
handles = [] | ||
for _ in range(input.num_operations): | ||
handles.append( | ||
await nexus_client.start_operation( | ||
NexusService.workflow_backed_operation, op_input | ||
) | ||
) | ||
self._nexus_operations_have_started.set() | ||
return await asyncio.gather(*handles) | ||
|
||
@workflow.update | ||
async def nexus_operations_have_started(self) -> None: | ||
await self._nexus_operations_have_started.wait() | ||
|
||
|
||
async def test_multiple_operation_invocations_can_connect_to_same_handler_workflow( | ||
client: Client, env: WorkflowEnvironment | ||
): | ||
if env.supports_time_skipping: | ||
pytest.skip("Nexus tests don't work with time-skipping server") | ||
|
||
task_queue = str(uuid.uuid4()) | ||
workflow_id = str(uuid.uuid4()) | ||
|
||
async with Worker( | ||
client, | ||
nexus_service_handlers=[NexusService()], | ||
workflows=[CallerWorkflow, HandlerWorkflow], | ||
task_queue=task_queue, | ||
): | ||
await create_nexus_endpoint(task_queue, client) | ||
caller_handle = await client.start_workflow( | ||
CallerWorkflow.run, | ||
args=[ | ||
CallerWorkflowInput( | ||
workflow_id=workflow_id, | ||
task_queue=task_queue, | ||
num_operations=5, | ||
) | ||
], | ||
id=str(uuid.uuid4()), | ||
task_queue=task_queue, | ||
) | ||
await caller_handle.execute_update(CallerWorkflow.nexus_operations_have_started) | ||
await client.get_workflow_handle(workflow_id).signal( | ||
HandlerWorkflow.complete, "test-result" | ||
) | ||
assert await caller_handle.result() == ["test-result"] * 5 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Nexus Signal-with-Start Missing Conflict Options
The
_build_signal_with_start_workflow_execution_request
method is missing theon_conflict_options
logic for Nexus operations. This logic, which sets options for attaching request IDs, callbacks, and links, is present in_build_start_workflow_execution_request
. Without it, Nexus operations using signal-with-start won't properly attach to existing workflows, causing inconsistent behavior.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a bad review comment, but we don't need to support signal-with-start in this context yet.