-
Notifications
You must be signed in to change notification settings - Fork 175
Add Nexus cancellation sample #718
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
Quinn-With-Two-Ns
merged 9 commits into
temporalio:main
from
Quinn-With-Two-Ns:nexus-cancellation
Feb 27, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
159c7b8
Update Nexus Sample for v1.28.0
Quinn-With-Two-Ns f0f6a51
fix typo
Quinn-With-Two-Ns 9818e87
Small updates
Quinn-With-Two-Ns c46211a
Add Nexus Cancellation Sample
Quinn-With-Two-Ns 3107b32
Update cancellation sample
Quinn-With-Two-Ns 59a785c
fix typo
Quinn-With-Two-Ns d2caa2a
run license check
Quinn-With-Two-Ns 40694f0
add log
Quinn-With-Two-Ns 71d795c
run spotless
Quinn-With-Two-Ns 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
36 changes: 36 additions & 0 deletions
36
core/src/main/java/io/temporal/samples/nexuscancellation/README.MD
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,36 @@ | ||
| # Nexus Cancellation | ||
|
|
||
| This sample shows how to cancel a Nexus operation from a caller workflow. | ||
|
|
||
| From more details on Nexus and how to setup to run this samples please see the [Nexus Sample](../nexus/README.MD). | ||
|
|
||
| In separate terminal windows: | ||
|
|
||
| ### Nexus handler worker | ||
|
|
||
| ``` | ||
| ./gradlew -q execute -PmainClass=io.temporal.samples.nexuscancellation.handler.HandlerWorker \ | ||
| --args="-target-host localhost:7233 -namespace my-target-namespace" | ||
| ``` | ||
|
|
||
| ### Nexus caller worker | ||
|
|
||
| ``` | ||
| ./gradlew -q execute -PmainClass=io.temporal.samples.nexuscancellation.caller.CallerWorker \ | ||
| --args="-target-host localhost:7233 -namespace my-caller-namespace" | ||
| ``` | ||
|
|
||
| ### Start caller workflow | ||
|
|
||
| ``` | ||
| ./gradlew -q execute -PmainClass=io.temporal.samples.nexuscancellation.caller.CallerStarter \ | ||
| --args="-target-host localhost:7233 -namespace my-caller-namespace" | ||
| ``` | ||
|
|
||
| ### Output | ||
|
|
||
| which should result in: | ||
| ``` | ||
| INFO i.t.s.n.caller.CallerStarter - Started workflow workflowId: 326732dd-a2b1-4de7-9ddd-dcee4f9f0229 runId: d580499f-79d5-461d-bd49-6248b4e522ae | ||
| INFO i.t.s.n.caller.CallerStarter - Workflow result: Hallo Nexus 👋 | ||
| ``` |
46 changes: 46 additions & 0 deletions
46
core/src/main/java/io/temporal/samples/nexuscancellation/caller/CallerStarter.java
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,46 @@ | ||
| /* | ||
| * Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved | ||
| * | ||
| * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Modifications copyright (C) 2017 Uber Technologies, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). You may not | ||
| * use this file except in compliance with the License. A copy of the License is | ||
| * located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file is distributed on | ||
| * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| * express or implied. See the License for the specific language governing | ||
| * permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package io.temporal.samples.nexuscancellation.caller; | ||
|
|
||
| import io.temporal.api.common.v1.WorkflowExecution; | ||
| import io.temporal.client.WorkflowClient; | ||
| import io.temporal.client.WorkflowOptions; | ||
| import io.temporal.samples.nexus.options.ClientOptions; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public class CallerStarter { | ||
| private static final Logger logger = LoggerFactory.getLogger(CallerStarter.class); | ||
|
|
||
| public static void main(String[] args) { | ||
| WorkflowClient client = ClientOptions.getWorkflowClient(args); | ||
|
|
||
| WorkflowOptions workflowOptions = | ||
| WorkflowOptions.newBuilder().setTaskQueue(CallerWorker.DEFAULT_TASK_QUEUE_NAME).build(); | ||
| HelloCallerWorkflow helloWorkflow = | ||
| client.newWorkflowStub(HelloCallerWorkflow.class, workflowOptions); | ||
| WorkflowExecution execution = WorkflowClient.start(helloWorkflow::hello, "Nexus"); | ||
| logger.info( | ||
| "Started workflow workflowId: {} runId: {}", | ||
| execution.getWorkflowId(), | ||
| execution.getRunId()); | ||
| logger.info("Workflow result: {}", helloWorkflow.hello("Nexus")); | ||
| } | ||
| } |
51 changes: 51 additions & 0 deletions
51
core/src/main/java/io/temporal/samples/nexuscancellation/caller/CallerWorker.java
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,51 @@ | ||
| /* | ||
| * Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved | ||
| * | ||
| * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Modifications copyright (C) 2017 Uber Technologies, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). You may not | ||
| * use this file except in compliance with the License. A copy of the License is | ||
| * located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file is distributed on | ||
| * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| * express or implied. See the License for the specific language governing | ||
| * permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package io.temporal.samples.nexuscancellation.caller; | ||
|
|
||
| import io.temporal.client.WorkflowClient; | ||
| import io.temporal.samples.nexus.options.ClientOptions; | ||
| import io.temporal.samples.nexus.service.NexusService; | ||
| import io.temporal.worker.Worker; | ||
| import io.temporal.worker.WorkerFactory; | ||
| import io.temporal.worker.WorkflowImplementationOptions; | ||
| import io.temporal.workflow.NexusServiceOptions; | ||
| import java.util.Collections; | ||
|
|
||
| public class CallerWorker { | ||
| public static final String DEFAULT_TASK_QUEUE_NAME = "my-caller-workflow-task-queue"; | ||
|
|
||
| public static void main(String[] args) { | ||
| WorkflowClient client = ClientOptions.getWorkflowClient(args); | ||
|
|
||
| WorkerFactory factory = WorkerFactory.newInstance(client); | ||
|
|
||
| Worker worker = factory.newWorker(DEFAULT_TASK_QUEUE_NAME); | ||
| worker.registerWorkflowImplementationTypes( | ||
| WorkflowImplementationOptions.newBuilder() | ||
| .setNexusServiceOptions( | ||
| Collections.singletonMap( | ||
| NexusService.class.getSimpleName(), | ||
| NexusServiceOptions.newBuilder().setEndpoint("my-nexus-endpoint-name").build())) | ||
| .build(), | ||
| HelloCallerWorkflowImpl.class); | ||
|
|
||
| factory.start(); | ||
| } | ||
| } |
29 changes: 29 additions & 0 deletions
29
core/src/main/java/io/temporal/samples/nexuscancellation/caller/HelloCallerWorkflow.java
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,29 @@ | ||
| /* | ||
| * Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved | ||
| * | ||
| * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Modifications copyright (C) 2017 Uber Technologies, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). You may not | ||
| * use this file except in compliance with the License. A copy of the License is | ||
| * located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file is distributed on | ||
| * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| * express or implied. See the License for the specific language governing | ||
| * permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package io.temporal.samples.nexuscancellation.caller; | ||
|
|
||
| import io.temporal.workflow.WorkflowInterface; | ||
| import io.temporal.workflow.WorkflowMethod; | ||
|
|
||
| @WorkflowInterface | ||
| public interface HelloCallerWorkflow { | ||
| @WorkflowMethod | ||
| String hello(String message); | ||
| } |
95 changes: 95 additions & 0 deletions
95
core/src/main/java/io/temporal/samples/nexuscancellation/caller/HelloCallerWorkflowImpl.java
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,95 @@ | ||
| /* | ||
| * Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved | ||
| * | ||
| * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Modifications copyright (C) 2017 Uber Technologies, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). You may not | ||
| * use this file except in compliance with the License. A copy of the License is | ||
| * located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file is distributed on | ||
| * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| * express or implied. See the License for the specific language governing | ||
| * permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package io.temporal.samples.nexuscancellation.caller; | ||
|
|
||
| import static io.temporal.samples.nexus.service.NexusService.Language.*; | ||
|
|
||
| import io.temporal.failure.CanceledFailure; | ||
| import io.temporal.failure.NexusOperationFailure; | ||
| import io.temporal.samples.nexus.service.NexusService; | ||
| import io.temporal.workflow.*; | ||
| import java.time.Duration; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import org.slf4j.Logger; | ||
|
|
||
| public class HelloCallerWorkflowImpl implements HelloCallerWorkflow { | ||
| public static final Logger log = Workflow.getLogger(HelloCallerWorkflowImpl.class); | ||
| private static final NexusService.Language[] languages = | ||
| new NexusService.Language[] {EN, FR, DE, ES, TR}; | ||
| NexusService nexusService = | ||
| Workflow.newNexusServiceStub( | ||
| NexusService.class, | ||
| NexusServiceOptions.newBuilder() | ||
| .setOperationOptions( | ||
| NexusOperationOptions.newBuilder() | ||
| .setScheduleToCloseTimeout(Duration.ofSeconds(10)) | ||
| .build()) | ||
| .build()); | ||
|
|
||
| @Override | ||
| public String hello(String message) { | ||
| List<Promise<NexusService.HelloOutput>> results = new ArrayList<>(languages.length); | ||
|
|
||
| /* | ||
| * Create our CancellationScope. Within this scope we call the nexus operation asynchronously | ||
| * hello method asynchronously for each of our defined languages. | ||
| */ | ||
| CancellationScope scope = | ||
| Workflow.newCancellationScope( | ||
| () -> { | ||
| for (NexusService.Language language : languages) { | ||
| results.add( | ||
| Async.function( | ||
| nexusService::hello, new NexusService.HelloInput(message, language))); | ||
| } | ||
| }); | ||
|
|
||
| /* | ||
| * Execute all nexus operations within the CancellationScope. Note that this execution is | ||
| * non-blocking as the code inside our cancellation scope is also non-blocking. | ||
| */ | ||
| scope.run(); | ||
|
|
||
| // We use "anyOf" here to wait for one of the nexus operation invocations to return | ||
| NexusService.HelloOutput result = Promise.anyOf(results).get(); | ||
|
|
||
| // Trigger cancellation of all uncompleted nexus operations invocations within the cancellation | ||
| // scope | ||
| scope.cancel(); | ||
| // Optionally, wait for all nexus operations to complete | ||
| // | ||
| // Note: Once the workflow completes any pending cancellation requests are dropped by the | ||
| // server. | ||
| for (Promise<NexusService.HelloOutput> promise : results) { | ||
| try { | ||
| promise.get(); | ||
| } catch (NexusOperationFailure e) { | ||
| // If the operation was cancelled, we can ignore the failure | ||
| if (e.getCause() instanceof CanceledFailure) { | ||
| log.info("Operation was cancelled"); | ||
| continue; | ||
| } | ||
| throw e; | ||
| } | ||
| } | ||
| return result.getMessage(); | ||
| } | ||
| } |
42 changes: 42 additions & 0 deletions
42
core/src/main/java/io/temporal/samples/nexuscancellation/handler/HandlerWorker.java
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,42 @@ | ||
| /* | ||
| * Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved | ||
| * | ||
| * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Modifications copyright (C) 2017 Uber Technologies, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). You may not | ||
| * use this file except in compliance with the License. A copy of the License is | ||
| * located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file is distributed on | ||
| * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| * express or implied. See the License for the specific language governing | ||
| * permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package io.temporal.samples.nexuscancellation.handler; | ||
|
|
||
| import io.temporal.client.WorkflowClient; | ||
| import io.temporal.samples.nexus.handler.NexusServiceImpl; | ||
| import io.temporal.samples.nexus.options.ClientOptions; | ||
| import io.temporal.worker.Worker; | ||
| import io.temporal.worker.WorkerFactory; | ||
|
|
||
| public class HandlerWorker { | ||
| public static final String DEFAULT_TASK_QUEUE_NAME = "my-handler-task-queue"; | ||
|
|
||
| public static void main(String[] args) { | ||
| WorkflowClient client = ClientOptions.getWorkflowClient(args); | ||
|
|
||
| WorkerFactory factory = WorkerFactory.newInstance(client); | ||
|
|
||
| Worker worker = factory.newWorker(DEFAULT_TASK_QUEUE_NAME); | ||
| worker.registerWorkflowImplementationTypes(HelloHandlerWorkflowImpl.class); | ||
| worker.registerNexusServiceImplementation(new NexusServiceImpl()); | ||
|
|
||
| factory.start(); | ||
| } | ||
| } |
48 changes: 48 additions & 0 deletions
48
...src/main/java/io/temporal/samples/nexuscancellation/handler/HelloHandlerWorkflowImpl.java
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,48 @@ | ||
| /* | ||
| * Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved | ||
| * | ||
| * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Modifications copyright (C) 2017 Uber Technologies, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). You may not | ||
| * use this file except in compliance with the License. A copy of the License is | ||
| * located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file is distributed on | ||
| * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| * express or implied. See the License for the specific language governing | ||
| * permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package io.temporal.samples.nexuscancellation.handler; | ||
|
|
||
| import io.temporal.failure.ApplicationFailure; | ||
| import io.temporal.samples.nexus.handler.HelloHandlerWorkflow; | ||
| import io.temporal.samples.nexus.service.NexusService; | ||
| import io.temporal.workflow.Workflow; | ||
| import java.time.Duration; | ||
|
|
||
| public class HelloHandlerWorkflowImpl implements HelloHandlerWorkflow { | ||
| @Override | ||
| public NexusService.HelloOutput hello(NexusService.HelloInput input) { | ||
| // Sleep for a random duration to simulate some work | ||
| Workflow.sleep(Duration.ofSeconds(Workflow.newRandom().nextInt(5))); | ||
| switch (input.getLanguage()) { | ||
| case EN: | ||
| return new NexusService.HelloOutput("Hello " + input.getName() + " 👋"); | ||
| case FR: | ||
| return new NexusService.HelloOutput("Bonjour " + input.getName() + " 👋"); | ||
| case DE: | ||
| return new NexusService.HelloOutput("Hallo " + input.getName() + " 👋"); | ||
| case ES: | ||
| return new NexusService.HelloOutput("¡Hola! " + input.getName() + " 👋"); | ||
| case TR: | ||
| return new NexusService.HelloOutput("Merhaba " + input.getName() + " 👋"); | ||
| } | ||
| throw ApplicationFailure.newFailure( | ||
| "Unsupported language: " + input.getLanguage(), "UNSUPPORTED_LANGUAGE"); | ||
| } | ||
| } | ||
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.
Would encourage something a bit more deterministic here so that the same amount of cancellations happen every sample run (e.g. maybe pretend that
ENis always slower or something)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.
Sure, can do that
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.
Let me know what you end up with and I'll update the Go sample. I kinda like that this isn't deterministic and you may get surprising results.