-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathProgram.cs
More file actions
111 lines (101 loc) · 3.63 KB
/
Copy pathProgram.cs
File metadata and controls
111 lines (101 loc) · 3.63 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
107
108
109
110
111
using Microsoft.Extensions.Logging;
using Temporalio.Client;
using Temporalio.Client.EnvConfig;
using Temporalio.Worker;
using TemporalioSamples.RefreshingClient;
async Task<TemporalClient> CreateClientAsync()
{
var connectOptions = ClientEnvConfig.LoadClientConnectOptions();
if (string.IsNullOrEmpty(connectOptions.TargetHost))
{
connectOptions.TargetHost = "localhost:7233";
}
connectOptions.LoggerFactory = LoggerFactory.Create(builder =>
builder.
AddSimpleConsole(options => options.TimestampFormat = "[HH:mm:ss] ").
SetMinimumLevel(LogLevel.Information));
return await TemporalClient.ConnectAsync(connectOptions);
}
async Task RunWorkerAsync(TemporalClient client)
{
// Cancellation token cancelled on ctrl+c
using var tokenSource = new CancellationTokenSource();
Console.CancelKeyPress += (_, eventArgs) =>
{
tokenSource.Cancel();
eventArgs.Cancel = true;
};
// Create an activity instance with some state
var activities = new MyActivities();
// Run worker until cancelled
Console.WriteLine("Running worker");
using var worker = new TemporalWorker(
client,
new TemporalWorkerOptions(taskQueue: "activity-simple-sample").
AddActivity(activities.SelectFromDatabaseAsync).
AddActivity(MyActivities.DoStaticThing).
AddWorkflow<MyWorkflow>());
var replaceWorkerClient = (TemporalClient newClient) =>
{
worker.Client = newClient;
Console.WriteLine("Client's new handle: {0}", worker.Client.BridgeClientProvider?.BridgeClient?.DangerousGetHandle());
return Task.FromResult(true);
};
try
{
await Task.WhenAll(ClientRefreshAsync(replaceWorkerClient, tokenSource.Token), worker.ExecuteAsync(tokenSource.Token));
}
catch (OperationCanceledException)
{
Console.WriteLine("Worker cancelled");
}
}
async Task ExecuteWorkflowAsync(TemporalClient client)
{
Console.WriteLine("Executing workflow");
await client.ExecuteWorkflowAsync(
(MyWorkflow wf) => wf.RunAsync(),
new(id: "activity-simple-workflow-id", taskQueue: "activity-simple-sample"));
}
async Task ClientRefreshAsync(Func<TemporalClient, Task> asyncFunc, CancellationToken cancellationToken)
{
Console.WriteLine("This program will refresh its Temporal client every 10 seconds.");
await RunRecurringTaskAsync(TimeSpan.FromSeconds(10), cancellationToken, asyncFunc);
}
async Task RunRecurringTaskAsync(TimeSpan interval, CancellationToken cancellationToken, Func<TemporalClient, Task> asyncFunc)
{
while (!cancellationToken.IsCancellationRequested)
{
try
{
await Task.Delay(interval, cancellationToken);
Console.WriteLine("Refreshing client...");
var client = await CreateClientAsync();
await asyncFunc(client);
}
catch (OperationCanceledException)
{
Console.WriteLine("Task cancelled.");
break;
}
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
// Continue running even if one iteration fails
}
#pragma warning restore CA1031 // Do not catch general exception types
}
}
var client = await CreateClientAsync();
switch (args.ElementAtOrDefault(0))
{
case "worker":
await RunWorkerAsync(client);
break;
case "workflow":
await ExecuteWorkflowAsync(client);
break;
default:
throw new ArgumentException("Must pass 'worker' or 'workflow' as the single argument");
}