Skip to content
Draft
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/fixed/stubborn-carrier-pigeon.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**Experimental**: Standalone Nexus operations started with `USE_EXISTING` now attach `completion_callbacks` to an already-running operation instead of silently dropping them.
5 changes: 5 additions & 0 deletions temporalio/client/_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1618,6 +1618,11 @@ async def start_nexus_operation(
await cb._to_proto(data_converter) for cb in input.completion_callbacks
],
)
# Under the USE_EXISTING conflict policy, attach callbacks to an
# already-running operation instead of dropping them.
if req.completion_callbacks:
req.on_conflict_options.attach_request_id = True
req.on_conflict_options.attach_completion_callbacks = True

if input.schedule_to_close_timeout is not None:
req.schedule_to_close_timeout.FromTimedelta(input.schedule_to_close_timeout)
Expand Down
55 changes: 54 additions & 1 deletion tests/nexus/test_nexus_handler_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
NexusOperationFailureError,
WorkflowUpdateStage,
)
from temporalio.common import RawValue
from temporalio.common import NexusOperationIDConflictPolicy, RawValue
from temporalio.converter import PayloadCodec
from temporalio.exceptions import ApplicationError, CancelledError, TerminatedError
from temporalio.nexus import WorkflowRunOperationContext, workflow_run_operation
Expand Down Expand Up @@ -792,6 +792,59 @@ async def received_callbacks() -> None:
assert request.result.value.message == "callback test termination"


@pytest.mark.requires_local_server
async def test_use_existing_attaches_completion_callbacks(
client: Client, env: WorkflowEnvironment
):
"""A USE_EXISTING start attaches its callbacks to the already-running operation."""
if env.supports_time_skipping:
pytest.skip(
"Standalone Nexus Operation tests don't work with time-skipping server"
)
operation_handler = StandaloneTestServiceHandler()
completion_handler = EchoOnCompleteHandler()
blocking_input = f"use-existing-{uuid.uuid4()}"
operation_id = str(uuid.uuid4())
async with _callback_workers(
client, env, completion_handler, operation_handler
) as (endpoint_name, completion_task_queue):
nexus_client = client.create_nexus_client(
service=StandaloneTestService, endpoint=endpoint_name
)
handles = [
await nexus_client.start_operation(
StandaloneTestService.blocking_async,
EchoInput(value=blocking_input),
id=operation_id,
id_conflict_policy=NexusOperationIDConflictPolicy.USE_EXISTING,
schedule_to_close_timeout=timedelta(seconds=30),
completion_callbacks=[
nexus.create_completion_callback(
operation=EchoOnCompleteHandler.on_complete,
task_queue=completion_task_queue,
source_context=CallbackContext(label=label),
)
],
)
for label in ("first", "second")
]
assert handles[0].run_id == handles[1].run_id

await asyncio.wait_for(operation_handler.started_blocking.wait(), timeout=10)
await client.get_workflow_handle(
f"blocking_async-{blocking_input}"
).start_update(
BlockingHandlerWorkflow.unblock,
wait_for_stage=WorkflowUpdateStage.COMPLETED,
)
assert await handles[1].result() == EchoOutput(value=blocking_input)

async def received_callbacks() -> None:
assert set(completion_handler.requests) == {"first", "second"}

await assert_eventually(received_callbacks)


@pytest.mark.requires_local_server
async def test_multiple_completion_callbacks_on_operation(
client: Client, env: WorkflowEnvironment
Expand Down
Loading