diff --git a/nexus-messaging/src/callerpattern/README.md b/nexus-messaging/src/callerpattern/README.md index 89ea339bf..16aae3753 100644 --- a/nexus-messaging/src/callerpattern/README.md +++ b/nexus-messaging/src/callerpattern/README.md @@ -1,9 +1,9 @@ ## Caller pattern The handler worker starts a `GreetingWorkflow` for a user ID at boot. -`NexusGreetingService` holds that ID and routes every Nexus operation to it. +`nexusGreetingServiceHandler` derives the workflow ID and routes every Nexus operation to it. The caller's input does not have that workflow ID as the caller doesn't know it -- but the caller sends in the User ID, -and `NexusGreetingService` knows how to get the desired workflow ID from that User ID (via the `GreetingWorkflow_for_` prefix). +and `nexusGreetingServiceHandler` knows how to get the desired workflow ID from that User ID (via the `GreetingWorkflow_for_` prefix). The handler worker uses the same prefix to generate a workflow ID from a user ID when it launches the workflow. @@ -16,19 +16,24 @@ The caller workflow: ### Running -Start a Temporal server: +Start a compatible Temporal dev server with Workflow Update callbacks enabled: ```bash -temporal server start-dev +./temporal server start-dev \ + --dynamic-config-value history.enableCHASMCallbacks=true \ + --dynamic-config-value history.enableUpdateCallbacks=true \ + --dynamic-config-value history.enableCHASMSignalBacklinks=true \ + --namespace nexus-messaging-handler-namespace \ + --namespace nexus-messaging-caller-namespace ``` -Create the namespaces and Nexus endpoint: +This sample requires a Temporal dev-server build that supports Workflow Update callbacks. Download the compatible +binary from the [Temporal CLI pre-release instructions](https://docs.temporal.io/standalone-nexus-operation#temporal-cli-support). -```bash -temporal operator namespace create --namespace nexus-messaging-handler-namespace -temporal operator namespace create --namespace nexus-messaging-caller-namespace +Create the Nexus endpoint: -temporal operator nexus endpoint create \ +```bash +./temporal operator nexus endpoint create \ --name nexus-messaging-nexus-endpoint \ --target-namespace nexus-messaging-handler-namespace \ --target-task-queue nexus-messaging-handler-task-queue diff --git a/nexus-messaging/src/callerpattern/service/handler.ts b/nexus-messaging/src/callerpattern/service/handler.ts index 837caedc9..fb28c3976 100644 --- a/nexus-messaging/src/callerpattern/service/handler.ts +++ b/nexus-messaging/src/callerpattern/service/handler.ts @@ -8,27 +8,34 @@ function workflowIdForUser(userId: string): string { } 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); + }, + }), - getLanguage: async (ctx, input: GetLanguageInput) => { - const client = temporalNexus.getClient(); - const handle = client.workflow.getHandle(workflowIdForUser(input.userId)); - return await handle.query(getLanguageQuery); - }, + getLanguage: new temporalNexus.TemporalOperationHandler({ + async start(_ctx, client, input: GetLanguageInput) { + const handle = client.client.workflow.getHandle(workflowIdForUser(input.userId)); + const result = await handle.query(getLanguageQuery); + return temporalNexus.TemporalOperationResult.sync(result); + }, + }), - setLanguage: async (ctx, input: SetLanguageInput) => { - const client = temporalNexus.getClient(); - const handle = client.workflow.getHandle(workflowIdForUser(input.userId)); - return await handle.executeUpdate(setLanguageUpdate, { args: [input.language] }); - }, + setLanguage: new temporalNexus.TemporalOperationHandler({ + async start(_ctx, client, input: SetLanguageInput) { + const handle = client.getWorkflowHandle(workflowIdForUser(input.userId)); + return await handle.update(setLanguageUpdate, { args: [input.language] }); + }, + }), - approve: async (ctx, input: ApproveInput) => { - const client = temporalNexus.getClient(); - const handle = client.workflow.getHandle(workflowIdForUser(input.userId)); - await handle.signal(approveSignal); - }, + approve: new temporalNexus.TemporalOperationHandler({ + async start(_ctx, client, input: ApproveInput) { + const handle = client.getWorkflowHandle(workflowIdForUser(input.userId)); + await handle.signal(approveSignal); + return temporalNexus.TemporalOperationResult.sync(undefined); + }, + }), }); diff --git a/nexus-messaging/src/ondemandpattern/README.md b/nexus-messaging/src/ondemandpattern/README.md index 32eadeef5..4e35d9e9f 100644 --- a/nexus-messaging/src/ondemandpattern/README.md +++ b/nexus-messaging/src/ondemandpattern/README.md @@ -15,19 +15,24 @@ The caller workflow: ### Running -Start a Temporal server: +Start a compatible Temporal dev server with Workflow Update callbacks enabled: ```bash -temporal server start-dev +./temporal server start-dev \ + --dynamic-config-value history.enableCHASMCallbacks=true \ + --dynamic-config-value history.enableUpdateCallbacks=true \ + --dynamic-config-value history.enableCHASMSignalBacklinks=true \ + --namespace nexus-messaging-handler-namespace \ + --namespace nexus-messaging-caller-namespace ``` -Create the namespaces and Nexus endpoint: +This sample requires a Temporal dev-server build that supports Workflow Update callbacks. Download the compatible +binary from the [Temporal CLI pre-release instructions](https://docs.temporal.io/standalone-nexus-operation#temporal-cli-support). -```bash -temporal operator namespace create --namespace nexus-messaging-handler-namespace -temporal operator namespace create --namespace nexus-messaging-caller-namespace +Create the Nexus endpoint: -temporal operator nexus endpoint create \ +```bash +./temporal operator nexus endpoint create \ --name nexus-messaging-nexus-endpoint \ --target-namespace nexus-messaging-handler-namespace \ --target-task-queue nexus-messaging-handler-task-queue diff --git a/nexus-messaging/src/ondemandpattern/service/handler.ts b/nexus-messaging/src/ondemandpattern/service/handler.ts index d83145ed7..aae3f67c5 100644 --- a/nexus-messaging/src/ondemandpattern/service/handler.ts +++ b/nexus-messaging/src/ondemandpattern/service/handler.ts @@ -27,27 +27,34 @@ export const nexusRemoteGreetingServiceHandler = nexus.serviceHandler(nexusRemot }, ), - getLanguages: async (ctx, input: GetLanguagesInput) => { - const client = temporalNexus.getClient(); - const handle = client.workflow.getHandle(getWorkflowId(input.userId)); - return await handle.query(getLanguagesQuery); - }, + getLanguages: new temporalNexus.TemporalOperationHandler({ + async start(_ctx, client, input: GetLanguagesInput) { + const handle = client.client.workflow.getHandle(getWorkflowId(input.userId)); + const result = await handle.query(getLanguagesQuery); + return temporalNexus.TemporalOperationResult.sync(result); + }, + }), - getLanguage: async (ctx, input: GetLanguageInput) => { - const client = temporalNexus.getClient(); - const handle = client.workflow.getHandle(getWorkflowId(input.userId)); - return await handle.query(getLanguageQuery); - }, + getLanguage: new temporalNexus.TemporalOperationHandler({ + async start(_ctx, client, input: GetLanguageInput) { + const handle = client.client.workflow.getHandle(getWorkflowId(input.userId)); + const result = await handle.query(getLanguageQuery); + return temporalNexus.TemporalOperationResult.sync(result); + }, + }), - setLanguage: async (ctx, input: SetLanguageInput) => { - const client = temporalNexus.getClient(); - const handle = client.workflow.getHandle(getWorkflowId(input.userId)); - return await handle.executeUpdate(setLanguageUpdate, { args: [input.language] }); - }, + setLanguage: new temporalNexus.TemporalOperationHandler({ + async start(_ctx, client, input: SetLanguageInput) { + const handle = client.getWorkflowHandle(getWorkflowId(input.userId)); + return await handle.update(setLanguageUpdate, { args: [input.language] }); + }, + }), - approve: async (ctx, input: ApproveInput) => { - const client = temporalNexus.getClient(); - const handle = client.workflow.getHandle(getWorkflowId(input.userId)); - await handle.signal(approveSignal); - }, + approve: new temporalNexus.TemporalOperationHandler({ + async start(_ctx, client, input: ApproveInput) { + const handle = client.getWorkflowHandle(getWorkflowId(input.userId)); + await handle.signal(approveSignal); + return temporalNexus.TemporalOperationResult.sync(undefined); + }, + }), });