-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathProgram.cs
More file actions
84 lines (76 loc) · 2.94 KB
/
Program.cs
File metadata and controls
84 lines (76 loc) · 2.94 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
using Microsoft.Extensions.Logging;
using Temporalio.Client;
using Temporalio.Common.EnvConfig;
using Temporalio.Worker;
using TemporalioSamples.UpdateWithStartEarlyReturn;
// Create a client to localhost on default namespace
var connectOptions = ClientEnvConfig.LoadClientConnectOptions();
connectOptions.TargetHost ??= "localhost:7233";
connectOptions.LoggerFactory = LoggerFactory.Create(builder =>
builder.
AddSimpleConsole(options => options.TimestampFormat = "[HH:mm:ss] ").
SetMinimumLevel(LogLevel.Information));
var client = await TemporalClient.ConnectAsync(connectOptions);
const string TaskQueue = "update-with-start-early-return";
async Task RunWorkerAsync()
{
// Cancellation token cancelled on ctrl+c
using var tokenSource = new CancellationTokenSource();
Console.CancelKeyPress += (_, eventArgs) =>
{
tokenSource.Cancel();
eventArgs.Cancel = true;
};
// Run worker until cancelled
Console.WriteLine("Running worker");
using var worker = new TemporalWorker(
client,
new TemporalWorkerOptions(TaskQueue).
AddAllActivities(typeof(Activities), null).
AddWorkflow<PaymentWorkflow>());
try
{
await worker.ExecuteAsync(tokenSource.Token);
}
catch (OperationCanceledException)
{
Console.WriteLine("Worker cancelled");
}
}
async Task ExecuteWorkflowAsync()
{
const string paymentId = "my-early-return-payment-123";
Console.WriteLine("Starting payment processing and waiting for authorize");
// Issue payment for 500 as an update with start which will only wait until
// the update (i.e. authorize) is complete, not the whole workflow like
// traditional workflow runs
var startOperation = WithStartWorkflowOperation.Create(
(PaymentWorkflow wf) => wf.RunAsync(new(500)),
new(id: paymentId, taskQueue: TaskQueue)
{
// Unlike the lazy-init sample, we want to fail if this is already
// running, our only purpose here is early return, not get-or-create
// logic
IdConflictPolicy = Temporalio.Api.Enums.V1.WorkflowIdConflictPolicy.Fail,
});
await client.ExecuteUpdateWithStartWorkflowAsync(
(PaymentWorkflow wf) => wf.WaitUntilAuthorizedAsync(),
new(startOperation));
Console.WriteLine("Payment authorized, can move on while rest of payment processing finishes...");
// Go ahead and wait for payment to be complete (we don't have to do this,
// we're only doing it for the sample)
var handle = await startOperation.GetHandleAsync();
await handle.GetResultAsync();
Console.WriteLine("Payment processing complete");
}
switch (args.ElementAtOrDefault(0))
{
case "worker":
await RunWorkerAsync();
break;
case "workflow":
await ExecuteWorkflowAsync();
break;
default:
throw new ArgumentException("Must pass 'worker' or 'workflow' as the single argument");
}