Skip to content

Commit 370ec7e

Browse files
committed
Activity tag support (code)
1 parent 6a7880d commit 370ec7e

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

src/Abstractions/TaskOptions.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ public TaskOptions(TaskRetryOptions? retry = null)
5252
/// <param name="instanceId">The instance ID to use.</param>
5353
/// <returns>A new <see cref="SubOrchestrationOptions" />.</returns>
5454
public SubOrchestrationOptions WithInstanceId(string instanceId) => new(this, instanceId);
55+
56+
/// <summary>
57+
/// Returns a new <see cref="CallActivityOptions" /> with the provided tags.
58+
/// </summary>
59+
/// <param name="tags">The tags to associate with the activity.</param>
60+
/// <returns>A new <see cref="CallActivityOptions" />.</returns>
61+
public CallActivityOptions WithTags(IReadOnlyDictionary<string, string> tags) => new(this, tags);
5562
}
5663

5764
/// <summary>
@@ -109,3 +116,46 @@ public record StartOrchestrationOptions(string? InstanceId = null, DateTimeOffse
109116
/// </summary>
110117
public IReadOnlyDictionary<string, string> Tags { get; init; } = ImmutableDictionary.Create<string, string>();
111118
}
119+
120+
/// <summary>
121+
/// Options for calling activities from an orchestrator.
122+
/// </summary>
123+
public record CallActivityOptions : TaskOptions
124+
{
125+
/// <summary>
126+
/// Initializes a new instance of the <see cref="CallActivityOptions"/> class.
127+
/// </summary>
128+
/// <param name="retry">The task retry options.</param>
129+
/// <param name="tags">The tags to associate with the activity.</param>
130+
public CallActivityOptions(TaskRetryOptions? retry = null, IReadOnlyDictionary<string, string>? tags = null)
131+
: base(retry)
132+
{
133+
if (tags != null)
134+
{
135+
this.Tags = tags;
136+
}
137+
}
138+
139+
/// <summary>
140+
/// Initializes a new instance of the <see cref="CallActivityOptions"/> class.
141+
/// </summary>
142+
/// <param name="options">The task options to wrap.</param>
143+
/// <param name="tags">The tags to associate with the activity.</param>
144+
public CallActivityOptions(TaskOptions options, IReadOnlyDictionary<string, string>? tags = null)
145+
: base(options)
146+
{
147+
if (tags != null)
148+
{
149+
this.Tags = tags;
150+
}
151+
else if (options is CallActivityOptions derived)
152+
{
153+
this.Tags = derived.Tags;
154+
}
155+
}
156+
157+
/// <summary>
158+
/// Gets the tags to associate with the activity instance.
159+
/// </summary>
160+
public IReadOnlyDictionary<string, string> Tags { get; init; } = ImmutableDictionary.Create<string, string>();
161+
}

src/Shared/Grpc/ProtoUtils.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ internal static HistoryEvent ConvertHistoryEvent(P.HistoryEvent proto, EntityCon
9090
proto.EventId,
9191
proto.TaskScheduled.Name,
9292
proto.TaskScheduled.Version,
93-
proto.TaskScheduled.Input);
93+
proto.TaskScheduled.Input) {
94+
Tags = proto.TaskScheduled.Tags,
95+
};
9496
break;
9597
case P.HistoryEvent.EventTypeOneofCase.TaskCompleted:
9698
historyEvent = new TaskCompletedEvent(

src/Worker/Core/Shims/TaskOrchestrationContextWrapper.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,26 +138,36 @@ public override async Task<T> CallActivityAsync<T>(
138138

139139
try
140140
{
141+
IReadOnlyDictionary<string, string>? tags = null;
142+
if (options is CallActivityOptions callActivityOptions)
143+
{
144+
if (callActivityOptions.Tags is not null)
145+
{
146+
tags = callActivityOptions.Tags;
147+
}
148+
}
149+
141150
// TODO: Cancellation (https://github.com/microsoft/durabletask-dotnet/issues/7)
142151
if (options?.Retry?.Policy is RetryPolicy policy)
143152
{
144153
return await this.innerContext.ScheduleWithRetry<T>(
145154
name.Name,
146155
name.Version,
147156
policy.ToDurableTaskCoreRetryOptions(),
157+
tags,
148158
input);
149159
}
150160
else if (options?.Retry?.Handler is AsyncRetryHandler handler)
151161
{
152162
return await this.InvokeWithCustomRetryHandler(
153-
() => this.innerContext.ScheduleTask<T>(name.Name, name.Version, input),
163+
() => this.innerContext.ScheduleTask<T>(name.Name, name.Version, tags, input),
154164
name.Name,
155165
handler,
156166
default);
157167
}
158168
else
159169
{
160-
return await this.innerContext.ScheduleTask<T>(name.Name, name.Version, input);
170+
return await this.innerContext.ScheduleTask<T>(name.Name, name.Version, tags, input);
161171
}
162172
}
163173
catch (global::DurableTask.Core.Exceptions.TaskFailedException e)

0 commit comments

Comments
 (0)