Skip to main content

Temporal Operation Handler

View Markdown
caution

The Temporal Operation Handler is pre-release. TemporalOperationHandler is marked experimental in the SDKs and may change in backwards-incompatible ways.

A Nexus Service publishes Operations that other teams call across Namespace boundaries. TemporalOperationHandler is how you implement those Operations.

It is a single handler type that can back an Operation with any Temporal primitive — start a Workflow, run an Update, start an Activity — or complete the Operation inline with no backing Execution at all. Whichever you choose, the handler is the same shape, and every Execution it touches is connected back to the caller automatically.

What you can do with it

Back an Operation with whichever primitive fits the work. A multi-step process is a Workflow. A single durable step is an Activity, with no Workflow wrapped around it. A change to something already running is an Update. The caller sees the same Operation contract either way, and you can change your choice later without touching callers.

Combine messaging and a backing in one handler. A handler can Signal a running Workflow to unblock it and then return a different Execution's result for the caller to await. These are not separate handler types you pick between; they compose inside one start handler.

Get observability across the Namespace boundary without wiring it. The Client handed to your handler propagates bidirectional links and request Ids on every call it makes. The caller-side and handler-side Executions are connected in the UI and in Event History, so a single trace crosses the boundary between two teams' Namespaces.

Stay idempotent through retries. The server retries Nexus start requests, and the request Id travels with them. Deriving the backing Execution's Id from it means a retry targets the same Execution rather than starting a second one.

Cancel through the same handler. The Operation token records which kind of Execution backs the Operation, so a cancellation request reaches the right place. The default behavior is usually what you want, and each kind can be overridden when it is not.

Grow a handler without rewriting it. An Operation that starts out completing inline can later gain an async backing, or pick up a Signal, without changing handler type or breaking its contract.

The Nexus-aware Client

A TemporalOperationHandler start handler receives three things: a context, a Client, and the Operation input.

The Client is what makes the linking automatic, so prefer it over constructing your own inside a handler. Reaching for your own Client still works, but messages sent that way are not connected back to the caller.

The Client exposes two kinds of call, and the distinction shapes how you write the handler. Each SDK spells them differently; the examples below show the exact call per language.

Async backings — at most one per Operation invocation. These determine what the Operation is, and their result is delivered to the caller through the Nexus completion callback when the underlying Execution finishes.

  • Start a Workflow — the Operation completes when the Workflow returns
  • Update a Workflow — the Operation completes when the Update completes
  • Start an Activity — the Operation completes when the Activity returns; see Nexus Standalone Activity

Sync messaging — as many as you need. Reach these through the underlying Temporal Client that the Nexus-aware Client exposes. They take effect during the handler call, still get link propagation, and do not require an async backing.

  • Signal — delivered during the handler call to a Workflow that is already running
  • Signal-with-Start — delivers the Signal, starting the Workflow first if it is not already running

A handler that only sends messages returns a synchronous result, and the Operation completes immediately.

Write an Operation handler

The examples below use a Nexus Service with a startGreeting Operation backed by a Workflow, an updateShippingAddress Operation backed by an Update, a cancelOrder Operation that sends a Signal, and a greet Operation backed by an Activity. Click the language tabs to see example code in each language - Go, Java, .NET, Python, and TypeScript.

Back an Operation with a Workflow

Call startWorkflow on the Client and return its result. The Operation completes when the Workflow returns, delivering the Workflow's return value to the caller.

op := temporalnexus.MustNewTemporalOperation(
temporalnexus.TemporalOperationOptions[GreetingInput, GreetingOutput]{
Name: "startGreeting",
Start: func(
ctx context.Context,
nc temporalnexus.NexusClient,
input GreetingInput,
_ temporalnexus.StartTemporalOperationOptions,
) (temporalnexus.TemporalOperationResult[GreetingOutput], error) {
return temporalnexus.StartWorkflow(ctx, nc,
client.StartWorkflowOptions{ID: "greeting-" + input.Name},
GreetingWorkflow, input)
},
})

Go exposes the start calls as package-level functions taking the Client, rather than as methods on it, because Go does not allow generic methods on a non-generic struct.

Back an Operation with an Update

Back an Operation with an Update when it changes something already running. The target Workflow has to exist already, and the Operation completes when the Update completes.

op := temporalnexus.MustNewTemporalOperation(
temporalnexus.TemporalOperationOptions[UpdateAddressInput, AddressOutput]{
Name: "updateShippingAddress",
Start: func(
ctx context.Context,
nc temporalnexus.NexusClient,
input UpdateAddressInput,
_ temporalnexus.StartTemporalOperationOptions,
) (temporalnexus.TemporalOperationResult[AddressOutput], error) {
return temporalnexus.StartUpdateWorkflow[AddressOutput](ctx, nc,
client.UpdateWorkflowOptions{
WorkflowID: "order-" + input.OrderID,
UpdateName: "updateShippingAddress",
Args: []any{input},
WaitForStage: client.WorkflowUpdateStageAccepted,
})
},
})

Three constraints apply to Update-backed Operations, and the first two fail the Operation rather than degrading:

  • Only the accepted stage is supported. A Nexus Operation can only back an asynchronous Update, so the wait-for-stage must be "accepted". Go, Java, and .NET require you to set it and fail the Operation if it is anything else. Python and TypeScript set it for you.
  • A callback URL is required. The Operation is how the Update's result reaches the caller, so a caller that provided no callback URL gets a BAD_REQUEST handler error.
  • The Update Id defaults to the Nexus request Id. Leaving it unset is what you usually want: a retried start request carries the same request Id, so it targets the same Update rather than running a second one.

The result is async in the normal case, carrying an update-workflow Operation token. If the Update has already completed by the time it is accepted — a retried request with the same Update Id, or an Update that completes immediately — you get a synchronous result instead.

Send a Signal from an Operation

Reach the Workflow Client through the injected Client, send the message, and return a synchronous result. The Operation completes during the handler call, and the Signal is linked back to the caller.

op := temporalnexus.MustNewTemporalOperation(
temporalnexus.TemporalOperationOptions[CancelOrderInput, nexus.NoValue]{
Name: "cancelOrder",
Start: func(
ctx context.Context,
nc temporalnexus.NexusClient,
input CancelOrderInput,
_ temporalnexus.StartTemporalOperationOptions,
) (temporalnexus.TemporalOperationResult[nexus.NoValue], error) {
err := nc.GetWorkflowClient().SignalWorkflow(
ctx, "order-"+input.OrderID, "", "requestCancellation", input)
if err != nil {
return temporalnexus.TemporalOperationResult[nexus.NoValue]{}, err
}
return temporalnexus.NewSyncResult[nexus.NoValue](nil), nil
},
})

The same Client also offers Signal-with-Start, and a handler may send several messages before returning.

Back an Operation with an Activity

Call startActivity when the work is a single durable step. The Activity runs with no parent Workflow, so the options require an Activity Id and at least one timeout. Java also requires a Task Queue; the other SDKs default it to the Task Queue the Operation is running on. See Nexus Standalone Activity.

op := temporalnexus.MustNewTemporalOperation(
temporalnexus.TemporalOperationOptions[GreetingInput, GreetingOutput]{
Name: "greet",
Start: func(
ctx context.Context,
nc temporalnexus.NexusClient,
input GreetingInput,
opts temporalnexus.StartTemporalOperationOptions,
) (temporalnexus.TemporalOperationResult[GreetingOutput], error) {
return temporalnexus.StartActivity(ctx, nc, client.StartActivityOptions{
ID: "greet-" + opts.RequestID,
TaskQueue: TaskQueueName,
StartToCloseTimeout: 10 * time.Second,
}, Greet, input)
},
})

Coming from the earlier handler APIs

Skip this section if you are new to Nexus.

Earlier SDK versions had a separate helper per pattern. Existing handlers will keep working, there is no forced migration.

If you usedUse instead
The Workflow-run helper (WorkflowRunOperation, NewWorkflowRunOperation, @workflow_run_operation)TemporalOperationHandler with a Workflow backing
The synchronous handler (OperationHandler.sync, nexus.NewSyncOperation, @sync_operation)TemporalOperationHandler returning a sync result
A Workflow wrapping a single ActivityTemporalOperationHandler with an Activity backing
A Temporal Client fetched inside a handlerThe Client injected into the start handler

Two things improve when you migrate. Messages and Executions get bidirectional linking, which hand-fetched Clients do not produce. And one handler type covers every case, so an Operation can change what backs it without changing shape.

Migrating a Workflow-backed Operation

The earlier helper reached the Client through the Operation context and returned a Workflow handle or method reference, rather than being handed a Client and returning an Operation result:

op := temporalnexus.NewWorkflowRunOperation(
"startGreeting",
GreetingWorkflow,
func(ctx context.Context, input GreetingInput, opts nexus.StartOperationOptions) (client.StartWorkflowOptions, error) {
return client.StartWorkflowOptions{
ID: "greeting-" + input.Name,
}, nil
})

Replace it with Back an Operation with a Workflow.

Migrating a synchronous Operation

A messaging Operation used to be a synchronous handler that fetched its own Client, which is why those messages produced no links:

// nexus.NewSyncOperation comes from the separate nexus-rpc SDK, not from temporalnexus.
op := nexus.NewSyncOperation("cancelOrder",
func(ctx context.Context, input CancelOrderInput, o nexus.StartOperationOptions) (nexus.NoValue, error) {
c := temporalnexus.GetClient(ctx)
return nil, c.SignalWorkflow(ctx, "order-"+input.OrderID, "", "requestCancellation", input)
})

Replace it with Send a Signal from an Operation.

RESOURCES