forked from temporalio/samples-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
106 lines (98 loc) · 3.78 KB
/
Copy pathProgram.cs
File metadata and controls
106 lines (98 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using Microsoft.Extensions.Logging;
using Temporalio.Client;
using Temporalio.Converters;
using Temporalio.Worker;
using TemporalioSamples.ContextPropagation;
using TemporalioSamples.NexusContextPropagation;
using TemporalioSamples.NexusContextPropagation.Caller;
using TemporalioSamples.NexusContextPropagation.Handler;
using var loggerFactory = LoggerFactory.Create(builder =>
builder.
AddSimpleConsole(options => options.TimestampFormat = "[HH:mm:ss] ").
SetMinimumLevel(LogLevel.Information));
var logger = loggerFactory.CreateLogger<Program>();
// Cancellation token cancelled on ctrl+c
using var tokenSource = new CancellationTokenSource();
Console.CancelKeyPress += (_, eventArgs) =>
{
tokenSource.Cancel();
eventArgs.Cancel = true;
};
Task<TemporalClient> ConnectClientAsync(string temporalNamespace) =>
TemporalClient.ConnectAsync(new("localhost:7233")
{
Namespace = temporalNamespace,
LoggerFactory = loggerFactory,
// This is where we set the interceptor to propagate context
Interceptors =
[
new ContextPropagationInterceptor<string?>(
MyContext.UserIdLocal,
DataConverter.Default.PayloadConverter),
// Separate interceptor just for moving in and out of Nexus operation headers. This could
// have been implemented in the ContextPropagationInterceptor, but for sample logic
// separation, it was added as a separate interceptor in this project instead.
new NexusContextPropagationInterceptor(MyContext.UserIdLocal),
],
});
async Task RunHandlerWorkerAsync()
{
// Run worker until cancelled
logger.LogInformation("Running handler worker");
using var worker = new TemporalWorker(
await ConnectClientAsync("nexus-context-propagation-handler-namespace"),
new TemporalWorkerOptions(taskQueue: "nexus-context-propagation-handler-sample").
AddNexusService(new HelloService()).
AddWorkflow<HelloHandlerWorkflow>());
try
{
await worker.ExecuteAsync(tokenSource.Token);
}
catch (OperationCanceledException)
{
logger.LogInformation("Handler worker cancelled");
}
}
async Task RunCallerWorkerAsync()
{
// Run worker until cancelled
logger.LogInformation("Running caller worker");
using var worker = new TemporalWorker(
await ConnectClientAsync("nexus-context-propagation-caller-namespace"),
new TemporalWorkerOptions(taskQueue: "nexus-context-propagation-caller-sample").
AddWorkflow<HelloCallerWorkflow>());
try
{
await worker.ExecuteAsync(tokenSource.Token);
}
catch (OperationCanceledException)
{
logger.LogInformation("Caller worker cancelled");
}
}
async Task ExecuteCallerWorkflowAsync()
{
// Set our user ID that can be accessed in the workflows and Nexus service
MyContext.UserId = "some-user";
logger.LogInformation("Executing caller workflow");
var client = await ConnectClientAsync("nexus-context-propagation-caller-namespace");
var result = await client.ExecuteWorkflowAsync(
(HelloCallerWorkflow wf) => wf.RunAsync("Temporal", IHelloService.HelloLanguage.Es),
new(id: "nexus-context-propagation-id", taskQueue: "nexus-context-propagation-caller-sample"));
logger.LogInformation("Workflow result: {Result}", result);
}
switch (args.ElementAtOrDefault(0))
{
case "handler-worker":
await RunHandlerWorkerAsync();
break;
case "caller-worker":
await RunCallerWorkerAsync();
break;
case "caller-workflow":
await ExecuteCallerWorkflowAsync();
break;
default:
throw new ArgumentException(
"Must pass 'handler-worker', 'caller-worker', or 'caller-workflow' as the single argument");
}