Skip to content

Commit

Permalink
in prog
Browse files Browse the repository at this point in the history
  • Loading branch information
lorensr committed Mar 29, 2024
1 parent 813db50 commit c15d314
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
19 changes: 9 additions & 10 deletions src/SignalsQueries/LoyaltyProgram.workflow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
5 changes: 4 additions & 1 deletion src/SignalsQueries/MyActivities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
13 changes: 8 additions & 5 deletions src/SignalsQueries/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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))
Expand Down

0 comments on commit c15d314

Please sign in to comment.