forked from temporalio/samples-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyActivities.cs
35 lines (30 loc) · 1.34 KB
/
MyActivities.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
using Microsoft.Extensions.Logging;
using Temporalio.Activities;
namespace TemporalioSamples.ActivityHeartbeatingCancellation;
public static class MyActivities
{
[Activity]
public static async Task FakeProgressAsync(int sleepIntervalMs = 1000)
{
try
{
// Allow for resuming from heartbeat
var startingPoint = ActivityExecutionContext.Current.Info.HeartbeatDetails.Count > 0
? await ActivityExecutionContext.Current.Info.HeartbeatDetailAtAsync<int>(0)
: 1;
ActivityExecutionContext.Current.Logger.LogInformation("Starting activity at progress: {StartingPoint}", startingPoint);
for (var progress = startingPoint; progress <= 100; ++progress)
{
await Task.Delay(sleepIntervalMs, ActivityExecutionContext.Current.CancellationToken);
ActivityExecutionContext.Current.Logger.LogInformation("Progress: {Progress}", progress);
ActivityExecutionContext.Current.Heartbeat(progress);
}
ActivityExecutionContext.Current.Logger.LogInformation("Fake progress activity completed");
}
catch (OperationCanceledException)
{
ActivityExecutionContext.Current.Logger.LogInformation("Fake progress activity cancelled");
throw;
}
}
}