-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathProgram.cs
More file actions
100 lines (92 loc) · 3.4 KB
/
Program.cs
File metadata and controls
100 lines (92 loc) · 3.4 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
using Microsoft.Extensions.Logging;
using Temporalio.Client;
using Temporalio.Common.EnvConfig;
using Temporalio.Worker;
using TemporalioSamples.NexusSimple;
using TemporalioSamples.NexusSimple.Caller;
using TemporalioSamples.NexusSimple.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)
{
var connectOptions = ClientEnvConfig.LoadClientConnectOptions();
connectOptions.TargetHost ??= "localhost:7233";
connectOptions.Namespace = temporalNamespace;
connectOptions.LoggerFactory = loggerFactory;
return TemporalClient.ConnectAsync(connectOptions);
}
async Task RunHandlerWorkerAsync()
{
// Run worker until cancelled
logger.LogInformation("Running handler worker");
using var worker = new TemporalWorker(
await ConnectClientAsync("nexus-simple-handler-namespace"),
new TemporalWorkerOptions(taskQueue: "nexus-simple-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-simple-caller-namespace"),
new TemporalWorkerOptions(taskQueue: "nexus-simple-caller-sample").
AddWorkflow<EchoCallerWorkflow>().
AddWorkflow<HelloCallerWorkflow>());
try
{
await worker.ExecuteAsync(tokenSource.Token);
}
catch (OperationCanceledException)
{
logger.LogInformation("Caller worker cancelled");
}
}
async Task ExecuteCallerWorkflowAsync()
{
logger.LogInformation("Executing caller echo workflow");
var client = await ConnectClientAsync("nexus-simple-caller-namespace");
var result1 = await client.ExecuteWorkflowAsync(
(EchoCallerWorkflow wf) => wf.RunAsync("Nexus Echo 👋"),
new(id: "nexus-simple-echo-id", taskQueue: "nexus-simple-caller-sample"));
logger.LogInformation("Workflow result: {Result}", result1);
logger.LogInformation("Executing caller hello workflow");
var result2 = await client.ExecuteWorkflowAsync(
(HelloCallerWorkflow wf) => wf.RunAsync("Temporal", IHelloService.HelloLanguage.Es),
new(id: "nexus-simple-hello-id", taskQueue: "nexus-simple-caller-sample"));
logger.LogInformation("Workflow result: {Result}", result2);
}
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");
}