From c15d3145752fa9364ac397c5539780ec0bc15fe4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Loren=20=F0=9F=A4=93?= Date: Thu, 28 Mar 2024 23:16:14 -0400 Subject: [PATCH] in prog --- src/SignalsQueries/LoyaltyProgram.workflow.cs | 19 +++++++++---------- src/SignalsQueries/MyActivities.cs | 5 ++++- src/SignalsQueries/Program.cs | 13 ++++++++----- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/SignalsQueries/LoyaltyProgram.workflow.cs b/src/SignalsQueries/LoyaltyProgram.workflow.cs index fbc2c9d..858a46a 100644 --- a/src/SignalsQueries/LoyaltyProgram.workflow.cs +++ b/src/SignalsQueries/LoyaltyProgram.workflow.cs @@ -7,32 +7,31 @@ namespace TemporalioSamples.SignalsQueries; public class LoyaltyProgram { private string? userId; - private int points = 0; + + [WorkflowQuery] + public int Points { get; private set; } [WorkflowRun] public async Task RunAsync(string userId) { this.userId = userId; - // TODO dear chad, how do i await cancellation? or something short/simple that prevents from returning + // Keep this workflow running forever + await Workflow.WaitConditionAsync(() => false); } [WorkflowSignal] public async Task NotifyPurchaseAsync(int purchaseTotalCents) { - points += purchaseTotalCents; - Workflow.Logger.LogInformation("Added {Result} points, total: {Total}", purchaseTotalCents, points); + Points += purchaseTotalCents; + Workflow.Logger.LogInformation("Added {Result} points, total: {Total}", purchaseTotalCents, Points); - if (points >= 10_000) + if (Points >= 10_000) { - Workflow.Logger.LogInformation("Sending coupon to {UserId}", userId); await Workflow.ExecuteActivityAsync( () => MyActivities.SendCoupon(userId), new() { ScheduleToCloseTimeout = TimeSpan.FromMinutes(5) }); - points -= 10_000; + Points -= 10_000; } } - - [WorkflowQuery] - public int GetPoints() => points; } \ No newline at end of file diff --git a/src/SignalsQueries/MyActivities.cs b/src/SignalsQueries/MyActivities.cs index 8b9cf2c..1e24cf5 100644 --- a/src/SignalsQueries/MyActivities.cs +++ b/src/SignalsQueries/MyActivities.cs @@ -5,5 +5,8 @@ namespace TemporalioSamples.SignalsQueries; public class MyActivities { [Activity] - public static string SendCoupon(string? userId) => "coupon emailed"; + public static void SendCoupon(string? userId) + { + ActivityExecutionContext.Current.Logger.LogInformation("Sending coupon to user {UserId}", userId); + } } \ No newline at end of file diff --git a/src/SignalsQueries/Program.cs b/src/SignalsQueries/Program.cs index 243df8d..1746531 100644 --- a/src/SignalsQueries/Program.cs +++ b/src/SignalsQueries/Program.cs @@ -22,9 +22,6 @@ async Task RunWorkerAsync() 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( @@ -45,11 +42,17 @@ async Task RunWorkerAsync() async Task ExecuteWorkflowAsync() { Console.WriteLine("Executing workflow"); - await client.ExecuteWorkflowAsync( + var handle = await client.StartWorkflowAsync( (LoyaltyProgram wf) => wf.RunAsync("user-id-123"), new(id: "signals-queries-workflow-id", taskQueue: "signals-queries-sample")); - // TODO signal and query + Console.WriteLine("Signal: Purchase made for $80"); + await handle.SignalAsync(wf => wf.NotifyPurchaseAsync(8_000)); + Console.WriteLine("Signal: Purchase made for $30"); + await handle.SignalAsync(wf => wf.NotifyPurchaseAsync(3_000)); + + var points = await handle.QueryAsync(wf => wf.Points); + Console.WriteLine("Remaining points: {Points}", points); } switch (args.ElementAtOrDefault(0))