diff --git a/docs/develop/dotnet/nexus/feature-guide.mdx b/docs/develop/dotnet/nexus/feature-guide.mdx index a57d3e0491..d7c95a2432 100644 --- a/docs/develop/dotnet/nexus/feature-guide.mdx +++ b/docs/develop/dotnet/nexus/feature-guide.mdx @@ -21,6 +21,13 @@ New to Nexus? Start with the [Nexus .NET Quickstart](/develop/dotnet/nexus/quick ::: + +:::note + +This Feature Guide includes the new Nexus developer experience: pre-release APIs for the [Temporal Operation Handler](/nexus/temporal-operation-handler) and [Nexus Standalone Activity](/nexus/standalone-activity). These APIs are experimental and subject to change. + +::: + This page shows how to do the following: - [Run a development Temporal Service with Nexus enabled](#run-the-temporal-nexus-development-server) @@ -42,8 +49,10 @@ This documentation uses source code derived from the [.NET Nexus sample](https:/ Prerequisites: -- [Install the latest Temporal CLI](https://learn.temporal.io/getting_started/dotnet/dev_environment/#set-up-a-local-temporal-service-for-development-with-temporal-cli) (v1.3.0 or higher recommended) -- [Install the latest Temporal .NET SDK](https://learn.temporal.io/getting_started/dotnet/dev_environment/#install-the-temporal-net-sdk) (v1.9.0 or higher) +- [Install the latest Temporal CLI](https://learn.temporal.io/getting_started/dotnet/dev_environment/#set-up-a-local-temporal-service-for-development-with-temporal-cli) + (v1.3.0 or higher recommended) +- [Install the latest Temporal .NET SDK](https://learn.temporal.io/getting_started/dotnet/dev_environment/#install-the-temporal-net-sdk) + (v1.18.0 or higher recommended) The first step in working with Temporal Nexus involves starting a Temporal server with Nexus enabled. @@ -86,44 +95,21 @@ Defining a clear contract for the Nexus Service is crucial for smooth communicat In this example, there is a service package that describes the Service and Operation names along with input/output types for caller Workflows to use the Nexus Endpoint. -Each [Temporal SDK includes and uses a default Data Converter](/dataconversion). -The default data converter encodes payloads in the following order: Null, Byte array, Protobuf JSON, and JSON. -In a polyglot environment, that is where more than one language and SDK is being used to develop a Temporal solution, Protobuf and JSON are common choices. -This example uses .NET classes serialized into JSON. - -[NexusSimple/IHelloService.cs](https://github.com/temporalio/samples-dotnet/blob/main/src/NexusSimple/IHelloService.cs) -```csharp -using NexusRpc; - -[NexusService] -public interface IHelloService -{ - static readonly string EndpointName = "nexus-simple-endpoint"; - - [NexusOperation] - EchoOutput Echo(EchoInput input); - - [NexusOperation] - HelloOutput SayHello(HelloInput input); - - public record EchoInput(string Message); - - public record EchoOutput(string Message); +You can hand-write that package, but the preferred way is to generate it with the [Nexus Code Generator](https://github.com/temporalio/nexgen). +You write the contract once as a JSON or YAML definition file and run `nexgen` against it, and it emits the typed models, +runtime validators, and the Service definition itself. - public record HelloInput(string Name, HelloLanguage Language); +This is what makes a Nexus Service polyglot. Both sides generate from the same definition file: the handler implements +the Service, the caller invokes its Operations, and neither hand-writes a request or response type. A Python handler +and a Go caller share no code, but they both run off that same service contract - so they interoperate with no +coordination between the teams beyond the contract itself. - public record HelloOutput(string Message); - - public enum HelloLanguage - { - En, - Fr, - De, - Es, - Tr, - } -} -``` +The generated validators check every payload against the contract, when a value is parsed off the wire and again when +it is serialized onto it, so bad data is rejected at the boundary rather than reaching your Workflow or Activity. A value validates +identically in every language, which is what lets a caller and a handler written in different languages trust the same +contract. See the [`chat.nexusrpc.yaml`](https://github.com/temporalio/nexgen/blob/main/samples/schemas/chat.nexusrpc.yaml) +sample contract and the [Definition files](https://github.com/temporalio/nexgen#definition-files) section of the +`nexgen` README for the file format. ## Develop a Nexus Service and Operation handlers {/* #develop-nexus-service-operation-handlers */} @@ -134,64 +120,84 @@ Use a synchronous Nexus Operation only when its complete execution path is highl Use an asynchronous Nexus Operation when latency or availability is uncertain, the work might exceed the handler deadline, or execution depends on a potentially unreliable service or database. Handlers should be reliable since the [circuit breaker](/nexus/operations#circuit-breaking) trips after 5 consecutive retryable errors, blocking all Operations from the caller to that Endpoint. -The `Temporalio.Nexus` namespace has utilities to help create Nexus Operations: +Every Operation is written with a [Temporal Operation Handler](/nexus/temporal-operation-handler). Mark a method +`[TemporalOperation]`; that method is the Operation handler and receives three things: a +`TemporalOperationStartContext`, an `ITemporalNexusClient`, and the Operation input. The Operation the method handles is +matched by method name to the corresponding `[NexusOperation]` method on the Service interface. What you do with the +Client decides what backs the Operation: -- `NexusOperationExecutionContext.Current.TemporalClient` \- Get the Temporal Client that the Worker was initialized with for synchronous handlers backed by - Temporal primitives such as Signals and Queries -- `WorkflowRunOperationHandler.FromHandleFactory` \- Run a Workflow as an asynchronous Nexus Operation +- **Synchronous.** Return `TemporalOperationResult.SyncResult(...)` and the Operation completes during the handler + call. The caller has its result as soon as the call returns. +- **Asynchronous.** Call `StartWorkflowAsync`, `StartActivityAsync`, or `StartWorkflowUpdateAsync` on the Client. The + handler returns as soon as that Execution has started, and the Operation stays open until the Execution finishes, + which may be days later. Its result is delivered to the caller through the Nexus completion callback. This is what + lets an Operation outlive the [Nexus request timeout](/cloud/limits#nexus-operation-request-timeout). -This example starts with a sync Operation handler example using the `OperationHandler.Sync` method, and then shows how to create an async Operation handler that uses `WorkflowRunOperationHandler.FromHandleFactory` to start a handler Workflow from a Nexus Operation. +A handler can perform any number of synchronous side effects, such as sending a Signal, but at most one asynchronous +backing per invocation. ### Develop a Synchronous Nexus Operation handler -The `OperationHandler.Sync` method is for exposing simple RPC handlers. -Use `NexusOperationExecutionContext.Current.TemporalClient` to get the Temporal Client for signaling, querying, and listing Workflows. -Implementations can also make other calls, but handlers should be reliable to avoid tripping the [circuit breaker](/nexus/operations#circuit-breaking). +Return a synchronous result when the Operation can answer immediately. The handler computes the answer and returns it, +and the Operation completes during the call. + +Handlers should be reliable to avoid tripping the [circuit breaker](/nexus/operations#circuit-breaking), and the whole +call has to finish inside the [Nexus request timeout](/cloud/limits#nexus-operation-request-timeout). -[NexusSimple/Handler/HelloService.cs](https://github.com/temporalio/samples-dotnet/blob/main/src/NexusSimple/Handler/HelloService.cs) ```csharp using NexusRpc.Handlers; +using Temporalio.Nexus; [NexusServiceHandler(typeof(IHelloService))] public class HelloService { - [NexusOperationHandler] - public IOperationHandler Echo() => - // This Nexus service operation is a simple sync handler - OperationHandler.Sync( - (ctx, input) => new(input.Message)); - - // ... + [TemporalOperation] + public Task> Echo( + TemporalOperationStartContext ctx, + ITemporalNexusClient client, + IHelloService.EchoInput input) => + Task.FromResult(TemporalOperationResult.SyncResult( + new(input.Message))); } ``` ### Use the Temporal Client for Signals, Queries, and Updates -A common pattern is to use the Temporal Client from within a sync handler to Signal, Query, or Update a Workflow. -You can also use Signal-With-Start or Update-With-Start to ensure the Workflow is started and send it a Signal or Update. -All calls must complete within the [Nexus request timeout](/cloud/limits#nexus-operation-request-timeout). Updates should be short-lived to stay within this deadline. +A common pattern is to reach a Workflow that is already running. Query it or Signal it from a synchronous Operation, or +use Signal-With-Start to make sure the Workflow exists before the Signal arrives. Those calls complete during the +handler call, so they have to finish inside the +[Nexus request timeout](/cloud/limits#nexus-operation-request-timeout). + +Updates are the exception. Do not wait for one inside the handler. Start it with `StartWorkflowUpdateAsync` and it backs +the Operation. The handler returns straight away, and the Operation completes when the Update does, however long it +takes. -The [nexus_messaging](https://github.com/temporalio/samples-dotnet/tree/main/src/NexusMessaging) sample shows how to create a Nexus Service that uses synchronous operations to send Updates and Queries: +The [NexusMessaging](https://github.com/temporalio/samples-dotnet/tree/main/src/NexusMessaging) +sample shows a Nexus Service that Queries and Signals a running Workflow from synchronous Operations, and backs an +Operation with a Workflow Update. -Use `NexusOperationExecutionContext`, like below, to get the Client that the Worker was initialized with. In this example, the Workflow Id is derived from the client Id using the `WorkflowIdForUser` method. This converts a given client Id (in this case, the client is passing in a user Id) to generate a Workflow Id from it. -This way the client only needs the identifier it cares about. +The Client your handler receives is not an ordinary Temporal Client. It propagates +[bidirectional links](/nexus/execution-debugging#bi-directional-linking) and request Ids on every call, so the +caller-side and handler-side Executions are connected in the UI without wiring anything. Reach the Workflow Client +through `client.TemporalClient` rather than constructing your own. -[NexusMessaging/CallerPattern/Handler/NexusGreetingService.cs](https://github.com/temporalio/samples-dotnet/tree/main/src/NexusMessaging/CallerPattern/Handler/NexusGreetingService.cs) +In this example the Workflow Id is derived from an identifier carried in the Operation input, so the caller only needs +the identifier it cares about: ```csharp private static string WorkflowIdForUser(string userId) => $"GreetingWorkflow_for_{userId}"; -[NexusOperationHandler] -public IOperationHandler GetLanguages() => - OperationHandler.Sync( - async (ctx, input) => - { - // Access the Temporal client from the Nexus operation context - var client = NexusOperationExecutionContext.Current.TemporalClient; - var handle = client.GetWorkflowHandle(WorkflowIdForUser(input.UserId)); - return await handle.QueryAsync(wf => wf.QueryLanguages(input.IncludeUnsupported)); - }); - ... +[TemporalOperation] +public async Task> Approve( + TemporalOperationStartContext ctx, + ITemporalNexusClient client, + INexusGreetingService.ApproveInput input) +{ + var handle = client.TemporalClient.GetWorkflowHandle( + WorkflowIdForUser(input.UserId)); + await handle.SignalAsync(wf => wf.ApproveAsync(input)); + return TemporalOperationResult.SyncResult(new()); +} ``` There are two examples of messaging through Nexus in the sample code: the [caller pattern](https://github.com/temporalio/samples-dotnet/tree/main/src/NexusMessaging/CallerPattern) and the [on-demand pattern](https://github.com/temporalio/samples-dotnet/tree/main/src/NexusMessaging/OnDemandPattern). @@ -199,31 +205,23 @@ The caller pattern shows how to send messages to an existing Workflow, while the ### Develop an Asynchronous Nexus Operation handler to start a Workflow -Use the `WorkflowRunOperationHandler.FromHandleFactory` method, which is the easiest way to expose a Workflow as an operation. +Call `StartWorkflowAsync` on the Client. The Operation completes when the Workflow returns, and the Workflow's return +value is delivered to the caller as the Operation's result. -[NexusSimple/Handler/HelloService.cs](https://github.com/temporalio/samples-dotnet/blob/main/src/NexusSimple/Handler/HelloService.cs) ```csharp - -using NexusRpc.Handlers; -using Temporalio.Nexus; - [NexusServiceHandler(typeof(IHelloService))] public class HelloService { - // ... - - [NexusOperationHandler] - public IOperationHandler SayHello() => - // This Nexus service operation is backed by a workflow run - WorkflowRunOperationHandler.FromHandleFactory( - (WorkflowRunOperationContext context, IHelloService.HelloInput input) => - context.StartWorkflowAsync( - (HelloHandlerWorkflow wf) => wf.RunAsync(input), - // Workflow IDs should typically be business meaningful IDs and are used to - // dedupe workflow starts. For this example, we're using the request ID - // allocated by Temporal when the caller workflow schedules the operation, - // this ID is guaranteed to be stable across retries of this operation. - new() { Id = context.HandlerContext.RequestId })); + [TemporalOperation] + public Task> SayHello( + TemporalOperationStartContext ctx, + ITemporalNexusClient client, + IHelloService.HelloInput input) => + client.StartWorkflowAsync( + (HelloHandlerWorkflow wf) => wf.RunAsync(input), + // Workflow IDs should typically be business meaningful IDs and are used to dedupe + // workflow starts. Task queue defaults to the operation's task queue when omitted. + new() { Id = $"hello-{input.Name}-{input.Language}" }); } ``` @@ -237,27 +235,13 @@ Workflow IDs should typically be business-meaningful IDs and are used to dedupe #### Map a Nexus Operation input to multiple Workflow arguments -A Nexus Operation can only take one input parameter. If you want a Nexus Operation to start a Workflow that takes multiple arguments, simply pass in different arguments using `RunAsync`. +A Nexus Operation can only take one input parameter. To start a Workflow that takes several, pass them as separate +arguments in the `RunAsync` lambda: -[NexusMultiArg/Handler/HelloService.cs](https://github.com/temporalio/samples-dotnet/blob/main/src/NexusMultiArg/Handler/HelloService.cs) ```csharp -[NexusServiceHandler(typeof(IHelloService))] -public class HelloService -{ - [NexusOperationHandler] - public IOperationHandler SayHello() => - // This Nexus service operation is backed by a workflow run. For this sample, we are - // altering the parameters to the workflow (in this case expanding to two parameters). - WorkflowRunOperationHandler.FromHandleFactory( - (WorkflowRunOperationContext context, IHelloService.HelloInput input) => - context.StartWorkflowAsync( - (HelloHandlerWorkflow wf) => wf.RunAsync(input.Language, input.Name), - // Workflow IDs should typically be business meaningful IDs and are used to - // dedupe workflow starts. For this example, we're using the request ID - // allocated by Temporal when the caller workflow schedules the operation, - // this ID is guaranteed to be stable across retries of this operation. - new() { Id = context.HandlerContext.RequestId })); -} +client.StartWorkflowAsync( + (HelloHandlerWorkflow wf) => wf.RunAsync(input.Language, input.Name), + new() { Id = $"hello-{input.Name}-{input.Language}" }); ``` ### Register a Nexus Service in a Worker @@ -265,6 +249,7 @@ public class HelloService After developing an asynchronous Nexus Operation handler to start a Workflow, the next step is to register a Nexus Service in a Worker. [NexusSimple/Program.cs](https://github.com/temporalio/samples-dotnet/blob/main/src/NexusSimple/Program.cs) + ```csharp async Task RunHandlerWorkerAsync() { @@ -286,55 +271,15 @@ async Task RunHandlerWorkerAsync() } ``` -### Use dependency injection with a Nexus Service handler {/* #dependency-injection */} - -Nexus Service handlers support dependency injection through the [Temporalio.Extensions.Hosting](https://github.com/temporalio/sdk-dotnet/tree/main/src/Temporalio.Extensions.Hosting) generic-host Worker. -Register the handler on the Worker with `AddScopedNexusService`, and the container injects the handler's constructor dependencies. -Use `AddSingletonNexusService` or `AddTransientNexusService` for singleton or transient lifetimes instead, mirroring `AddScopedActivities` / `AddSingletonActivities` / `AddTransientActivities`. - -For a complete, runnable example, see the [NexusDependencyInjection sample](https://github.com/temporalio/samples-dotnet/tree/main/src/NexusDependencyInjection). - -[NexusDependencyInjection/Program.cs](https://github.com/temporalio/samples-dotnet/blob/main/src/NexusDependencyInjection/Program.cs) -```csharp -IHost host = Host.CreateDefaultBuilder(args) - .ConfigureServices(ctx => - ctx. - // Add the dependency that will be injected into the Nexus Service handler - AddScoped(). - // Add the worker - AddHostedTemporalWorker(handlerTaskQueue). - ConfigureOptions(options => options.ClientOptions = LoadConnectOptions()). - // Add the Nexus Service handler at the scoped level - AddScopedNexusService()) - .Build(); -await host.RunAsync(); -``` - -The handler receives its dependencies through its constructor. -The container creates a new scoped handler instance and its scoped dependencies for each Operation invocation; it does not cache them between invocations: - -[NexusDependencyInjection/Handler/GreetingServiceHandler.cs](https://github.com/temporalio/samples-dotnet/blob/main/src/NexusDependencyInjection/Handler/GreetingServiceHandler.cs) -```csharp -[NexusServiceHandler(typeof(IGreetingService))] -public class GreetingServiceHandler -{ - private readonly IGreetingClient greetingClient; - - // The dependency is injected by the container - public GreetingServiceHandler(IGreetingClient greetingClient) => this.greetingClient = greetingClient; - - [NexusOperationHandler] - public IOperationHandler SayHello() => - OperationHandler.Sync( - (ctx, input) => greetingClient.GetGreetingAsync(input.Name)); -} -``` +Nexus Service handlers also support dependency injection through the generic-host Worker. See [NexusDependencyInjection sample](https://github.com/temporalio/samples-dotnet/tree/main/src/NexusDependencyInjection). ## Develop a caller Workflow that uses the Nexus Service {/* #develop-caller-workflow-nexus-service */} -Import the Service API package that has the necessary service and operation names and input/output types to execute a Nexus Operation from the caller Workflow: +Import the Service interface that has the necessary Operation names and input/output types to execute a Nexus Operation +from the caller Workflow: [NexusSimple/Caller/EchoCallerWorkflow.workflow.cs](https://github.com/temporalio/samples-dotnet/blob/main/src/NexusSimple/Caller/EchoCallerWorkflow.workflow.cs) + ```csharp using Temporalio.Workflows; @@ -352,6 +297,7 @@ public class EchoCallerWorkflow ``` [NexusSimple/Caller/HelloCallerWorkflow.workflow.cs](https://github.com/temporalio/samples-dotnet/blob/main/src/NexusSimple/Caller/HelloCallerWorkflow.workflow.cs) + ```csharp using Temporalio.Workflows; @@ -368,56 +314,12 @@ public class HelloCallerWorkflow } ``` -### Set Nexus Operation timeouts - -Nexus Operations support [three types of timeouts](/nexus/operations#timeouts) that control how long the caller is willing to wait at different stages of the Operation lifecycle. -Set these timeouts in `NexusWorkflowOperationOptions` when calling `ExecuteNexusOperationAsync`. - -#### Schedule-to-Close timeout - -The [Schedule-to-Close timeout](/nexus/operations#schedule-to-close-timeout) limits the total duration of the Operation from when it is scheduled to when it completes. -The Nexus Machinery automatically retries failed requests until this timeout is exceeded. - -```csharp -var output = await Workflow.CreateNexusWorkflowClient(IHelloService.EndpointName). - ExecuteNexusOperationAsync(svc => svc.SayHello(new(name, language)), new NexusWorkflowOperationOptions - { - ScheduleToCloseTimeout = TimeSpan.FromMinutes(10), - }); -``` - -#### Schedule-to-Start timeout - -The [Schedule-to-Start timeout](/nexus/operations#schedule-to-start-timeout) limits how long the caller will wait for the Operation to be started by the handler. -If not set, no Schedule-to-Start timeout is enforced. - -```csharp -var output = await Workflow.CreateNexusWorkflowClient(IHelloService.EndpointName). - ExecuteNexusOperationAsync(svc => svc.SayHello(new(name, language)), new NexusWorkflowOperationOptions - { - ScheduleToStartTimeout = TimeSpan.FromMinutes(2), - }); -``` - -#### Start-to-Close timeout - -The [Start-to-Close timeout](/nexus/operations#start-to-close-timeout) limits how long the caller will wait for an asynchronous Operation to complete after it has been started. -This timeout only applies to asynchronous Operations. -If not set, no Start-to-Close timeout is enforced. - -```csharp -var output = await Workflow.CreateNexusWorkflowClient(IHelloService.EndpointName). - ExecuteNexusOperationAsync(svc => svc.SayHello(new(name, language)), new NexusWorkflowOperationOptions - { - StartToCloseTimeout = TimeSpan.FromMinutes(5), - }); -``` - ### Register the caller Workflow in a Worker After developing the caller Workflow, the next step is to register it with a Worker. [NexusSimple/Program.cs](https://github.com/temporalio/samples-dotnet/blob/main/src/NexusSimple/Program.cs) + ```csharp async Task RunCallerWorkerAsync() { @@ -444,6 +346,7 @@ async Task RunCallerWorkerAsync() To initiate the caller Workflow, a starter program is used. [NexusSimple/Program.cs](https://github.com/temporalio/samples-dotnet/blob/main/src/NexusSimple/Program.cs) + ```csharp async Task ExecuteCallerWorkflowAsync() { @@ -507,9 +410,11 @@ When a Nexus operation is started, the caller can specify different cancellation The default is `WaitCancellationCompleted`. Users can set a different option for `CancellationType` in `NexusWorkflowOperationOptions` when starting an operation. -Once the caller Workflow completes, the caller's Nexus Machinery will not make any further attempts to cancel operations that are still running. -It's okay to leave operations running in some use cases. -To ensure cancellations are delivered, wait for all pending operations to finish before exiting the Workflow. +Once the caller Workflow completes, the caller's Nexus Machinery stops attempting to cancel operations that have not yet +been canceled, letting them run to completion. + +It's okay to leave operations running in some use cases. To ensure cancellations are delivered, wait for all pending +operations to deliver their cancellation requests before exiting the Workflow. See the [Nexus cancellation sample](https://github.com/temporalio/samples-dotnet/tree/main/src/NexusCancellation) for reference. diff --git a/docs/develop/go/nexus/feature-guide.mdx b/docs/develop/go/nexus/feature-guide.mdx index 66365ed25a..425cc0a98d 100644 --- a/docs/develop/go/nexus/feature-guide.mdx +++ b/docs/develop/go/nexus/feature-guide.mdx @@ -23,6 +23,13 @@ New to Nexus? Start with the [Nexus Go Quickstart](/develop/go/nexus/quickstart) ::: + +:::note + +This Feature Guide includes the new Nexus developer experience: pre-release APIs for the [Temporal Operation Handler](/nexus/temporal-operation-handler), [Nexus Standalone Activity](/nexus/standalone-activity), and (where supported) the [Nexus Code Generator](/nexus/code-generator). These APIs are experimental and subject to change. + +::: + This page shows how to do the following: - [Run a development Temporal Service with Nexus enabled](#run-the-temporal-nexus-development-server) @@ -45,8 +52,7 @@ This documentation uses source code derived from the [Go Nexus sample](https://g Prerequisites: - [Install the latest Temporal CLI](/develop/run-a-development-server) (v1.3.0 or higher recommended) -- [Install the latest Temporal Go SDK](/develop/go/set-up-your-local-go) - (v1.33.0 or higher recommended) +- [Install the latest Temporal Go SDK](/develop/go/set-up-your-local-go) (v1.48.0 or higher recommended) The first step in working with Temporal Nexus involves starting a Temporal server with Nexus enabled. @@ -89,28 +95,21 @@ Defining a clear contract for the Nexus Service is crucial for smooth communicat In this example, there is a service package that describes the Service and Operation names along with input/output types for caller Workflows to use the Nexus Endpoint. -Each [Temporal SDK includes and uses a default Data Converter](/dataconversion). -The default data converter encodes payloads in the following order: Null, Byte array, Protobuf JSON, and JSON. -In a polyglot environment, that is where more than one language and SDK is being used to develop a Temporal solution, Protobuf and JSON are common choices. -This example uses native Go types. +You can hand-write that package, but the preferred way is to generate it with the [Nexus Code Generator](https://github.com/temporalio/nexgen). +You write the contract once as a JSON or YAML definition file and run `nexgen` against it, and it emits the typed models, +runtime validators, and the Service definition itself. - -[nexus/service/api.go](https://github.com/temporalio/samples-go/blob/main/nexus/service/api.go) -```go -// ... -const HelloServiceName = "my-hello-service" +This is what makes a Nexus Service polyglot. Both sides generate from the same definition file: the handler implements +the Service, the caller invokes its Operations, and neither hand-writes a request or response type. A Python handler +and a Go caller share no code, but they both run off that same service contract - so they interoperate with no +coordination between the teams beyond the contract itself. -// Echo operation -const EchoOperationName = "echo" - -type EchoInput struct { - Message string -} - -type EchoOutput EchoInput - -``` - +The generated validators check every payload against the contract, when a value is parsed off the wire and again when +it is serialized onto it, so bad data is rejected at the boundary rather than reaching your Workflow or Activity. A value validates +identically in every language, which is what lets a caller and a handler written in different languages trust the same +contract. See the [`chat.nexusrpc.yaml`](https://github.com/temporalio/nexgen/blob/main/samples/schemas/chat.nexusrpc.yaml) +sample contract and the [Definition files](https://github.com/temporalio/nexgen#definition-files) section of the +`nexgen` README for the file format. ## Develop a Nexus Service and Operation handlers {/* #develop-nexus-service-operation-handlers */} @@ -121,73 +120,84 @@ Use a synchronous Nexus Operation only when its complete execution path is highl Use an asynchronous Nexus Operation when latency or availability is uncertain, the work might exceed the handler deadline, or execution depends on a potentially unreliable service or database. Handlers should be reliable since the [circuit breaker](/nexus/operations#circuit-breaking) trips after 5 consecutive retryable errors, blocking all Operations from the caller to that Endpoint. -The `temporalnexus` package has builders to create Nexus Operations and other helpers for authoring Operation handlers: +Every Operation is written with a [Temporal Operation Handler](/nexus/temporal-operation-handler). `temporalnexus.MustNewTemporalOperation(...)` takes a `Start` callback that receives three things: a context, a `NexusClient`, and the +Operation input. What you do with the Client decides what backs the Operation: -- `NewWorkflowRunOperation` \- Run a Workflow as an asynchronous Nexus Operation -- `GetClient` \- Get the Temporal Client that the Worker was initialized with for synchronous handlers backed by - Temporal primitives such as Signals and Queries +- **Synchronous.** Return `temporalnexus.NewSyncResult(...)` and the Operation completes during the handler call. The + caller has its result as soon as the call returns. +- **Asynchronous.** Call `temporalnexus.StartWorkflow`, `temporalnexus.StartActivity`, or + `temporalnexus.StartUpdateWorkflow` with the Client. The handler returns as soon as that Execution has started, and + the Operation stays open until the Execution finishes, which may be days later. Its result is delivered to the caller + through the Nexus completion callback. This is what lets an Operation outlive the + [Nexus request timeout](/cloud/limits#nexus-operation-request-timeout). -This tutorial starts with a sync Operation handler example using the `nexus.NewSyncOperation` method, and then shows how to create an async Operation handler that uses `NewWorkflowRunOperation` to start a handler Workflow from a Nexus Operation. +A handler can perform any number of synchronous side effects, such as sending a Signal, but at most one asynchronous +backing per invocation. ### Develop a Synchronous Nexus Operation handler -The `nexus.NewSyncOperation` builder function is for exposing simple RPC handlers. -Use `temporalnexus.GetClient(ctx)` to get the Temporal Client for signaling, querying, and listing Workflows. -Implementations can also make other calls, but handlers should be reliable to avoid tripping the [circuit breaker](/nexus/operations#circuit-breaking). - - -[nexus/handler/app.go](https://github.com/temporalio/samples-go/blob/main/nexus/handler/app.go) -```go -// ... - -import ( - "context" - "fmt" - - "github.com/nexus-rpc/sdk-go/nexus" - - "go.temporal.io/sdk/client" - "go.temporal.io/sdk/temporalnexus" - "go.temporal.io/sdk/workflow" - - "github.com/temporalio/samples-go/nexus/service" -) +Return a synchronous result when the Operation can answer immediately. The handler computes the answer and returns it, +and the Operation completes during the call. -// NewSyncOperation is a meant for exposing simple RPC handlers. -var EchoOperation = nexus.NewSyncOperation(service.EchoOperationName, func(ctx context.Context, input service.EchoInput, options nexus.StartOperationOptions) (service.EchoOutput, error) { - // Use temporalnexus.GetClient to get the client that the worker was initialized with to perform client calls - // such as signaling, querying, and listing workflows. Implementations are free to make arbitrary calls to other - // services or databases, or perform simple computations such as this one. - return service.EchoOutput(input), nil -}) +Handlers should be reliable to avoid tripping the [circuit breaker](/nexus/operations#circuit-breaking), and the whole +call has to finish inside the [Nexus request timeout](/cloud/limits#nexus-operation-request-timeout). +```go +var EchoOperation = temporalnexus.MustNewTemporalOperation( + temporalnexus.TemporalOperationOptions[service.EchoInput, service.EchoOutput]{ + Name: service.EchoOperationName, + Start: func( + ctx context.Context, + nc temporalnexus.NexusClient, + input service.EchoInput, + options temporalnexus.StartTemporalOperationOptions, + ) (temporalnexus.TemporalOperationResult[service.EchoOutput], error) { + return temporalnexus.NewSyncResult(service.EchoOutput(input)), nil + }, + }) ``` - ### Use the Temporal Client for Signals, Queries, and Updates -A common pattern is to use the Temporal Client from within a sync handler to Signal, Query, or Update a Workflow. -You can also use Signal-With-Start or Update-With-Start to ensure the Workflow is started and send it a Signal or Update. -All calls must complete within the [Nexus request timeout](/cloud/limits#nexus-operation-request-timeout). -The ctx provided to the handler is automatically set with this deadline, so passing it directly to Temporal Client calls will correctly propagate the timeout. -Updates should be short-lived to stay within this deadline. +A common pattern is to reach a Workflow that is already running. Query it or Signal it from a synchronous Operation, or +use Signal-With-Start to make sure the Workflow exists before the Signal arrives. Those calls complete during the +handler call, so they have to finish inside the +[Nexus request timeout](/cloud/limits#nexus-operation-request-timeout). -The [nexus_messaging](https://github.com/temporalio/samples-go/tree/main/nexus-messaging) sample shows how to create a Nexus Service that uses synchronous operations to send Updates and Queries. +Updates are the exception. Do not wait for one inside the handler. Start it with `temporalnexus.StartUpdateWorkflow` and +it backs the Operation. The handler returns straight away, and the Operation completes when the Update does, however +long it takes. -Use the Nexus library, as shown below, to get the Client that the Worker was initialized with. In this example, the Workflow Id is derived from the client Id, with the `GetWorkflowID` method. This converts a given client Id (in this case, the client is passing in a user Id) to generate a Workflow Id from it. -This way the client only needs the identifier it cares about. +The [nexus-messaging](https://github.com/temporalio/samples-go/tree/main/nexus-messaging) +sample shows a Nexus Service that Queries and Signals a running Workflow from synchronous Operations, and backs an +Operation with a Workflow Update. -[nexus-messaging/callerpattern/handler/app.go](https://github.com/temporalio/samples-go/blob/main/nexus-messaging/callerpattern/handler/app.go) +The Client your handler receives is not an ordinary Temporal Client. It propagates +[bidirectional links](/nexus/execution-debugging#bi-directional-linking) and request Ids on every call, so the +caller-side and handler-side Executions are connected in the UI without wiring anything. Reach the Workflow Client +through `nc.GetWorkflowClient()` rather than constructing your own. -```go -func GetWorkflowID(userID string) string { - return WorkflowIDPrefix + userID -} +In this example the Workflow Id is derived from an identifier carried in the Operation input, so the caller only needs +the identifier it cares about: -var GetLanguagesOperation = nexus.NewSyncOperation(service.GetLanguagesOperationName, func(ctx context.Context, input service.GetLanguagesInput, options nexus.StartOperationOptions) (service.GetLanguagesOutput, error) { - c := temporalnexus.GetClient(ctx) - workflowID := GetWorkflowID(input.UserID) - ... +```go +var ApproveOperation = temporalnexus.MustNewTemporalOperation( + temporalnexus.TemporalOperationOptions[service.ApproveInput, service.ApproveOutput]{ + Name: service.ApproveOperationName, + Start: func( + ctx context.Context, + nc temporalnexus.NexusClient, + input service.ApproveInput, + options temporalnexus.StartTemporalOperationOptions, + ) (temporalnexus.TemporalOperationResult[service.ApproveOutput], error) { + err := nc.GetWorkflowClient().SignalWorkflow( + ctx, GetWorkflowID(input.UserID), "", service.ApproveSignalName, input) + if err != nil { + return temporalnexus.TemporalOperationResult[service.ApproveOutput]{}, err + } + return temporalnexus.NewSyncResult(service.ApproveOutput{}), nil + }, + }) ``` There are two examples of messaging through Nexus in the sample code, [caller pattern](https://github.com/temporalio/samples-go/tree/main/nexus-messaging/callerpattern/) and [on-demand pattern](https://github.com/temporalio/samples-go/tree/main/nexus-messaging/ondemandpattern/). @@ -195,28 +205,29 @@ The caller pattern shows how to send messages to an existing Workflow, while the ### Develop an Asynchronous Nexus Operation handler to start a Workflow -Use the `NewWorkflowRunOperation` constructor, which is the easiest way to expose a Workflow as an operation. -See alternatives [here](https://pkg.go.dev/go.temporal.io/sdk/temporalnexus). +Call `temporalnexus.StartWorkflow` with the Client. The Operation completes when the Workflow returns, and the +Workflow's return value is delivered to the caller as the Operation's result. - -[nexus/handler/app.go](https://github.com/temporalio/samples-go/blob/main/nexus/handler/app.go) ```go -// ... -var HelloOperation = temporalnexus.NewWorkflowRunOperation(service.HelloOperationName, HelloHandlerWorkflow, func(ctx context.Context, input service.HelloInput, options nexus.StartOperationOptions) (client.StartWorkflowOptions, error) { - return client.StartWorkflowOptions{ - // Workflow IDs should typically be business meaningful IDs and are used to dedupe workflow starts. - // For this example, use a business ID derived from the greeting input so repeated operations - // for the same name and language resolve to the same workflow. - ID: service.HelloWorkflowID(input), - // Task queue defaults to the task queue this operation is handled on. - }, nil -}) - +var HelloOperation = temporalnexus.MustNewTemporalOperation( + temporalnexus.TemporalOperationOptions[service.HelloInput, service.HelloOutput]{ + Name: service.HelloOperationName, + Start: func( + ctx context.Context, + nc temporalnexus.NexusClient, + input service.HelloInput, + options temporalnexus.StartTemporalOperationOptions, + ) (temporalnexus.TemporalOperationResult[service.HelloOutput], error) { + return temporalnexus.StartWorkflow(ctx, nc, client.StartWorkflowOptions{ + ID: service.HelloWorkflowID(input), + // Task queue defaults to the task queue this operation is handled on. + }, HelloHandlerWorkflow, input) + }, + }) ``` - -Workflow IDs should typically be business-meaningful IDs and are used to dedupe Workflow starts. -For the `HelloOperation`, `input.ID` is passed as part of the Nexus Service contract. +Workflow IDs should typically be business-meaningful IDs and are used to dedupe Workflow starts. In general, the ID +should be passed in the Operation input as part of the Nexus Service contract. :::tip RESOURCES @@ -226,33 +237,15 @@ For the `HelloOperation`, `input.ID` is passed as part of the Nexus Service cont #### Map a Nexus Operation input to multiple Workflow arguments -A Nexus Operation can only take one input parameter. If you want a Nexus Operation to start a Workflow that takes multiple arguments use -`NewWorkflowRunOperationWithOptions` or `MustNewWorkflowRunOperationWithOptions`. +A Nexus Operation can only take one input parameter. `temporalnexus.StartWorkflow` is typed for a Workflow that takes a +single argument, so to start a Workflow that takes several, use `temporalnexus.StartUntypedWorkflow` and pass the +arguments after the Workflow function: - -[nexus-multiple-arguments/handler/app.go](https://github.com/temporalio/samples-go/blob/main/nexus-multiple-arguments/handler/app.go) ```go -var HelloOperation = temporalnexus.MustNewWorkflowRunOperationWithOptions(temporalnexus.WorkflowRunOperationOptions[service.HelloInput, service.HelloOutput]{ - Name: service.HelloOperationName, - Handler: func(ctx context.Context, input service.HelloInput, options nexus.StartOperationOptions) (temporalnexus.WorkflowHandle[service.HelloOutput], error) { - return temporalnexus.ExecuteUntypedWorkflow[service.HelloOutput]( - ctx, - options, - client.StartWorkflowOptions{ - // Workflow IDs should typically be business meaningful IDs and are used to dedupe workflow starts. - // For this example, use a business ID derived from the greeting input so repeated operations - // for the same name and language resolve to the same workflow. - ID: service.HelloWorkflowID(input), - }, - HelloHandlerWorkflow, - input.Name, - input.Language, - ) - }, -}) - +return temporalnexus.StartUntypedWorkflow[service.HelloOutput](ctx, nc, client.StartWorkflowOptions{ + ID: service.HelloWorkflowID(input), +}, HelloHandlerWorkflow, input.Name, input.Language) ``` - ### Register a Nexus Service in a Worker @@ -364,45 +357,6 @@ func HelloCallerWorkflow(ctx workflow.Context, name string, language service.Lan ``` -### Set Nexus Operation timeouts - -Nexus Operations support [three types of timeouts](/nexus/operations#timeouts) that control how long the caller is willing to wait at different stages of the Operation lifecycle. -Set these timeouts in `NexusOperationOptions` when calling `ExecuteOperation`. - -#### Schedule-to-Close timeout - -The [Schedule-to-Close timeout](/nexus/operations#schedule-to-close-timeout) limits the total duration of the Operation from when it is scheduled to when it completes. -The Nexus Machinery automatically retries failed requests until this timeout is exceeded. - -```go -fut := c.ExecuteOperation(ctx, service.HelloOperationName, service.HelloInput{Name: name, Language: language}, workflow.NexusOperationOptions{ - ScheduleToCloseTimeout: 10 * time.Minute, -}) -``` - -#### Schedule-to-Start timeout - -The [Schedule-to-Start timeout](/nexus/operations#schedule-to-start-timeout) limits how long the caller will wait for the Operation to be started by the handler. -If not set, no Schedule-to-Start timeout is enforced. - -```go -fut := c.ExecuteOperation(ctx, service.HelloOperationName, service.HelloInput{Name: name, Language: language}, workflow.NexusOperationOptions{ - ScheduleToStartTimeout: 2 * time.Minute, -}) -``` - -#### Start-to-Close timeout - -The [Start-to-Close timeout](/nexus/operations#start-to-close-timeout) limits how long the caller will wait for an asynchronous Operation to complete after it has been started. -This timeout only applies to asynchronous Operations. -If not set, no Start-to-Close timeout is enforced. - -```go -fut := c.ExecuteOperation(ctx, service.HelloOperationName, service.HelloInput{Name: name, Language: language}, workflow.NexusOperationOptions{ - StartToCloseTimeout: 5 * time.Minute, -}) -``` - ### Register the caller Workflow in a Worker After developing the caller Workflow, the next step is to register it with a Worker. @@ -450,7 +404,7 @@ func main() { ### Develop a starter to start the caller Workflow -To initiate the caller Workflow, a starter program is required. +To initiate the caller Workflow, a starter program is used. [nexus/caller/starter/main.go](https://github.com/temporalio/samples-go/blob/main/nexus/caller/starter/main.go) @@ -510,13 +464,13 @@ func runWorkflow(c client.Client, workflow interface{}, args ...interface{}) { ## Make Nexus calls across Namespaces with a development Server {/* #nexus-calls-across-namespaces-dev-server */} -Follow the steps below to run the Nexus handler Worker, the Nexus caller Worker, and the starter. +Follow the steps below to run the Nexus handler Worker, the Nexus caller Worker, and the starter app. ### Run Workers connected to a local development server Run the Nexus handler Worker: -``` +```bash cd handler go run ./worker \ -target-host localhost:7233 \ @@ -525,7 +479,7 @@ go run ./worker \ In another terminal window, run the Nexus caller Worker: -``` +```bash cd caller go run ./worker \ -target-host localhost:7233 \ @@ -538,7 +492,7 @@ With the Workers running, the final step in the local development process is to Run the starter: -``` +```bash cd caller go run ./starter \ -target-host localhost:7233 \ @@ -563,9 +517,11 @@ Only asynchronous operations can be canceled in Nexus, as cancelation is sent us The Workflow or other resources backing the operation may choose to ignore the cancelation request. If ignored, the operation may enter a terminal state. -Once the caller Workflow completes, the caller's Nexus Machinery will not make any further attempts to cancel operations that are still running. -It's okay to leave operations running in some use cases. -To ensure cancelations are delivered, wait for all pending operations to finish before exiting the Workflow. +Once the caller Workflow completes, the caller's Nexus Machinery stops attempting to cancel operations that have not yet +been canceled, letting them run to completion. + +It's okay to leave operations running in some use cases. To ensure cancelations are delivered, wait for all pending +operations to deliver their cancellation requests before exiting the Workflow. See the [Nexus cancelation sample](https://github.com/temporalio/samples-go/tree/main/nexus-cancelation) for reference. @@ -670,9 +626,12 @@ tcld nexus endpoint create \ The `--allow-namespace` flag adds caller Namespaces that can use the Nexus Endpoint to its allowlist. +The `--allow-namespace` is used to build an Endpoint allowlist of caller Namespaces that can use the Nexus Endpoint, as +described in Runtime Access Control. + Alternatively, you can create a Nexus Endpoint through the UI: [https://cloud.temporal.io/nexus](https://cloud.temporal.io/nexus). -### Run Workers Connected to Temporal Cloud with TLS certificates +### Run Workers connected to Temporal Cloud Run the handler Worker: @@ -698,7 +657,10 @@ go run ./worker \ -client-key 'path/to/your/ca.key' ``` -### Start a caller Workflow +To connect with an API key instead of mTLS certificates, replace `-client-cert` and `-client-key` with +`-api-key `. + +### Start a caller Workflow in Temporal Cloud ``` cd caller @@ -717,50 +679,6 @@ This will result in: 2024/10/04 19:57:40 Workflow result: Β‘Hola! Nexus πŸ‘‹ ``` -### Run Workers Connected to Temporal Cloud with API keys - -[View the source code](https://github.com/temporalio/samples-go/tree/main/nexus) in the context of the rest of the application code. - -Run the handler Worker: - -``` -cd handler - -go run ./worker \ - -target-host .tmprl.cloud:7233 \ - -namespace \ - -api-key -``` - -Run the caller Worker: - -``` -cd caller - -go run ./worker \ - -target-host .tmprl.cloud:7233 \ - -namespace \ - -api-key -``` - -### Start a caller Workflow - -``` -cd caller - -go run ./starter \ - -target-host .tmprl.cloud:7233 \ - -namespace \ - -api-key -``` - -This will result in: - -``` -2024/10/04 19:57:40 Workflow result: Nexus Echo πŸ‘‹ -2024/10/04 19:57:40 Workflow result: Β‘Hola! Nexus πŸ‘‹ -``` - ## Observability ### Web UI diff --git a/docs/develop/java/nexus/feature-guide.mdx b/docs/develop/java/nexus/feature-guide.mdx index 731a3ecd25..f12c1bd614 100644 --- a/docs/develop/java/nexus/feature-guide.mdx +++ b/docs/develop/java/nexus/feature-guide.mdx @@ -23,6 +23,13 @@ New to Nexus? Start with the [Nexus Java Quickstart](/develop/java/nexus/quickst ::: + +:::note + +This Feature Guide includes the new Nexus developer experience: pre-release APIs for the [Temporal Operation Handler](/nexus/temporal-operation-handler), [Nexus Standalone Activity](/nexus/standalone-activity), and (where supported) the [Nexus Code Generator](/nexus/code-generator). These APIs are experimental and subject to change. + +::: + This page shows how to do the following: - [Run a development Temporal Service with Nexus enabled](#run-the-temporal-nexus-development-server) @@ -94,99 +101,21 @@ Defining a clear contract for the Nexus Service is crucial for smooth communicat In this example, there is a service package that describes the Service and Operation names along with input/output types for caller Workflows to use the Nexus Endpoint. -Each [Temporal SDK includes and uses a default Data Converter](/dataconversion). The default -data converter encodes payloads in the following order: Null, Byte array, Protobuf JSON, and JSON. In a polyglot -environment, that is where more than one language and SDK is being used to develop a Temporal solution, Protobuf and -JSON are common choices. This example uses Java classes serialized into JSON. - - - -[core/src/main/java/io/temporal/samples/nexus/service/NexusService.java](https://github.com/temporalio/samples-java/blob/nexus-snip-sync/core/src/main/java/io/temporal/samples/nexus/service/NexusService.java) - -```java -@Service -public interface SampleNexusService { - enum Language { - EN, - FR, - DE, - ES, - TR - } - - class HelloInput { - private final String name; - private final Language language; - - @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) - public HelloInput( - @JsonProperty("name") String name, @JsonProperty("language") Language language) { - this.name = name; - this.language = language; - } - - @JsonProperty("name") - public String getName() { - return name; - } - - @JsonProperty("language") - public Language getLanguage() { - return language; - } - } - - class HelloOutput { - private final String message; +You can hand-write that package, but the preferred way is to generate it with the [Nexus Code Generator](https://github.com/temporalio/nexgen). +You write the contract once as a JSON or YAML definition file and run `nexgen` against it, and it emits the typed models, +runtime validators, and the Service definition itself. - @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) - public HelloOutput(@JsonProperty("message") String message) { - this.message = message; - } - - @JsonProperty("message") - public String getMessage() { - return message; - } - } - - class EchoInput { - private final String message; - - @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) - public EchoInput(@JsonProperty("message") String message) { - this.message = message; - } - - @JsonProperty("message") - public String getMessage() { - return message; - } - } - - class EchoOutput { - private final String message; - - @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) - public EchoOutput(@JsonProperty("message") String message) { - this.message = message; - } - - @JsonProperty("message") - public String getMessage() { - return message; - } - } - - @Operation - HelloOutput hello(HelloInput input); - - @Operation - EchoOutput echo(EchoInput input); -} -``` +This is what makes a Nexus Service polyglot. Both sides generate from the same definition file: the handler implements +the Service, the caller invokes its Operations, and neither hand-writes a request or response type. A Python handler +and a Go caller share no code, but they both run off that same service contract - so they interoperate with no +coordination between the teams beyond the contract itself. - +The generated validators check every payload against the contract, when a value is parsed off the wire and again when +it is serialized onto it, so bad data is rejected at the boundary rather than reaching your Workflow or Activity. A value validates +identically in every language, which is what lets a caller and a handler written in different languages trust the same +contract. See the [`chat.nexusrpc.yaml`](https://github.com/temporalio/nexgen/blob/main/samples/schemas/chat.nexusrpc.yaml) +sample contract and the [Definition files](https://github.com/temporalio/nexgen#definition-files) section of the +`nexgen` README for the file format. ## Develop a Nexus Service and Operation handlers {/* #develop-nexus-service-operation-handlers */} @@ -198,75 +127,74 @@ Use an asynchronous Nexus Operation when latency or availability is uncertain, t Handlers should be reliable since the [circuit breaker](/nexus/operations#circuit-breaking) trips after 5 consecutive retryable errors, blocking all Operations from the caller to that Endpoint. -The `io.temporal.nexus.*` packages have utilities to help create Nexus Operations: +Every Operation is written with [`TemporalOperationHandler`](/nexus/temporal-operation-handler). `TemporalOperationHandler.create(...)` hands your Operation handler three things: a context, a Client, and the +Operation input. What you do with the Client decides what backs the Operation: -- `Nexus.getOperationContext().getWorkflowClient()` \- Get the Temporal Client that the Worker was initialized with for - synchronous handlers backed by Temporal primitives such as Signals and Queries -- `WorkflowRunOperation.fromWorkflowMethod` \- Run a Workflow as an asynchronous Nexus Operation +- **Synchronous.** Return `TemporalOperationResult.sync(...)` and the Operation completes during the handler call. The + caller has its result as soon as the call returns. +- **Asynchronous.** Call `startWorkflow`, `startActivity`, or `startWorkflowUpdate` on the Client. The handler returns + as soon as that Execution has started, and the Operation stays open until the Execution finishes, which may be days + later. Its result is delivered to the caller through the Nexus completion callback. This is what lets an Operation + outlive the [Nexus request timeout](/cloud/limits#nexus-operation-request-timeout). -This example starts with a sync Operation handler example using the `OperationHandler.sync` method, and then shows how -to create an async Operation handler that uses `WorkflowRunOperation.fromWorkflowMethod` to start a handler Workflow -from a Nexus Operation. +A handler can perform any number of synchronous side effects, such as sending a Signal, but at most one asynchronous +backing per invocation. ### Develop a Synchronous Nexus Operation handler -The `OperationHandler.sync` method is for exposing simple RPC handlers. Use -`Nexus.getOperationContext().getWorkflowClient(ctx)` to get the Temporal Client for signaling, querying, and listing -Workflows. Implementations can also make other calls, but handlers should be reliable to avoid tripping the -[circuit breaker](/nexus/operations#circuit-breaking). +Return a synchronous result when the Operation can answer immediately. The handler computes the answer and returns it, +and the Operation completes during the call. -{/* SNIPSTART samples-java-nexus-handler {"selectedLines": ["1-16", "43"]} */} -[core/src/main/java/io/temporal/samples/nexus/handler/NexusServiceImpl.java](https://github.com/temporalio/samples-java/blob/nexus-snip-sync/core/src/main/java/io/temporal/samples/nexus/handler/NexusServiceImpl.java) +Handlers should be reliable to avoid tripping the [circuit breaker](/nexus/operations#circuit-breaking), and the whole +call has to finish inside the [Nexus request timeout](/cloud/limits#nexus-operation-request-timeout). ```java -// To create a service implementation, annotate the class with @ServiceImpl and provide the -// interface that the service implements. The service implementation class should have methods that -// return OperationHandler that correspond to the operations defined in the service interface. @ServiceImpl(service = SampleNexusService.class) public class SampleNexusServiceImpl { + @OperationImpl public OperationHandler echo() { - // OperationHandler.sync is a meant for exposing simple RPC handlers. - return OperationHandler.sync( - // The method is for making arbitrary short calls to other services or databases, or - // perform simple computations such as this one. Users can also access a workflow client by - // calling - // Nexus.getOperationContext().getWorkflowClient(ctx) to make arbitrary calls such as - // signaling, querying, or listing workflows. - (ctx, details, input) -> new SampleNexusService.EchoOutput(input.getMessage())); + return TemporalOperationHandler.create( + (ctx, client, input) -> + TemporalOperationResult.sync(new SampleNexusService.EchoOutput(input.getMessage()))); } -// ... } ``` -{/* SNIPEND */} - ### Use the Temporal Client for Signals, Queries, and Updates -A common pattern is to use the Temporal Client from within a sync handler to Signal, Query, or Update a Workflow. You -can also use Signal-With-Start or Update-With-Start to ensure the Workflow is started and send it a Signal or Update. -All calls must complete within the [Nexus request timeout](/cloud/limits#nexus-operation-request-timeout). Updates -should be short-lived to stay within this deadline. +A common pattern is to reach a Workflow that is already running. Query it or Signal it from a synchronous Operation, or +use Signal-With-Start to make sure the Workflow exists before the Signal arrives. Those calls complete during the +handler call, so they have to finish inside the +[Nexus request timeout](/cloud/limits#nexus-operation-request-timeout). -The [nexus_messaging](https://github.com/temporalio/samples-java/tree/main/core/src/main/java/io/temporal/samples/nexusmessaging) sample shows how to create a Nexus Service that uses synchronous operations to send Updates and Queries. +Updates are the exception. Do not wait for one inside the handler. Start it with `startWorkflowUpdate` and it backs the +Operation. The handler returns straight away, and the Operation completes when the Update does, however long it takes. -Use the Nexus library, as shown below, to get the Client that the Worker was initialized with. In this example, the Workflow Id is derived from the client Id, with the "getWorkflowId" method. This converts a given client Id (in this case, the client is passing in a user Id) to generate a Workflow Id from it. -This way the client only needs the identifier it cares about. +The [nexus_messaging](https://github.com/temporalio/samples-java/tree/main/core/src/main/java/io/temporal/samples/nexusmessaging) +sample shows a Nexus Service that Queries and Signals a running Workflow from synchronous Operations, and backs an +Operation with a Workflow Update. -[nexusmessaging/callerpattern/handler/NexusGreetingServiceImpl.java](https://github.com/temporalio/samples-java/blob/main/core/src/main/java/io/temporal/samples/nexusmessaging/callerpattern/handler/NexusGreetingServiceImpl.java) -```java -static final String WORKFLOW_ID_PREFIX = "GreetingWorkflow_for_"; +The Client your handler receives is not an ordinary Temporal Client. It propagates +[bidirectional links](/nexus/execution-debugging#bi-directional-linking) and request Ids on every call, so the +caller-side and handler-side Executions are connected in the UI without wiring anything. Reach the Workflow Client +through it rather than constructing your own. - public static String getWorkflowId(String userId) { - return WORKFLOW_ID_PREFIX + userId; - } +In this example the Workflow Id is derived from an identifier carried in the Operation input, so the caller only needs +the identifier it cares about: - private GreetingWorkflow getWorkflowStub(String userId) { - return Nexus.getOperationContext() - .getWorkflowClient() - .newWorkflowStub(GreetingWorkflow.class, getWorkflowId(userId)); - } - ... +```java +@OperationImpl +public OperationHandler approve() { + return TemporalOperationHandler.create( + (ctx, client, input) -> { + client + .getWorkflowClient() + .newWorkflowStub(GreetingWorkflow.class, "GreetingWorkflow_for_" + input.getUserId()) + .approve(input); + return TemporalOperationResult.sync(new ApproveOutput()); + }); +} ``` There are two examples of messaging through Nexus in the sample code, [caller pattern](https://github.com/temporalio/samples-java/tree/main/core/src/main/java/io/temporal/samples/nexusmessaging/callerpattern/) and [on-demand pattern](https://github.com/temporalio/samples-java/tree/main/core/src/main/java/io/temporal/samples/nexusmessaging/ondemandpattern/). @@ -274,46 +202,27 @@ The caller pattern shows how to send messages to an existing Workflow, while the ### Develop an Asynchronous Nexus Operation handler to start a Workflow -Use the `WorkflowRunOperation.fromWorkflowMethod` method, which is the easiest way to expose a Workflow as an operation. - - - -[core/src/main/java/io/temporal/samples/nexus/handler/NexusServiceImpl.java](https://github.com/temporalio/samples-java/blob/nexus-snip-sync/core/src/main/java/io/temporal/samples/nexus/handler/NexusServiceImpl.java) +Call `startWorkflow` on the Client. The Operation completes when the Workflow returns, and the Workflow's return +value is delivered to the caller as the Operation's result. ```java -// To create a service implementation, annotate the class with @ServiceImpl and provide the -// interface that the service implements. The service implementation class should have methods that -// return OperationHandler that correspond to the operations defined in the service interface. -@ServiceImpl(service = SampleNexusService.class) -public class SampleNexusServiceImpl { -// ... - @OperationImpl - public OperationHandler hello() { - // Use the WorkflowRunOperation.fromWorkflowMethod constructor, which is the easiest - // way to expose a workflow as an operation. To expose a workflow with a different input - // parameters then the operation or from an untyped stub, use the - // WorkflowRunOperation.fromWorkflowHandler constructor and the appropriate constructor method - // on WorkflowHandle. - return WorkflowRunOperation.fromWorkflowMethod( - (ctx, details, input) -> - Nexus.getOperationContext() - .getWorkflowClient() - .newWorkflowStub( - HelloHandlerWorkflow.class, - // Workflow IDs should typically be business meaningful IDs and are used to - // dedupe workflow starts. - // For this example, we're using the request ID allocated by Temporal when - // the - // caller workflow schedules - // the operation, this ID is guaranteed to be stable across retries of this - // operation. - // - // Task queue defaults to the task queue this operation is handled on. - WorkflowOptions.newBuilder().setWorkflowId(details.getRequestId()).build()) +@OperationImpl +public OperationHandler hello() { + return TemporalOperationHandler.create( + (ctx, client, input) -> + client.startWorkflow( + HelloHandlerWorkflow.class, + HelloHandlerWorkflow::hello, + input, + WorkflowOptions.newBuilder() + .setWorkflowId( + String.format( + "hello-%s-%s", + input.getName(), input.getLanguage().name().toLowerCase(Locale.ROOT))) + .build())); +} ``` - - Workflow IDs should typically be business-meaningful IDs and are used to dedupe Workflow starts. In general, the ID should be passed in the Operation input as part of the Nexus Service contract. @@ -326,67 +235,25 @@ Conflict-Policy of Use-Existing. #### Map a Nexus Operation input to multiple Workflow arguments -A Nexus Operation can only take one input parameter. If you want a Nexus Operation to start a Workflow that takes -multiple arguments use the `WorkflowRunOperation.fromWorkflowHandle` method. - - - -[core/src/main/java/io/temporal/samples/nexusmultipleargs/handler/NexusServiceImpl.java](https://github.com/temporalio/samples-java/blob/nexus-snip-sync/core/src/main/java/io/temporal/samples/nexusmultipleargs/handler/NexusServiceImpl.java) +A Nexus Operation can only take one input parameter. To start a Workflow that takes several, pass the arguments +directly to `startWorkflow` between the method reference and the Workflow options: ```java -// To create a service implementation, annotate the class with @ServiceImpl and provide the -// interface that the service implements. The service implementation class should have methods that -// return OperationHandler that correspond to the operations defined in the service interface. -@ServiceImpl(service = SampleNexusService.class) -public class SampleNexusServiceImpl { - @OperationImpl - public OperationHandler echo() { - // OperationHandler.sync is a meant for exposing simple RPC handlers. - return OperationHandler.sync( - // The method is for making arbitrary short calls to other services or databases, or - // perform simple computations such as this one. Users can also access a workflow client by - // calling - // Nexus.getOperationContext().getWorkflowClient(ctx) to make arbitrary calls such as - // signaling, querying, or listing workflows. - (ctx, details, input) -> new SampleNexusService.EchoOutput(input.getMessage())); - } - - @OperationImpl - public OperationHandler hello() { - // If the operation input parameters are different from the workflow input parameters, - // use the WorkflowRunOperation.fromWorkflowHandler constructor and the appropriate constructor - // method on WorkflowHandle to map the Nexus input to the workflow parameters. - return WorkflowRunOperation.fromWorkflowHandle( - (ctx, details, input) -> - WorkflowHandle.fromWorkflowMethod( - Nexus.getOperationContext() - .getWorkflowClient() - .newWorkflowStub( - HelloHandlerWorkflow.class, - // Workflow IDs should typically be business meaningful IDs and are used - // to - // dedupe workflow starts. - // For this example, we're using the request ID allocated by Temporal - // when - // the - // caller workflow schedules - // the operation, this ID is guaranteed to be stable across retries of - // this - // operation. - // - // Task queue defaults to the task queue this operation is handled on. - WorkflowOptions.newBuilder() - .setWorkflowId(details.getRequestId()) - .build()) - ::hello, - input.getName(), - input.getLanguage())); - } +@OperationImpl +public OperationHandler hello() { + return TemporalOperationHandler.create( + (ctx, client, input) -> + client.startWorkflow( + HelloHandlerWorkflow.class, + HelloHandlerWorkflow::hello, + input.getName(), + input.getLanguage(), + WorkflowOptions.newBuilder() + .setWorkflowId("hello-" + input.getName()) + .build())); } ``` - - ### Register a Nexus Service in a Worker After developing an asynchronous Nexus Operation handler to start a Workflow, the next step is to register a Nexus diff --git a/docs/develop/python/nexus/feature-guide.mdx b/docs/develop/python/nexus/feature-guide.mdx index 440e20b1b4..7fa2cea50d 100644 --- a/docs/develop/python/nexus/feature-guide.mdx +++ b/docs/develop/python/nexus/feature-guide.mdx @@ -22,6 +22,13 @@ New to Nexus? Start with the [Nexus Python Quickstart](/develop/python/nexus/qui ::: + +:::note + +This Feature Guide includes the new Nexus developer experience: pre-release APIs for the [Temporal Operation Handler](/nexus/temporal-operation-handler), [Nexus Standalone Activity](/nexus/standalone-activity), and (where supported) the [Nexus Code Generator](/nexus/code-generator). These APIs are experimental and subject to change. + +::: + This page shows how to do the following: - [Run a development Temporal Service with Nexus enabled](#run-the-temporal-nexus-development-server) @@ -30,12 +37,9 @@ This page shows how to do the following: - [Define the Nexus Service contract](#define-nexus-service-contract) - [Develop a Nexus Service and Operation handlers](#develop-nexus-service-operation-handlers) - [Develop a caller Workflow that uses a Nexus Service](#develop-caller-workflow-nexus-service) -- [Understand exceptions in Nexus Operations](#exceptions-in-nexus-operations) -- [Cancel a Nexus Operation](#canceling-a-nexus-operation) +- [Make Nexus calls across Namespaces with a development Server](#nexus-calls-across-namespaces-dev-server) - [Make Nexus calls across Namespaces in Temporal Cloud](#nexus-calls-across-namespaces-temporal-cloud) -
- :::note This documentation uses source code derived from the [Python Nexus sample](https://github.com/temporalio/samples-python/tree/main/hello_nexus). @@ -46,8 +50,10 @@ This documentation uses source code derived from the [Python Nexus sample](https Prerequisites: -- [Install the latest Temporal CLI](https://learn.temporal.io/getting_started/python/dev_environment/#set-up-a-local-temporal-service-for-development-with-temporal-cli) (`v1.3.0` or higher recommended) -- [Install the latest Temporal Python SDK](https://learn.temporal.io/getting_started/python/dev_environment/#add-temporal-python-sdk-dependencies) (`v1.14.1` or higher) +- [Install the latest Temporal CLI](https://learn.temporal.io/getting_started/python/dev_environment/#set-up-a-local-temporal-service-for-development-with-temporal-cli) + (`v1.3.0` or higher recommended) +- [Install the latest Temporal Python SDK](https://learn.temporal.io/getting_started/python/dev_environment/#add-temporal-python-sdk-dependencies) + (`v1.32.0` or higher recommended) The first step in working with Temporal Nexus involves starting a Temporal Server with Nexus enabled. @@ -68,8 +74,8 @@ temporal operator namespace create --namespace my-target-namespace temporal operator namespace create --namespace my-caller-namespace ``` -For this example, `my-target-namespace` will contain the Nexus Operation handler, and you will use a Workflow in `my-caller-namespace` to call that Operation handler. -We use different namespaces to demonstrate cross-Namespace Nexus calls. +`my-target-namespace` will contain the Nexus Operation handler, and we will use a Workflow in `my-caller-namespace` to +call that Operation handler. We use different namespaces to demonstrate cross-Namespace Nexus calls. ## Create a Nexus Endpoint to route requests from caller to handler {/* #create-nexus-endpoint */} @@ -88,131 +94,142 @@ You can also use the Web UI to create the Namespaces and Nexus endpoint. Defining a clear contract for the Nexus Service is crucial for smooth communication. -In this example, there is a service package that describes the Service and Operation names along with input/output types for caller Workflows to use the Nexus Endpoint. - -Each [Temporal SDK includes and uses a default Data Converter](/dataconversion). -The default data converter encodes payloads in the following order: Null, Byte array, Protobuf JSON, and JSON. -In a polyglot environment, that is where more than one language and SDK is being used to develop a Temporal solution, Protobuf and JSON are common choices. -This example uses Python dataclasses serialized into JSON. - -[hello_nexus/service.py](https://github.com/temporalio/samples-python/blob/main/hello_nexus/service.py) - -```python -from dataclasses import dataclass - -import nexusrpc - +In this example, there is a service module that describes the Service and Operation names along with input/output types +for caller Workflows to use the Nexus Endpoint. -@dataclass -class MyInput: - name: str +You can hand-write that module, but the preferred way is to generate it with the [Nexus Code Generator](https://github.com/temporalio/nexgen). +You write the contract once as a JSON or YAML definition file and run `nexgen` against it, and it emits the typed models, +runtime validators, and the Service definition itself. +This is what makes a Nexus Service polyglot. Both sides generate from the same definition file: the handler implements +the Service, the caller invokes its Operations, and neither hand-writes a request or response type. A Python handler +and a Go caller share no code, but they both run off that same service contract - so they interoperate with no +coordination between the teams beyond the contract itself. -@dataclass -class MyOutput: - message: str +The generated validators check every payload against the contract, when a value is parsed off the wire and again when +it is serialized onto it, so bad data is rejected at the boundary rather than reaching your Workflow or Activity. A value validates +identically in every language, which is what lets a caller and a handler written in different languages trust the same +contract. See the [`chat.nexusrpc.yaml`](https://github.com/temporalio/nexgen/blob/main/samples/schemas/chat.nexusrpc.yaml) +sample contract and the [Definition files](https://github.com/temporalio/nexgen#definition-files) section of the +`nexgen` README for the file format. - -@nexusrpc.service -class MyNexusService: - my_sync_operation: nexusrpc.Operation[MyInput, MyOutput] - my_workflow_run_operation: nexusrpc.Operation[MyInput, MyOutput] -``` - -## Develop a Nexus Service handler and Operation handlers {/* #develop-nexus-service-operation-handlers */} +## Develop a Nexus Service and Operation handlers {/* #develop-nexus-service-operation-handlers */} Nexus Operation handlers are typically defined in the same Worker as the underlying Temporal primitives they abstract. -Operation handlers can decide if a given Nexus Operation will be synchronous or asynchronous. -They can invoke underlying Temporal primitives such as a Query, Signal, or Update using the Temporal SDK Client, or run other reliable code. +Operation handlers can decide if a given Nexus Operation will be synchronous or asynchronous. They can invoke underlying +Temporal primitives such as a Query, Signal, or Update using the Temporal SDK Client, or run other reliable code. Use a synchronous Nexus Operation only when its complete execution path is highly reliable, has predictably low latency, and finishes well within the [10-second handler deadline](/cloud/limits#nexus-operation-request-timeout). Use an asynchronous Nexus Operation when latency or availability is uncertain, the work might exceed the handler deadline, or execution depends on a potentially unreliable service or database. -Handlers should be reliable since the [circuit breaker](/nexus/operations#circuit-breaking) trips after 5 consecutive retryable errors (for example: worker timeouts), blocking all Operations from the caller to that Endpoint. +Handlers should be reliable since the [circuit breaker](/nexus/operations#circuit-breaking) trips after 5 consecutive +retryable errors, blocking all Operations from the caller to that Endpoint. + +Every Operation is written with a [Temporal Operation Handler](/nexus/temporal-operation-handler). The +`@nexus.temporal_operation` decorator hands your start method three things: a context, a Client, and the Operation +input. What you do with the Client decides what backs the Operation: -The `nexusrpc.handler` and `temporalio.nexus` modules have utilities to help create Nexus Operations: +- **Synchronous.** Return `nexus.TemporalOperationResult.sync(...)` and the Operation completes during the handler call. + The caller has its result as soon as the call returns. +- **Asynchronous.** Call `start_workflow`, `start_activity`, or `start_workflow_update` on the Client. The handler + returns as soon as that Execution has started, and the Operation stays open until the Execution finishes, which may be + days later. Its result is delivered to the caller through the Nexus completion callback. This is what lets an + Operation outlive the [Nexus request timeout](/cloud/limits#nexus-operation-request-timeout). -- `nexusrpc.handler.sync_operation` - Create a synchronous operation handler -- `nexus.workflow_run_operation` - Create an asynchronous operation handler that starts a Workflow +A handler can perform any number of synchronous side effects, such as sending a Signal, but at most one asynchronous +backing per invocation. ### Develop a Synchronous Nexus Operation handler -The `@nexusrpc.handler.sync_operation` decorator is for exposing simple RPC handlers. +Return a synchronous result when the Operation can answer immediately. The handler computes the answer and returns it, +and the Operation completes during the call. -[hello_nexus/handler/service_handler.py](https://github.com/temporalio/samples-python/blob/main/hello_nexus/handler/service_handler.py) +Handlers should be reliable to avoid tripping the [circuit breaker](/nexus/operations#circuit-breaking), and the whole +call has to finish inside the [Nexus request timeout](/cloud/limits#nexus-operation-request-timeout). ```python import nexusrpc +from temporalio import nexus + @nexusrpc.handler.service_handler(service=MyNexusService) class MyNexusServiceHandler: - @nexusrpc.handler.sync_operation - async def my_sync_operation( - self, ctx: nexusrpc.handler.StartOperationContext, input: MyInput - ) -> MyOutput: - return MyOutput(message=f"Hello {input.name} from sync operation!") + @nexus.temporal_operation + async def echo( + self, + _ctx: nexus.TemporalStartOperationContext, + client: nexus.TemporalNexusClient, + input: EchoInput, + ) -> nexus.TemporalOperationResult[EchoOutput]: + return nexus.TemporalOperationResult.sync(EchoOutput(message=input.message)) ``` - -A synchronous operation handler must return quickly (less than `10s`). -Implementations can also make other calls, but handlers should be reliable to avoid tripping the [circuit breaker](/nexus/operations#circuit-breaking). - ### Use the Temporal Client for Signals, Queries, and Updates -A common pattern is to use the Temporal Client from within a sync handler to Signal, Query, or Update a Workflow. -You can also use Signal-With-Start or Update-With-Start to ensure the Workflow is started and send it a Signal or Update. -All calls must complete within the [Nexus request timeout](/cloud/limits#nexus-operation-request-timeout). Updates should be short-lived to stay within this deadline. - -The [nexus_messaging](https://github.com/temporalio/samples-python/tree/main/nexus_messaging) sample shows how to create a Nexus Service that uses synchronous operations to send Updates and Queries. +A common pattern is to reach a Workflow that is already running. Query it or Signal it from a synchronous Operation, or +use Signal-With-Start to make sure the Workflow exists before the Signal arrives. Those calls complete during the +handler call, so they have to finish inside the +[Nexus request timeout](/cloud/limits#nexus-operation-request-timeout). -Use `nexus.client()` to get the Client that the Worker was initialized with. In this example, the Workflow Id is derived from the client Id, with the "get_workflow_id" method. This takes a given client Id (in this case, the client is passing in a user ID) to generate a Workflow Id from it. -This way the client only needs the identifier it cares about. +Updates are the exception. Do not wait for one inside the handler. Start it with `start_workflow_update` and it backs +the Operation. The handler returns straight away, and the Operation completes when the Update does, however long it +takes. -[nexus_messaging/callerpattern/handler/service_handler.py](https://github.com/temporalio/samples-python/blob/main/nexus_messaging/callerpattern/handler/service_handler.py) +The [nexus_messaging](https://github.com/temporalio/samples-python/tree/main/nexus_messaging) +sample shows a Nexus Service that Queries and Signals a running Workflow from synchronous Operations, and backs an +Operation with a Workflow Update. -```python -from temporalio import nexus +The Client your handler receives is not an ordinary Temporal Client. It propagates +[bidirectional links](/nexus/execution-debugging#bi-directional-linking) and request Ids on every call, so the +caller-side and handler-side Executions are connected in the UI without wiring anything. Reach the Workflow Client +through `client.client` rather than constructing your own. -def get_workflow_id(user_id: str) -> str: - return f"{WORKFLOW_ID_PREFIX}{user_id}" +In this example the Workflow Id is derived from an identifier carried in the Operation input, so the caller only needs +the identifier it cares about: +```python @nexusrpc.handler.service_handler(service=NexusGreetingService) class NexusGreetingServiceHandler: - def _get_workflow_handle( - self, user_id: str + self, client: Client, user_id: str ) -> WorkflowHandle[GreetingWorkflow, str]: - return nexus.client().get_workflow_handle_for( - GreetingWorkflow.run, get_workflow_id(user_id) + return client.get_workflow_handle_for( + GreetingWorkflow.run, f"GreetingWorkflow_for_{user_id}" ) - ... + @nexus.temporal_operation + async def approve( + self, + _ctx: nexus.TemporalStartOperationContext, + client: nexus.TemporalNexusClient, + input: ApproveInput, + ) -> nexus.TemporalOperationResult[ApproveOutput]: + await self._get_workflow_handle(client.client, input.user_id).signal( + GreetingWorkflow.approve, input + ) + return nexus.TemporalOperationResult.sync(ApproveOutput()) ``` -There are two examples of messaging through Nexus in the sample code, [caller pattern](https://github.com/temporalio/samples-python/blob/main/nexus_messaging/callerpattern/) and [on demand pattern](https://github.com/temporalio/samples-python/blob/main/nexus_messaging/ondemandpattern/). +There are two examples of messaging through Nexus in the sample code, [caller pattern](https://github.com/temporalio/samples-python/tree/main/nexus_messaging/callerpattern/) and [on-demand pattern](https://github.com/temporalio/samples-python/tree/main/nexus_messaging/ondemandpattern/). The caller pattern shows how to send messages to an existing Workflow, while the on-demand pattern shows how to start a Workflow through Nexus and then send Signals to it. -In addition to `nexus.client()`, you can use `nexus.info()` to access information about the currently-executing Nexus Operation including its Task Queue. - - ### Develop an Asynchronous Nexus Operation handler to start a Workflow -Use the `@nexus.workflow_run_operation` decorator, which is the easiest way to expose a Workflow as an operation. - -[hello_nexus/handler/service_handler.py](https://github.com/temporalio/samples-python/blob/main/hello_nexus/handler/service_handler.py) +Call `start_workflow` on the Client. The Operation completes when the Workflow returns, and the Workflow's return value +is delivered to the caller as the Operation's result. ```python -import nexusrpc -from temporalio import nexus - @nexusrpc.handler.service_handler(service=MyNexusService) class MyNexusServiceHandler: - @nexus.workflow_run_operation - async def my_workflow_run_operation( - self, ctx: nexus.WorkflowRunOperationContext, input: MyInput - ) -> nexus.WorkflowHandle[MyOutput]: - return await ctx.start_workflow( - WorkflowStartedByNexusOperation.run, + @nexus.temporal_operation + async def hello( + self, + _ctx: nexus.TemporalStartOperationContext, + client: nexus.TemporalNexusClient, + input: HelloInput, + ) -> nexus.TemporalOperationResult[HelloOutput]: + return await client.start_workflow( + HelloHandlerWorkflow.run, input, - id=str(uuid.uuid4()), + id=f"hello-{input.name}-{input.language}", ) ``` @@ -226,42 +243,18 @@ Workflow IDs should typically be business-meaningful IDs and are used to dedupe #### Map a Nexus Operation input to multiple Workflow arguments -A Nexus Operation can only take one input parameter. If you want a Nexus Operation to start a Workflow that takes multiple arguments use the `ctx.start_workflow` method. - - -[nexus_multiple_args/handler/service_handler.py](https://github.com/temporalio/samples-python/blob/main/nexus_multiple_args/handler/service_handler.py) -```py -@nexusrpc.handler.service_handler(service=MyNexusService) -class MyNexusServiceHandler: - """ - Service handler that demonstrates multiple argument handling in Nexus operations. - """ - - # This is a nexus operation that is backed by a Temporal workflow. - # The key feature here is that it demonstrates how to map a single input object - # (HelloInput) to a workflow that takes multiple individual arguments. - @nexus.workflow_run_operation - async def hello( - self, ctx: nexus.WorkflowRunOperationContext, input: HelloInput - ) -> nexus.WorkflowHandle[HelloOutput]: - """ - Start a workflow with multiple arguments unpacked from the input object. - """ - return await ctx.start_workflow( - HelloHandlerWorkflow.run, - args=[ - input.name, # First argument: name - input.language, # Second argument: language - ], - id=f"hello-multi-args-{input.name}-{input.language}", - ) - +A Nexus Operation can only take one input parameter. To start a Workflow that takes several, pass them to +`start_workflow` as `args` instead of a single positional argument: +```python +return await client.start_workflow( + HelloHandlerWorkflow.run, + args=[input.name, input.language], + id=f"hello-{input.name}-{input.language}", +) ``` - - -### Register your Nexus Service handler in a Worker {/* #register-a-nexus-service-in-a-worker */} +### Register a Nexus Service in a Worker After developing an asynchronous Nexus Operation handler to start a Workflow, the next step is to register your Nexus Service handler in a Worker. At this stage you can pass any arguments you need to your service handler's `__init__` method. @@ -274,7 +267,7 @@ async def main(): worker = Worker( client, task_queue=TASK_QUEUE, - workflows=[WorkflowStartedByNexusOperation], + workflows=[HelloHandlerWorkflow], nexus_service_handlers=[MyNexusServiceHandler()], ) await worker.run() @@ -292,6 +285,7 @@ from temporalio import workflow with workflow.unsafe.imports_passed_through(): from hello_nexus.service import MyInput, MyNexusService, MyOutput + @workflow.defn class CallerWorkflow: @workflow.run @@ -319,22 +313,25 @@ class CallerWorkflow: After developing the caller Workflow, the next step is to register it with a Worker. -Finally, the caller Workflow must be started using `client.start_workflow()` or `client.execute_workflow()` +Finally, the caller Workflow must be started using `client.start_workflow()` or `client.execute_workflow()`. These steps are the same as for any normal Workflow. The Python sample combines them in a single application. See [hello_nexus/caller/app.py](https://github.com/temporalio/samples-python/blob/main/hello_nexus/caller/app.py) for reference. -## Exceptions in Nexus operations {/* #exceptions-in-nexus-operations */} +## Make Nexus calls across Namespaces with a development Server {/* #nexus-calls-across-namespaces-dev-server */} -Temporal provides general guidance on [Errors in Nexus operations](/references/failures#errors-in-nexus-operations). -In Python, there are three Nexus-specific exception classes: +In one terminal, run the Temporal worker in the handler namespace: +``` +uv run handler/worker.py +``` -- [`nexusrpc.OperationError`](https://nexus-rpc.github.io/sdk-python/nexusrpc.OperationError.html): this is the exception type you should raise in a Nexus operation to indicate that it has failed according to its own application logic and should not be retried. -- [`nexusrpc.HandlerError`](https://nexus-rpc.github.io/sdk-python/nexusrpc.HandlerError.html): you can raise this exception type in a Nexus operation with a specific [HandlerErrorType](https://nexus-rpc.github.io/sdk-python/nexusrpc.HandlerErrorType.html). The error will be marked retryable or non-retryable according to the type, following the [Nexus spec](https://github.com/nexus-rpc/api/blob/main/SPEC.md#predefined-handler-errors). The non-retryable handler error types are `BAD_REQUEST`, `UNAUTHENTICATED`, `UNAUTHORIZED`, `NOT_FOUND`, `NOT_IMPLEMENTED`; the retryable types are `RESOURCE_EXHAUSTED`, `INTERNAL`, `UNAVAILABLE`, `UPSTREAM_TIMEOUT`. -- [`temporalio.exceptions.NexusOperationError`](https://python.temporal.io/temporalio.exceptions.NexusOperationError.html): this is the error raised inside a Workflow when a Nexus operation fails for any reason. Use the `__cause__` attribute on the exception to access the cause chain. +In another terminal, run the Temporal worker in the caller namespace and start the caller workflow: +``` +uv run caller/app.py +``` -## Canceling a Nexus Operation {/* #canceling-a-nexus-operation */} +### Canceling a Nexus Operation {/* #canceling-a-nexus-operation */} To cancel a Nexus Operation from within a Workflow, call `handle.cancel()` on the operation handle. Only asynchronous operations can be canceled in Nexus, since cancellation is sent using an operation token. The Workflow or other resources backing the operation may choose to ignore the cancellation request. @@ -343,15 +340,20 @@ If ignored, the operation may enter a terminal state. When a Nexus operation is started, the caller can specify different cancellation types that control how the caller reacts to cancellation: - `ABANDON` - Do not request cancellation of the operation. -- `TRY_CANCEL` - Initiate a cancellation request and immediately report cancellation to the caller. Note that this type doesn't guarantee that cancellation is delivered to the operation handler if the caller exits before the delivery is done. -- `WAIT_REQUESTED` Request cancellation of the operation and wait for confirmation that the request was received. Doesn't wait for actual cancellation. +- `TRY_CANCEL` - Initiate a cancellation request and immediately report cancellation to the caller. Note that this type + doesn't guarantee that cancellation is delivered to the operation handler if the caller exits before the delivery is + done. +- `WAIT_REQUESTED` - Request cancellation of the operation and wait for confirmation that the request was received. + Doesn't wait for actual cancellation. - `WAIT_COMPLETED` - Wait for operation completion. Operation may or may not complete as cancelled. The default is `WAIT_COMPLETED`. Users can set a different option for `cancellation_type` when starting or executing an operation. -Once the caller Workflow completes, the caller's Nexus Machinery will not make any further attempts to cancel operations that are still running. -It's okay to leave operations running in some use cases. -To ensure cancellations are delivered, wait for all pending operations to finish before exiting the Workflow. +Once the caller Workflow completes, the caller's Nexus Machinery stops attempting to cancel operations that have not yet +been canceled, letting them run to completion. + +It's okay to leave operations running in some use cases. To ensure cancellations are delivered, wait for all pending +operations to deliver their cancellation requests before exiting the Workflow. See the [Nexus cancellation sample](https://github.com/temporalio/samples-python/tree/main/nexus_cancel) for reference. @@ -407,18 +409,18 @@ temporal cloud namespace create \ tcld login tcld namespace create \ - --namespace \ - --cloud-provider aws \ - --region us-west-2 \ - --ca-certificate-file 'path/to/your/ca.pem' \ - --retention-days 1 + --namespace \ + --cloud-provider aws \ + --region us-west-2 \ + --ca-certificate-file 'path/to/your/ca.pem' \ + --retention-days 1 tcld namespace create \ - --namespace \ - --cloud-provider aws \ - --region us-west-2 \ - --ca-certificate-file 'path/to/your/ca.pem' \ - --retention-days 1 + --namespace \ + --cloud-provider aws \ + --region us-west-2 \ + --ca-certificate-file 'path/to/your/ca.pem' \ + --retention-days 1 ``` diff --git a/docs/develop/typescript/nexus/feature-guide.mdx b/docs/develop/typescript/nexus/feature-guide.mdx index 891270c394..614ce57021 100644 --- a/docs/develop/typescript/nexus/feature-guide.mdx +++ b/docs/develop/typescript/nexus/feature-guide.mdx @@ -11,7 +11,7 @@ tags: import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; -import { CaptionedImage } from "@site/src/components"; +import { CaptionedImage } from '@site/src/components'; Use [Temporal Nexus](/evaluate/nexus) to connect Temporal Applications within and across Namespaces using a Nexus Endpoint, a Nexus Service contract, and Nexus Operations. @@ -21,6 +21,13 @@ New to Nexus? Start with the [Nexus TypeScript Quickstart](/develop/typescript/n ::: + +:::note + +This Feature Guide includes the new Nexus developer experience: pre-release APIs for the [Temporal Operation Handler](/nexus/temporal-operation-handler), [Nexus Standalone Activity](/nexus/standalone-activity), and (where supported) the [Nexus Code Generator](/nexus/code-generator). These APIs are experimental and subject to change. + +::: + This page shows how to do the following: - [Run a development Temporal Service with Nexus enabled](#run-the-temporal-nexus-development-server) @@ -29,12 +36,9 @@ This page shows how to do the following: - [Define the Nexus Service contract](#define-nexus-service-contract) - [Develop a Nexus Service and Operation handlers](#develop-nexus-service-operation-handlers) - [Develop a caller Workflow that uses a Nexus Service](#develop-caller-workflow-nexus-service) -- [Understand exceptions in Nexus Operations](#exceptions-in-nexus-operations) -- [Cancel a Nexus Operation](#canceling-a-nexus-operation) +- [Make Nexus calls across Namespaces with a development Server](#nexus-calls-across-namespaces-dev-server) - [Make Nexus calls across Namespaces in Temporal Cloud](#nexus-calls-across-namespaces-temporal-cloud) -
- :::note This documentation uses source code derived from the [TypeScript Nexus sample](https://github.com/temporalio/samples-typescript/tree/main/nexus-hello). @@ -45,8 +49,10 @@ This documentation uses source code derived from the [TypeScript Nexus sample](h Prerequisites: -- [Install the latest Temporal CLI](https://learn.temporal.io/getting_started/typescript/dev_environment/#set-up-a-local-temporal-service-for-development-with-temporal-cli) (`v1.3.0` or higher recommended) -- [Install the latest Temporal TypeScript SDK](https://learn.temporal.io/getting_started/typescript/dev_environment/#add-temporal-typescript-sdk-dependencies) (`v1.12.3` or higher) +- [Install the latest Temporal CLI](https://learn.temporal.io/getting_started/typescript/dev_environment/#set-up-a-local-temporal-service-for-development-with-temporal-cli) + (`v1.3.0` or higher recommended) +- [Install the latest Temporal TypeScript SDK](https://learn.temporal.io/getting_started/typescript/dev_environment/#add-temporal-typescript-sdk-dependencies) + (`v1.23.0` or higher recommended) The first step in working with Temporal Nexus involves starting a Temporal Server with Nexus enabled. @@ -67,8 +73,8 @@ temporal operator namespace create --namespace my-target-namespace temporal operator namespace create --namespace my-caller-namespace ``` -For this example, `my-target-namespace` will contain the Nexus Operation handler, and you will use a Workflow in `my-caller-namespace` to call that Operation handler. -We use different namespaces to demonstrate cross-Namespace Nexus calls. +`my-target-namespace` will contain the Nexus Operation handler, and we will use a Workflow in `my-caller-namespace` to +call that Operation handler. We use different namespaces to demonstrate cross-Namespace Nexus calls. ## Create a Nexus Endpoint to route requests from caller to handler {/* #create-nexus-endpoint */} @@ -87,136 +93,111 @@ You can also use the Web UI to create the Namespaces and Nexus endpoint. Defining a clear contract for the Nexus Service is crucial for smooth communication. -In this example, there is a service package that describes the Service and Operation names along with input/output types for caller Workflows to use the Nexus Endpoint. +In this example, there is a service module that describes the Service and Operation names along with input/output types +for caller Workflows to use the Nexus Endpoint. -Each [Temporal SDK includes and uses a default Data Converter](/dataconversion). -The default data converter encodes payloads in the following order: Null, Byte array, and JSON. -In a polyglot environment, that is where more than one language and SDK is being used to develop a Temporal solution, JSON is a common choice. -This example uses plain TypeScript objects, serialized into JSON. +You can hand-write that module, but the preferred way is to generate it with the [Nexus Code Generator](https://github.com/temporalio/nexgen). +You write the contract once as a JSON or YAML definition file and run `nexgen` against it, and it emits the typed models, +runtime validators, and the Service definition itself. -Note: By default, the TypeScript SDK [does not support Protobuf JSON encoding](https://typescript.temporal.io/api/interfaces/common.PayloadConverter). If passing Protobuf payloads use the [ProtobufJsonPayloadConverter](https://typescript.temporal.io/api/classes/protobufs.ProtobufJsonPayloadConverter) instead. +This is what makes a Nexus Service polyglot. Both sides generate from the same definition file: the handler implements +the Service, the caller invokes its Operations, and neither hand-writes a request or response type. A Python handler +and a Go caller share no code, but they both run off that same service contract - so they interoperate with no +coordination between the teams beyond the contract itself. - -[nexus-hello/src/api.ts](https://github.com/temporalio/samples-typescript/blob/main/nexus-hello/src/api.ts) -```ts -import * as nexus from 'nexus-rpc'; +The generated validators check every payload against the contract, when a value is parsed off the wire and again when +it is serialized onto it, so bad data is rejected at the boundary rather than reaching your Workflow or Activity. A value validates +identically in every language, which is what lets a caller and a handler written in different languages trust the same +contract. See the [`chat.nexusrpc.yaml`](https://github.com/temporalio/nexgen/blob/main/samples/schemas/chat.nexusrpc.yaml) +sample contract and the [Definition files](https://github.com/temporalio/nexgen#definition-files) section of the +`nexgen` README for the file format. -export const helloService = nexus.service('hello', { - /** - * Return the input message, unmodified. In the present sample, this Operation - * will be implemented using the Synchronous Nexus Operation handler syntax. - */ - echo: nexus.operation(), - - /** - * Return a salutation message, in the requested language. In the present sample, - * this Operation will be implemented by starting the `helloWorkflow` Workflow. - */ - hello: nexus.operation(), -}); +## Develop a Nexus Service and Operation handlers {/* #develop-nexus-service-operation-handlers */} -export interface EchoInput { - message: string; -} - -export interface EchoOutput { - message: string; -} - -export interface HelloInput { - name: string; - language: LanguageCode; -} - -export interface HelloOutput { - message: string; -} - -export type LanguageCode = 'en' | 'fr' | 'de' | 'es' | 'tr'; -``` - - -## Develop a Nexus Service handler and Operation handlers {/* #develop-nexus-service-operation-handlers */} - -A Nexus Service handler is defined using the `nexus-rpc`'s [`serviceHandler`](https://nexus-rpc.github.io/sdk-typescript/functions/serviceHandler.html) function. {/* Added */} -Nexus Service handlers are typically defined in the same Worker as the underlying Temporal primitives they abstract. -A Service handler must provide Operation handlers for each Operation declared by the Service. {/* Added */} -Operation handlers can decide if a given Nexus Operation will be synchronous or asynchronous. -They can invoke underlying Temporal primitives such as a Query, Signal, or Update using the Temporal SDK Client, or run other reliable code. +Nexus Operation handlers are typically defined in the same Worker as the underlying Temporal primitives they abstract. +Operation handlers can decide if a given Nexus Operation will be synchronous or asynchronous. They can invoke underlying +Temporal primitives such as a Query, Signal, or Update using the Temporal SDK Client, or run other reliable code. Use a synchronous Nexus Operation only when its complete execution path is highly reliable, has predictably low latency, and finishes well within the [10-second handler deadline](/cloud/limits#nexus-operation-request-timeout). Use an asynchronous Nexus Operation when latency or availability is uncertain, the work might exceed the handler deadline, or execution depends on a potentially unreliable service or database. -Handlers should be reliable since the [circuit breaker](/nexus/operations#circuit-breaking) trips after 5 consecutive retryable errors, blocking all Operations from the caller to that Endpoint. +Handlers should be reliable since the [circuit breaker](/nexus/operations#circuit-breaking) trips after 5 consecutive +retryable errors, blocking all Operations from the caller to that Endpoint. -The `@temporalio/nexus` package provides utilities to help create Nexus Operations that interact with a Temporal namespace: {/* Extended */} +Every Operation is written with [`TemporalOperationHandler`](/nexus/temporal-operation-handler). Its `start` function +receives three things: a context, a Client, and the Operation input. What you do with the Client decides what backs the +Operation: -- `WorkflowRunOperationHandler` - Create an asynchronous operation handler that starts a Workflow. -- `getClient()` - Get a Temporal Client connected using the same `NativeConnection` as the present Temporal Worker. - It can be used to implement synchronous handlers backed by Temporal primitives such as Signals and Queries. +- **Synchronous.** Return `TemporalOperationResult.sync(...)` and the Operation completes during the handler call. The + caller has its result as soon as the call returns. +- **Asynchronous.** Call `startWorkflow` or `startActivity` on the Client, or `update` on a handle from + `getWorkflowHandle`. The handler returns as soon as that Execution has started, and the Operation stays open until the + Execution finishes, which may be days later. Its result is delivered to the caller through the Nexus completion + callback. This is what lets an Operation outlive the + [Nexus request timeout](/cloud/limits#nexus-operation-request-timeout). + +A handler can perform any number of synchronous side effects, such as sending a Signal, but at most one asynchronous +backing per invocation. ### Develop a Synchronous Nexus Operation handler -Simple RPC handlers can be implemented as synchronous Nexus Operation handlers, which is defined in TypeScript as a simple async function. {/* sync operation vs async func is very confusing in this context */} -Use `getClient()` from `@temporalio/nexus` to get the Temporal Client for signaling, querying, and listing Workflows. -Implementations can also make other calls, but handlers should be reliable to avoid tripping the [circuit breaker](/nexus/operations#circuit-breaking). +Return a synchronous result when the Operation can answer immediately. The handler computes the answer and returns it, +and the Operation completes during the call. + +Handlers should be reliable to avoid tripping the [circuit breaker](/nexus/operations#circuit-breaking), and the whole +call has to finish inside the [Nexus request timeout](/cloud/limits#nexus-operation-request-timeout). - -[nexus-hello/src/service/handler.ts](https://github.com/temporalio/samples-typescript/blob/main/nexus-hello/src/service/handler.ts) ```ts import * as nexus from 'nexus-rpc'; -// ... -import { helloService, EchoInput, EchoOutput, HelloInput, HelloOutput } from '../api'; -// ... +import * as temporalNexus from '@temporalio/nexus'; +import { helloService, EchoInput, EchoOutput } from '../api'; + export const helloServiceHandler = nexus.serviceHandler(helloService, { - echo: async (ctx, input: EchoInput): Promise => { - // A simple async function can be used to defined a Synchronous Nexus Operation. - // This is often sufficient for Operations that simply make arbitrary short calls to - // other services or databases, or that perform simple computations such as this one. - // - // You may also access a Temporal Client by calling `temporalNexus.getClient()`. - // That Client can be used to make arbitrary calls, such as signaling, querying, - // or listing workflows. - return input; - }, -// ... + echo: new temporalNexus.TemporalOperationHandler({ + start: async (ctx, client, input) => { + return temporalNexus.TemporalOperationResult.sync({ message: input.message }); + }, + }), }); ``` - ### Use the Temporal Client for Signals, Queries, and Updates -A common pattern is to use the Temporal Client from within a sync handler to Signal, Query, or Update a Workflow. -You can also use Signal-With-Start or Update-With-Start to ensure the Workflow is started and send it a Signal or Update. -All calls must complete within the [Nexus request timeout](/cloud/limits#nexus-operation-request-timeout). -The handler receives an AbortSignal via `ctx.abortSignal` that is triggered when the deadline is exceeded -β€” pass it to Temporal Client calls to ensure they are canceled if the timeout is reached. -Updates should be short-lived to stay within this deadline. +A common pattern is to reach a Workflow that is already running. Query it or Signal it from a synchronous Operation, or +use `signalWithStartWorkflow` to make sure the Workflow exists before the Signal arrives. Those calls complete during the +handler call, so they have to finish inside the +[Nexus request timeout](/cloud/limits#nexus-operation-request-timeout). The handler receives an `AbortSignal` on +`ctx.abortSignal` that fires when the deadline is exceeded. Pass it to Temporal Client calls so they are canceled if +the timeout is reached. -The handler context also exposes `ctx.requestDeadline` as an optional `Date`, representing the time by which the current request must complete. -Note that this is the deadline for the current _request_, not the overall operation. -Use it to make decisions about whether to start work that may not finish in time, or to set timeouts on downstream calls. +Updates are the exception. Do not wait for one inside the handler. Start it with `update` on a handle from +`getWorkflowHandle` and it backs the Operation. The handler returns straight away, and the Operation completes when the +Update does, however long it takes. -The [nexus_messaging](https://github.com/temporalio/samples-typescript/tree/main/nexus-messaging) sample shows how to create a Nexus Service that uses synchronous operations to send Updates and Queries. +The [nexus-messaging](https://github.com/temporalio/samples-typescript/tree/main/nexus-messaging) +sample shows a Nexus Service that Queries and Signals a running Workflow from synchronous Operations, and backs an +Operation with a Workflow Update. -Use the Nexus library, as shown below, to get the Client that the Worker was initialized with. In this example, the Workflow Id is derived from the client Id, with the "workflowIdForUser" method. This converts a given client Id (in this case, the client is passing in a user ID) into a Workflow Id. -This way the client only needs the identifier it cares about. +The Client your handler receives is not an ordinary Temporal Client. It propagates +[bidirectional links](/nexus/execution-debugging#bi-directional-linking) and request Ids on every call, so the +caller-side and handler-side Executions are connected in the UI without wiring anything. Reach the Workflow Client +through `client.client` rather than constructing your own. -[nexus-messaging/src/callerpattern/service/handler.ts](https://github.com/temporalio/samples-typescript/blob/main/nexus-messaging/src/callerpattern/service/handler.ts) +In this example the Workflow Id is derived from an identifier carried in the Operation input, so the caller only needs +the identifier it cares about: ```ts -import * as temporalNexus from '@temporalio/nexus'; - function workflowIdForUser(userId: string): string { return `GreetingWorkflow_for_${userId}`; } export const nexusGreetingServiceHandler = nexus.serviceHandler(nexusGreetingService, { - getLanguages: async (ctx, input: GetLanguagesInput) => { - const client = temporalNexus.getClient(); - const handle = client.workflow.getHandle(workflowIdForUser(input.userId)); - return await handle.query(getLanguagesQuery); - }, - - ... + getLanguages: new temporalNexus.TemporalOperationHandler({ + async start(_ctx, client, input: GetLanguagesInput) { + const handle = client.client.workflow.getHandle(workflowIdForUser(input.userId)); + const result = await handle.query(getLanguagesQuery); + return temporalNexus.TemporalOperationResult.sync(result); + }, + }), +}); ``` There are two examples of messaging through Nexus in the sample code, [caller pattern](https://github.com/temporalio/samples-typescript/tree/main/nexus-messaging/src/callerpattern) and [on-demand pattern](https://github.com/temporalio/samples-typescript/tree/main/nexus-messaging/src/ondemandpattern). @@ -224,43 +205,24 @@ The caller pattern shows how to send messages to an existing Workflow, while the ### Develop an Asynchronous Nexus Operation handler to start a Workflow -Use `@temporalio/nexus`'s `WorkflowRunOperationHandler` helper class to easily expose a Temporal Workflow as a Nexus Operation. -Note that even though a Nexus operation can only take one input parameter, if you need to pass -multiple arguments through to the workflow, you can do so by using multiple properties of the input object, and placing them in -the array provided to the `args` option when calling `startWorkflow`. +Call `startWorkflow` on the Client. The Operation completes when the Workflow returns, and the Workflow's return value +is delivered to the caller as the Operation's result. - -[nexus-hello/src/service/handler.ts](https://github.com/temporalio/samples-typescript/blob/main/nexus-hello/src/service/handler.ts) ```ts -import * as nexus from 'nexus-rpc'; -import * as temporalNexus from '@temporalio/nexus'; -import { helloService, EchoInput, EchoOutput, HelloInput, HelloOutput } from '../api'; -import { helloWorkflow } from './workflows'; - -// ... export const helloServiceHandler = nexus.serviceHandler(helloService, { -// ... - hello: new temporalNexus.WorkflowRunOperationHandler( - // WorkflowRunOperationHandler takes a function that receives the Operation's context and input. - // That function can be used to validate and/or transform the input before passing it to - // the Workflow, as well as to customize various Workflow start options as appropriate. - // Call temporalNexus.startWorkflow() to actually start the Workflow from inside the - // WorkflowRunOperationHandler's delegate function. - async (ctx, input: HelloInput) => { - return await temporalNexus.startWorkflow(ctx, helloWorkflow, { + hello: new temporalNexus.TemporalOperationHandler({ + start: async (ctx, client, input) => + client.startWorkflow(helloWorkflow, { args: [input], // Workflow IDs should typically be business-meaningful IDs and are used to dedupe workflow starts. - // For this example, the workflow handles the greeting request for a given person and language pair. - workflowId: workflowIdForHello(input), + workflowId: `hello-${input.name}-${input.language}`, // Task queue defaults to the task queue this Operation is handled on. - }); - }, - ), + }), + }), }); ``` - Workflow IDs should typically be business-meaningful IDs and are used to dedupe Workflow starts. In general, the ID should be passed in the Operation input as part of the Nexus Service contract. @@ -271,7 +233,19 @@ In general, the ID should be passed in the Operation input as part of the Nexus ::: -### Register your Nexus Service handler in a Worker +#### Map a Nexus Operation input to multiple Workflow arguments + +A Nexus Operation can only take one input parameter. To start a Workflow that takes several, spread the pieces of the +input across the `args` array: + +```ts +client.startWorkflow(helloWorkflow, { + args: [input.name, input.language], + workflowId: `hello-${input.name}-${input.language}`, +}); +``` + +### Register a Nexus Service in a Worker After developing an asynchronous Nexus Operation handler to start a Workflow, the next step is to register your Nexus Service handler in a Worker. @@ -335,16 +309,27 @@ Refer to the [complete TypeScript sample](https://github.com/temporalio/samples- - [nexus-hello/src/caller/worker.ts](https://github.com/temporalio/samples-typescript/blob/main/nexus-hello/src/caller/worker.ts) shows how to register the caller Workflow in a Worker and run the Worker. - [nexus-hello/src/starter.ts](https://github.com/temporalio/samples-typescript/blob/main/nexus-hello/src/starter.ts) shows how to use a Temporal Client to execute the sample caller Workflow. -## Exceptions in Nexus operations {/* #exceptions-in-nexus-operations */} -Temporal provides general guidance on [Errors in Nexus operations](/references/failures#errors-in-nexus-operations). -In TypeScript, there are three Nexus-specific exception classes: +## Make Nexus calls across Namespaces with a development Server {/* #nexus-calls-across-namespaces-dev-server */} + +Follow the steps below to run the Nexus handler Worker, the Nexus caller Worker, and the starter app. + +1. Run `npm run start.service` to start the Worker that will be serving the Nexus Operation handlers and its associated +Workflows. That Worker connects to the `my-target-namespace` namespace. + +2. In another shell, run `npm run start.caller` to start the Worker that will be serving the Caller Workflows. That +Worker connects to the `my-caller-namespace` namespace. -- `nexus-rpc`'s [`OperationError`](https://nexus-rpc.github.io/sdk-typescript/classes/OperationError.html): this is the exception type you should throw in a Nexus operation to indicate that it has failed according to its own application logic and should not be retried. -- `nexus-rpc`'s [`HandlerError`](https://nexus-rpc.github.io/sdk-typescript/classes/HandlerError.html): you can throw this exception type in a Nexus operation with a specific [HandlerErrorType](https://nexus-rpc.github.io/sdk-typescript/types/HandlerErrorType.html). The error will be marked as either retryable or non-retryable according to the type, following the [Nexus spec](https://github.com/nexus-rpc/api/blob/main/SPEC.md#predefined-handler-errors). The non-retryable handler error types are `BAD_REQUEST`, `UNAUTHENTICATED`, `UNAUTHORIZED`, `NOT_FOUND`, `NOT_IMPLEMENTED`; the retryable types are `RESOURCE_EXHAUSTED`, `INTERNAL`, `UNAVAILABLE`, `UPSTREAM_TIMEOUT`. -- `@temporalio/nexus`'s [`NexusOperationFailure`](https://typescript.temporal.io/api/classes/common.NexusOperationFailure): this is the error thrown inside a Workflow when a Nexus operation fails for any reason. Use the `cause` attribute on the exception to access the cause chain. +3. In a third shell, `npm run workflow` to start an instance of the caller Workflows. -## Canceling a Nexus Operation {/* #canceling-a-nexus-operation */} +Example output: + +```bash +Echo message: This message is from the client +Hello message: Hello, Temporal! +``` + +### Canceling a Nexus Operation {/* #canceling-a-nexus-operation */} Nexus Operations, just like other cancellable APIs provided by the `@temporalio/workflow` package, execute within Cancellation Scopes. Requesting cancellation of a Cancellation Scope results in requesting cancellation for all cancellable operations owned by that scope. @@ -357,9 +342,11 @@ An example demonstrating this can be found at our [nexus cancellation sample](ht Only asynchronous operations can be canceled in Nexus, since cancellation is sent using an operation token. The Workflow or other resources backing the operation may choose to ignore the cancellation request. -Once the caller Workflow completes, the caller's Nexus Machinery will not make any further attempts to cancel operations that are still running. -It's okay to leave operations running in some use cases. -To ensure cancellations are delivered, wait for all pending operations to finish before exiting the Workflow. +Once the caller Workflow completes, the caller's Nexus Machinery stops attempting to cancel operations that have not yet +been canceled, letting them run to completion. + +It's okay to leave operations running in some use cases. To ensure cancellations are delivered, wait for all pending +operations to deliver their cancellation requests before exiting the Workflow. ## Make Nexus calls across Namespaces in Temporal Cloud {/* #nexus-calls-across-namespaces-temporal-cloud */} @@ -413,18 +400,18 @@ temporal cloud namespace create \ tcld login tcld namespace create \ - --namespace \ - --cloud-provider aws \ - --region us-west-2 \ - --ca-certificate-file 'path/to/your/ca.pem' \ - --retention-days 1 + --namespace \ + --cloud-provider aws \ + --region us-west-2 \ + --ca-certificate-file 'path/to/your/ca.pem' \ + --retention-days 1 tcld namespace create \ - --namespace \ - --cloud-provider aws \ - --region us-west-2 \ - --ca-certificate-file 'path/to/your/ca.pem' \ - --retention-days 1 + --namespace \ + --cloud-provider aws \ + --region us-west-2 \ + --ca-certificate-file 'path/to/your/ca.pem' \ + --retention-days 1 ``` @@ -507,36 +494,6 @@ For **synchronous Nexus Operations** the following are reported in the caller's ::: -### OpenTelemetry - -The `@temporalio/interceptors-opentelemetry` package supports Nexus Operations, providing automatic trace context propagation across Nexus boundaries from the caller Workflow to the handler. - -The easiest way to enable it is with the `OpenTelemetryPlugin`, which auto-registers Nexus interceptors alongside Activity and Workflow interceptors: - -```ts -import { OpenTelemetryPlugin } from '@temporalio/interceptors-opentelemetry'; - -const plugin = new OpenTelemetryPlugin({ - resource: myResource, - spanProcessor: mySpanProcessor, -}); - -const worker = await Worker.create({ - // ... - plugins: [plugin], - nexusServices: [myServiceHandler], -}); -``` - -The plugin creates the following spans: - -- **Caller side:** `StartNexusOperation:service/operation` β€” created when the caller Workflow starts a Nexus Operation. -- **Handler side:** `RunStartNexusOperation:service/operation` and `RunCancelNexusOperation:service/operation` β€” created when the handler processes the operation. These spans are children of the caller span, linked via trace context propagated in Nexus request headers. - -See the [interceptors-opentelemetry sample](https://github.com/temporalio/samples-typescript/tree/main/interceptors-opentelemetry) for a complete example. - -For custom interceptor logic beyond tracing (for example, logging, authorization), see [Nexus interceptor registration](/develop/typescript/workers/interceptors#nexus-interceptor-registration). - ## Learn more - Read the high-level description of the [Temporal Nexus feature](/evaluate/nexus) and watch the [Nexus keynote and demo](https://youtu.be/qqc2vsv1mrU?feature=shared&t=2082). diff --git a/docs/encyclopedia/nexus/nexus-code-generator.mdx b/docs/encyclopedia/nexus/nexus-code-generator.mdx new file mode 100644 index 0000000000..cd64381d63 --- /dev/null +++ b/docs/encyclopedia/nexus/nexus-code-generator.mdx @@ -0,0 +1,55 @@ +--- +id: nexus-code-generator +title: Nexus Code Generator +sidebar_label: Nexus Code Generator +description: The Nexus Code Generator turns one schema into typed models, runtime validators, and Nexus Service definitions for Go, Java, Python, and TypeScript. +toc_max_heading_level: 4 +slug: /nexus/code-generator +tags: + - Nexus + - Concepts +--- + +import { ReleaseNoteHeader } from '@site/src/components'; + + + APIs are experimental and may be subject to backwards-incompatible changes. + + +A [Nexus Service](/nexus/services) is called across a team boundary, often by a caller written in a different language than the handler implementation and deployed on its own schedule. +When each side hand-writes its own request and response types, the two copies drift, and nothing catches it until a call fails. + +[`nexgen`](https://github.com/temporalio/nexgen) generates client code for Go, Java, Python, and TypeScript from a schema file that defines the contract. The schema's types are modeled with [JSON Schema 2020-12](https://json-schema.org). +Both sides can then use code generated from the same file, which gives data validation and type safety across the languages and helps prevent drift. + +For each type it emits: + +- **Typed models** β€” an idiomatic struct, class, interface, or dataclass, with doc comments carried over from the schema. +- **A runtime validator**, automatically applied when a value is parsed off the wire and again when it is serialized onto it. +- **A [Nexus Service](/nexus/services) definition**, for a file that declares Services. The handler implements it; the caller uses it to invoke Operations. + +## How it works + +You write the contract once, as a JSON or YAML definition file, and run `nexgen` against it. +The generator emits contract code in Go, Java, Python, or TypeScript as requested. + +Both sides use that generated code: the handler implements the Service, and the caller invokes its Operations. +Because both were generated from the same file, they agree on the contract by construction, and the generated validators enforce it at runtime on every payload. + +This is what makes a Nexus Service polyglot. +A Python handler and a Go caller never share code β€” they share a definition file. +Generate from it in each language and they interoperate, with no coordination between the teams beyond the contract itself. + +## Data validation + +The generated validators check every payload against the contract, when a value is parsed off the wire and again when it is serialized onto it. +Bad data is rejected at the boundary instead of reaching your Workflow or Activity. + +Failures aggregate into a single error listing every violation, each naming the offending field and the constraint it broke. +A handler maps that to a `BAD_REQUEST` [Nexus handler error](/nexus/error-handling), so a malformed request tells the caller everything that was wrong in one response. + +A value is validated identically in every language, which is what lets a caller and a handler written in different ones trust the same contract. +Keeping that promise is why the supported schema subset is deliberately strict: anything ambiguous, or anything that cannot be expressed the same way everywhere, is rejected at generation time rather than becoming code that validates differently in one language than another. + +Three numeric and timestamp edge cases do not yet behave the same way in every language, covering negative zero, very large fractional integers, and nanosecond precision in Python. +See [Known cross-language divergences](https://github.com/temporalio/nexgen#known-cross-language-divergences) in the `nexgen` README for what each language does. diff --git a/docs/encyclopedia/nexus/nexus-services.mdx b/docs/encyclopedia/nexus/nexus-services.mdx index 6ba1a1e9a6..db2c6a0a35 100644 --- a/docs/encyclopedia/nexus/nexus-services.mdx +++ b/docs/encyclopedia/nexus/nexus-services.mdx @@ -21,3 +21,5 @@ Multiple Services can run in the same Worker. Services typically run alongside the Workflows they abstract, or in a dedicated router Worker using the [router-queue pattern](/nexus/patterns#router-queue-pattern). Callers reference a Service by name when executing a Nexus Operation. + +You can hand-write a Service definition, or generate it from a schema file with the [Nexus Code Generator](/nexus/code-generator), which emits the definition along with typed models and runtime validators for each language. diff --git a/docs/encyclopedia/nexus/nexus-standalone-activity.mdx b/docs/encyclopedia/nexus/nexus-standalone-activity.mdx new file mode 100644 index 0000000000..42bbdcb44f --- /dev/null +++ b/docs/encyclopedia/nexus/nexus-standalone-activity.mdx @@ -0,0 +1,54 @@ +--- +id: nexus-standalone-activity +title: Nexus Standalone Activity +sidebar_label: Nexus Standalone Activity +description: Back a Nexus Operation with a Standalone Activity when the work is a single durable step, with no Workflow wrapped around it. +toc_max_heading_level: 4 +slug: /nexus/standalone-activity +tags: + - Nexus + - Concepts +--- + +import { ReleaseNoteHeader } from '@site/src/components'; + + + APIs are experimental and may be subject to backwards-incompatible changes. + + +:::note Not the same as a Standalone Nexus Operation + +The two names are close and describe opposite ends of the call. +A [Standalone Nexus Operation](/standalone-nexus-operation) is about the **caller**: a Client starts an Operation directly, with no caller Workflow around it. +A Nexus Standalone Activity is about the **handler**: an Operation is backed by a single Activity, with no Workflow behind it. +They are independent choices, and either can be used without the other. + +::: + +An Activity-backed [Nexus Operation](/nexus/operations) runs a [Standalone Activity](/standalone-activity) and completes when that Activity returns. +Use it when the work behind an Operation is one durable step rather than a process: calling an external API, running a computation, writing to another system. + +Two things combine to make this happen. +The [Activity](/activities) supplies durability β€” retries on the policy you set, timeouts you control, and a record of every attempt. +The Operation supplies a typed contract and a [Namespace](/namespaces) boundary, so another team can call it without sharing your code, your deployment, or write access to your Namespace. + +Because the Activity carries the durability, no Workflow is needed behind the Operation. +A Workflow wrapping a single Activity costs two [Billable Actions](/cloud/actions-usage#actions-in-workflows) in Temporal Cloud β€” one to start the Workflow, one to start the Activity β€” where a Standalone Activity costs one. +Retries and heartbeats are billed the same way in either shape. + +## Required options + +Starting an Activity this way needs values a Workflow-called Activity does not, because there is no parent Workflow to supply them: + +- **An Activity Id**, unique within the Namespace. Deriving it from the Nexus request Id makes the start idempotent, so a retried request targets the same Activity Execution instead of sending a second notification or charge. +- **A timeout.** At least one of start-to-close or schedule-to-close. + +The Task Queue is optional and defaults to the one the Operation is running on. Set it explicitly to run the Activity on its own Worker fleet. + +## Cancellation + +An Activity is not interrupted by a cancellation request the way a Workflow is. +The Worker only learns about it on the next Heartbeat, so an Activity that never Heartbeats runs until it completes or times out. +Nothing here is Nexus-specific β€” see [Activity Cancellation](/activity-execution#cancellation). + +Back an Operation with a **Workflow** instead when the work has more than one step, needs to wait for something, needs to receive [messages](/sending-messages), or needs durable intermediate state. diff --git a/docs/encyclopedia/nexus/temporal-operation-handler.mdx b/docs/encyclopedia/nexus/temporal-operation-handler.mdx new file mode 100644 index 0000000000..86ac65b6ce --- /dev/null +++ b/docs/encyclopedia/nexus/temporal-operation-handler.mdx @@ -0,0 +1,36 @@ +--- +id: temporal-operation-handler +title: Temporal Operation Handler +sidebar_label: Temporal Operation Handler +description: The Temporal Operation Handler is a single handler type that backs a Nexus Operation with a Workflow, an Update, or an Activity, and links every Execution back to the caller. +toc_max_heading_level: 4 +slug: /nexus/temporal-operation-handler +tags: + - Nexus + - Concepts +--- + +import { ReleaseNoteHeader } from '@site/src/components'; + + + APIs are experimental and may be subject to backwards-incompatible changes. + + +Temporal has unified the Workflow handler and the synchronous operation handler into a single handler, and added the ability to back an Operation with a [Standalone Activity](/nexus/standalone-activity). + +What runs behind an Operation remains private to the handler, so you can change it later without touching the contract or any caller. + +## The Nexus-aware Client + +The Operation handler receives a context, the Operation input, and a Client. + +That Client is not an ordinary Temporal Client. +It propagates [bidirectional links](/nexus/execution-debugging#bi-directional-linking) and request Ids on every call, so caller-side and handler-side Executions are connected in the UI and in [Event History](/encyclopedia/event-history) without wiring anything. +Constructing your own Client inside a handler works, but the Executions it starts are not linked back to the caller. + +It exposes two kinds of call: + +- **Async backings**, at most one per Operation invocation. These determine what the Operation *is*, and their result reaches the caller through the Nexus completion callback. Starting a Workflow, starting an Activity, and starting a Workflow Update are all async backings. +- **Sync messaging**, as many as you need. Signals and Signal-with-Start take effect during the handler call and do not require an async backing. + +Deriving the backing Execution's Id from the Nexus request Id keeps a retried start request targeting the same Execution instead of creating a second one. diff --git a/sidebars.js b/sidebars.js index ba42163641..f310c88f7e 100644 --- a/sidebars.js +++ b/sidebars.js @@ -2111,7 +2111,6 @@ module.exports = { items: [ 'encyclopedia/nexus/nexus-services', 'encyclopedia/nexus/nexus-operations', - 'encyclopedia/nexus/standalone-nexus-operation', 'encyclopedia/nexus/nexus-endpoints', 'encyclopedia/nexus/nexus-registry', 'encyclopedia/nexus/nexus-patterns', @@ -2119,6 +2118,11 @@ module.exports = { 'encyclopedia/nexus/nexus-execution-debugging', 'encyclopedia/nexus/nexus-error-handling', 'encyclopedia/nexus/nexus-metrics', + // Pre-release features, kept at the bottom of the section. + 'encyclopedia/nexus/temporal-operation-handler', + 'encyclopedia/nexus/nexus-code-generator', + 'encyclopedia/nexus/standalone-nexus-operation', + 'encyclopedia/nexus/nexus-standalone-activity', ], }, {