forked from temporalio/samples-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoyaltyProgram.workflow.cs
47 lines (40 loc) · 1.33 KB
/
LoyaltyProgram.workflow.cs
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
namespace TemporalioSamples.SignalsQueries;
using Microsoft.Extensions.Logging;
using Temporalio.Workflows;
public record Purchase(string Id, int TotalCents);
[Workflow]
public class LoyaltyProgram
{
private readonly Queue<Purchase> toProcess = new();
[WorkflowQuery]
public int Points { get; private set; }
[WorkflowRun]
public async Task RunAsync(string userId)
{
while (true)
{
// Wait for purchase
await Workflow.WaitConditionAsync(() => toProcess.Count > 0);
// Process
var purchase = toProcess.Dequeue();
Points += purchase.TotalCents;
Workflow.Logger.LogInformation("Added {TotalCents} points, total: {Points}", purchase.TotalCents, Points);
if (Points >= 10_000)
{
await Workflow.ExecuteActivityAsync(
() => MyActivities.SendCoupon(userId),
new() { ScheduleToCloseTimeout = TimeSpan.FromMinutes(5) });
Points -= 10_000;
Workflow.Logger.LogInformation("Remaining points: {Points}", Points);
}
}
}
[WorkflowSignal]
public async Task NotifyPurchaseAsync(Purchase purchase)
{
if (!toProcess.Contains(purchase))
{
toProcess.Enqueue(purchase);
}
}
}