Skip to content

Commit 2319df1

Browse files
authored
Added retry policy to activity info (#511)
1 parent da7202a commit 2319df1

File tree

5 files changed

+24
-12
lines changed

5 files changed

+24
-12
lines changed

src/Temporalio/Activities/ActivityInfo.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ namespace Temporalio.Activities
2020
/// <param name="HeartbeatTimeout">Heartbeat timeout set by the caller.</param>
2121
/// <param name="IsLocal">Whether the activity is a local activity or not.</param>
2222
/// <param name="Priority">The Priority of this activity.</param>
23+
/// <param name="RetryPolicy">
24+
/// The retry policy of this activity.
25+
/// <para>
26+
/// Note that the server may have set a different policy than the one provided when scheduling the activity.
27+
/// If the value is null, it means the server didn't send information about retry policy (e.g. due to old server
28+
/// version), but it may still be defined server-side.
29+
/// </para>
30+
/// </param>
2331
/// <param name="ScheduleToCloseTimeout">Schedule to close timeout set by the caller.</param>
2432
/// <param name="ScheduledTime">When the activity was scheduled.</param>
2533
/// <param name="StartToCloseTimeout">Start to close timeout set by the caller.</param>
@@ -44,6 +52,7 @@ public record ActivityInfo(
4452
TimeSpan? HeartbeatTimeout,
4553
bool IsLocal,
4654
Temporalio.Common.Priority Priority,
55+
Temporalio.Common.RetryPolicy? RetryPolicy,
4756
TimeSpan? ScheduleToCloseTimeout,
4857
DateTime ScheduledTime,
4958
TimeSpan? StartToCloseTimeout,

src/Temporalio/Testing/ActivityEnvironment.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public record ActivityEnvironment
3131
HeartbeatTimeout: null,
3232
IsLocal: false,
3333
Priority: new(),
34+
RetryPolicy: new(),
3435
ScheduleToCloseTimeout: TimeSpan.FromSeconds(1),
3536
ScheduledTime: new(1970, 1, 1, 1, 1, 1, DateTimeKind.Utc),
3637
StartToCloseTimeout: TimeSpan.FromSeconds(1),

src/Temporalio/Worker/ActivityWorker.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ private void StartActivity(Bridge.Api.ActivityTask.ActivityTask tsk)
209209
HeartbeatTimeout: OptionalTimeSpan(start.HeartbeatTimeout),
210210
IsLocal: start.IsLocal,
211211
Priority: start.Priority is { } p ? new(p) : Priority.Default,
212+
RetryPolicy: start.RetryPolicy is { } rp ? RetryPolicy.FromProto(rp) : null,
212213
ScheduleToCloseTimeout: OptionalTimeSpan(start.ScheduleToCloseTimeout),
213214
ScheduledTime: start.ScheduledTime.ToDateTime(),
214215
StartToCloseTimeout: OptionalTimeSpan(start.StartToCloseTimeout),

tests/Temporalio.Tests/KitchenSinkWorkflow.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Temporalio.Api.Enums.V1;
2+
using Temporalio.Common;
23

34
namespace Temporalio.Tests;
45

@@ -55,6 +56,16 @@ public class KitchenSinkWorkflow
5556

5657
public Task SomeSignalAsync(string arg) => Task.CompletedTask;
5758

59+
public static RetryPolicy CreateRetryPolicy(int? maximumAttempts = null, IReadOnlyCollection<string>? nonRetryableErrorTypes = null) =>
60+
new()
61+
{
62+
InitialInterval = TimeSpan.FromMilliseconds(1),
63+
BackoffCoefficient = 1.01F,
64+
MaximumInterval = TimeSpan.FromMilliseconds(2),
65+
MaximumAttempts = maximumAttempts ?? 1,
66+
NonRetryableErrorTypes = nonRetryableErrorTypes,
67+
};
68+
5869
private async Task<(bool ShouldReturn, object? Value)> HandleActionAsync(
5970
KSWorkflowParams args, KSAction action)
6071
{
@@ -144,14 +155,7 @@ action with
144155
var opts = new ActivityOptions()
145156
{
146157
TaskQueue = action.ExecuteActivity.TaskQueue,
147-
RetryPolicy = new()
148-
{
149-
InitialInterval = TimeSpan.FromMilliseconds(1),
150-
BackoffCoefficient = 1.01F,
151-
MaximumInterval = TimeSpan.FromMilliseconds(2),
152-
MaximumAttempts = 1,
153-
NonRetryableErrorTypes = action.ExecuteActivity.NonRetryableErrorTypes ?? Array.Empty<string>(),
154-
},
158+
RetryPolicy = CreateRetryPolicy(action.ExecuteActivity.RetryMaxAttempts, action.ExecuteActivity.NonRetryableErrorTypes),
155159
};
156160
if (action.ExecuteActivity.ScheduleToCloseTimeoutMS != null)
157161
{
@@ -182,10 +186,6 @@ action with
182186
{
183187
opts.ScheduleToCloseTimeout = TimeSpan.FromMinutes(3);
184188
}
185-
if (action.ExecuteActivity.RetryMaxAttempts != null)
186-
{
187-
opts.RetryPolicy.MaximumAttempts = action.ExecuteActivity.RetryMaxAttempts.Value;
188-
}
189189
// Build cancellation token source for delayed cancelling
190190
using var cancelSource = CancellationTokenSource.CreateLinkedTokenSource(
191191
Workflow.CancellationToken);

tests/Temporalio.Tests/Worker/ActivityWorkerTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ public async Task ExecuteActivityAsync_CheckInfo_IsAccurate()
126126
Assert.Equal(1, info.Attempt);
127127
Assert.InRange(info.CurrentAttemptScheduledTime, beforeNow, afterNow);
128128
Assert.False(info.IsLocal);
129+
Assert.Equal(KitchenSinkWorkflow.CreateRetryPolicy(), info.RetryPolicy);
129130
}
130131

131132
[Fact]

0 commit comments

Comments
 (0)