|
| 1 | +namespace TemporalioSamples.NexusMessaging.CallerPattern.Handler; |
| 2 | + |
| 3 | +using NexusRpc.Handlers; |
| 4 | +using Temporalio.Nexus; |
| 5 | +using TemporalioSamples.NexusMessaging.CallerPattern; |
| 6 | +using TemporalioSamples.NexusMessaging.Common; |
| 7 | + |
| 8 | +// Entity pattern: the handler worker pre-starts a GreetingWorkflow per user at boot time. |
| 9 | +// This service routes each Nexus operation to that existing workflow by deriving the |
| 10 | +// workflow ID from the caller-supplied UserId. |
| 11 | +[NexusServiceHandler(typeof(INexusGreetingService))] |
| 12 | +public class NexusGreetingService |
| 13 | +{ |
| 14 | + // OperationHandler.Sync means the result is returned inline to the Nexus caller |
| 15 | + // (as opposed to WorkflowRunOperationHandler, which returns an async operation token). |
| 16 | + // The lambda may still be async internally. |
| 17 | + |
| 18 | + // Query: read-only, no state mutation — uses workflow query |
| 19 | + [NexusOperationHandler] |
| 20 | + public IOperationHandler<INexusGreetingService.GetLanguagesInput, INexusGreetingService.GetLanguagesOutput> GetLanguages() => |
| 21 | + OperationHandler.Sync<INexusGreetingService.GetLanguagesInput, INexusGreetingService.GetLanguagesOutput>( |
| 22 | + async (ctx, input) => |
| 23 | + { |
| 24 | + // Access the Temporal client from the Nexus operation context |
| 25 | + var client = NexusOperationExecutionContext.Current.TemporalClient; |
| 26 | + var handle = client.GetWorkflowHandle<GreetingWorkflow>(WorkflowIdForUser(input.UserId)); |
| 27 | + return await handle.QueryAsync(wf => wf.QueryLanguages(input.IncludeUnsupported)); |
| 28 | + }); |
| 29 | + |
| 30 | + // Query: read-only — returns the workflow's current language |
| 31 | + [NexusOperationHandler] |
| 32 | + public IOperationHandler<INexusGreetingService.GetLanguageInput, Language> GetLanguage() => |
| 33 | + OperationHandler.Sync<INexusGreetingService.GetLanguageInput, Language>( |
| 34 | + async (ctx, input) => |
| 35 | + { |
| 36 | + var client = NexusOperationExecutionContext.Current.TemporalClient; |
| 37 | + var handle = client.GetWorkflowHandle<GreetingWorkflow>(WorkflowIdForUser(input.UserId)); |
| 38 | + return await handle.QueryAsync(wf => wf.QueryLanguage()); |
| 39 | + }); |
| 40 | + |
| 41 | + // Update: mutates state and returns the previous value — uses workflow update |
| 42 | + [NexusOperationHandler] |
| 43 | + public IOperationHandler<INexusGreetingService.SetLanguageInput, Language> SetLanguage() => |
| 44 | + OperationHandler.Sync<INexusGreetingService.SetLanguageInput, Language>( |
| 45 | + async (ctx, input) => |
| 46 | + { |
| 47 | + var client = NexusOperationExecutionContext.Current.TemporalClient; |
| 48 | + var handle = client.GetWorkflowHandle<GreetingWorkflow>(WorkflowIdForUser(input.UserId)); |
| 49 | + return await handle.ExecuteUpdateAsync(wf => wf.SetLanguageAsync(input.Language)); |
| 50 | + }); |
| 51 | + |
| 52 | + // Signal: fire-and-forget, no return value needed — uses workflow signal |
| 53 | + [NexusOperationHandler] |
| 54 | + public IOperationHandler<INexusGreetingService.ApproveInput, NoValue> Approve() => |
| 55 | + OperationHandler.Sync<INexusGreetingService.ApproveInput, NoValue>( |
| 56 | + async (ctx, input) => |
| 57 | + { |
| 58 | + var client = NexusOperationExecutionContext.Current.TemporalClient; |
| 59 | + var handle = client.GetWorkflowHandle<GreetingWorkflow>(WorkflowIdForUser(input.UserId)); |
| 60 | + await handle.SignalAsync(wf => wf.ApproveAsync(input.Name)); |
| 61 | + return default; |
| 62 | + }); |
| 63 | + |
| 64 | + private static string WorkflowIdForUser(string userId) => $"GreetingWorkflow_for_{userId}"; |
| 65 | +} |
0 commit comments