-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathDurableTaskGrpcClient.cs
More file actions
288 lines (245 loc) · 9.9 KB
/
DurableTaskGrpcClient.cs
File metadata and controls
288 lines (245 loc) · 9.9 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Google.Protobuf.WellKnownTypes;
using Grpc.Core;
using Grpc.Net.Client;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using static DurableTask.Protobuf.TaskHubSidecarService;
using P = DurableTask.Protobuf;
namespace DurableTask.Grpc;
public class DurableTaskGrpcClient : DurableTaskClient
{
readonly IServiceProvider services;
readonly IDataConverter dataConverter;
readonly ILogger logger;
readonly IConfiguration? configuration;
readonly GrpcChannel sidecarGrpcChannel;
readonly TaskHubSidecarServiceClient sidecarClient;
readonly bool ownsChannel;
bool isDisposed;
DurableTaskGrpcClient(Builder builder)
{
this.services = builder.services ?? SdkUtils.EmptyServiceProvider;
this.dataConverter = builder.dataConverter ?? this.services.GetService<IDataConverter>() ?? SdkUtils.DefaultDataConverter;
this.logger = SdkUtils.GetLogger(builder.loggerFactory ?? this.services.GetService<ILoggerFactory>() ?? NullLoggerFactory.Instance);
this.configuration = builder.configuration ?? this.services.GetService<IConfiguration>();
if (builder.channel != null)
{
// Use the channel from the builder, which was given to us by the app (thus we don't own it and can't dispose it)
this.sidecarGrpcChannel = builder.channel;
this.ownsChannel = false;
}
else
{
// We have to create our own channel and are responsible for disposing it
this.sidecarGrpcChannel = GrpcChannel.ForAddress(builder.address ?? SdkUtils.GetSidecarAddress(this.configuration));
this.ownsChannel = true;
}
this.sidecarClient = new TaskHubSidecarServiceClient(this.sidecarGrpcChannel);
}
public static DurableTaskClient Create() => CreateBuilder().Build();
public static Builder CreateBuilder() => new();
public override async ValueTask DisposeAsync()
{
if (!this.isDisposed)
{
if (this.ownsChannel)
{
await this.sidecarGrpcChannel.ShutdownAsync();
this.sidecarGrpcChannel.Dispose();
}
GC.SuppressFinalize(this);
this.isDisposed = true;
}
}
/// <inheritdoc/>
public override async Task<string> ScheduleNewOrchestrationInstanceAsync(
TaskName orchestratorName,
string? instanceId = null,
object? input = null,
DateTimeOffset? startTime = null)
{
var request = new P.CreateInstanceRequest
{
Name = orchestratorName.Name,
Version = orchestratorName.Version,
InstanceId = instanceId ?? Guid.NewGuid().ToString("N"),
Input = this.dataConverter.Serialize(input),
};
this.logger.SchedulingOrchestration(
request.InstanceId,
orchestratorName,
sizeInBytes: request.Input != null ? Encoding.UTF8.GetByteCount(request.Input) : 0,
startTime.GetValueOrDefault(DateTimeOffset.UtcNow));
if (startTime.HasValue)
{
// Convert timestamps to UTC if not already UTC
request.ScheduledStartTimestamp = Timestamp.FromDateTimeOffset(startTime.Value.ToUniversalTime());
}
P.CreateInstanceResponse? result = await this.sidecarClient.StartInstanceAsync(request);
return result.InstanceId;
}
/// <inheritdoc/>
public override async Task RaiseEventAsync(string instanceId, string eventName, object? eventPayload)
{
if (string.IsNullOrEmpty(instanceId))
{
throw new ArgumentNullException(nameof(instanceId));
}
if (string.IsNullOrEmpty(eventName))
{
throw new ArgumentNullException(nameof(eventName));
}
P.RaiseEventRequest request = new()
{
InstanceId = instanceId,
Name = eventName,
Input = this.dataConverter.Serialize(eventPayload),
};
await this.sidecarClient.RaiseEventAsync(request);
}
public override async Task TerminateAsync(string instanceId, object? output)
{
if (string.IsNullOrEmpty(instanceId))
{
throw new ArgumentNullException(nameof(instanceId));
}
this.logger.TerminatingInstance(instanceId);
string? serializedOutput = this.dataConverter.Serialize(output);
await this.sidecarClient.TerminateInstanceAsync(new P.TerminateRequest
{
InstanceId = instanceId,
Output = serializedOutput,
});
}
/// <inheritdoc/>
public override async Task<OrchestrationMetadata?> GetInstanceMetadataAsync(
string instanceId,
bool getInputsAndOutputs = false)
{
if (string.IsNullOrEmpty(instanceId))
{
throw new ArgumentNullException(nameof(instanceId));
}
P.GetInstanceResponse response = await this.sidecarClient.GetInstanceAsync(
new P.GetInstanceRequest
{
InstanceId = instanceId,
GetInputsAndOutputs = getInputsAndOutputs,
});
// REVIEW: Should we return a non-null value instead of !exists?
if (!response.Exists)
{
return null;
}
return new OrchestrationMetadata(response, this.dataConverter, getInputsAndOutputs);
}
/// <inheritdoc/>
public override async Task<OrchestrationMetadata> WaitForInstanceStartAsync(
string instanceId,
CancellationToken cancellationToken,
bool getInputsAndOutputs = false)
{
this.logger.WaitingForInstanceStart(instanceId, getInputsAndOutputs);
P.GetInstanceRequest request = new()
{
InstanceId = instanceId,
GetInputsAndOutputs = getInputsAndOutputs,
};
P.GetInstanceResponse response;
try
{
response = await this.sidecarClient.WaitForInstanceStartAsync(
request,
cancellationToken: cancellationToken);
}
catch (RpcException e) when (e.StatusCode == StatusCode.Cancelled)
{
throw new OperationCanceledException($"The {nameof(WaitForInstanceStartAsync)} operation was canceled.", e, cancellationToken);
}
return new OrchestrationMetadata(response, this.dataConverter, getInputsAndOutputs);
}
/// <inheritdoc/>
public override async Task<OrchestrationMetadata> WaitForInstanceCompletionAsync(
string instanceId,
CancellationToken cancellationToken,
bool getInputsAndOutputs = false)
{
this.logger.WaitingForInstanceCompletion(instanceId, getInputsAndOutputs);
P.GetInstanceRequest request = new()
{
InstanceId = instanceId,
GetInputsAndOutputs = getInputsAndOutputs,
};
P.GetInstanceResponse response;
try
{
response = await this.sidecarClient.WaitForInstanceCompletionAsync(
request,
cancellationToken: cancellationToken);
}
catch (RpcException e) when (e.StatusCode == StatusCode.Cancelled)
{
throw new OperationCanceledException($"The {nameof(WaitForInstanceCompletionAsync)} operation was canceled.", e, cancellationToken);
}
return new OrchestrationMetadata(response, this.dataConverter, getInputsAndOutputs);
}
public sealed class Builder
{
internal IServiceProvider? services;
internal ILoggerFactory? loggerFactory;
internal IDataConverter? dataConverter;
internal IConfiguration? configuration;
internal GrpcChannel? channel;
internal string? address;
public Builder UseLoggerFactory(ILoggerFactory loggerFactory)
{
this.loggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory));
return this;
}
public Builder UseServices(IServiceProvider services)
{
this.services = services ?? throw new ArgumentNullException(nameof(services));
return this;
}
public Builder UseAddress(string address)
{
this.address = SdkUtils.ValidateAddress(address);
return this;
}
/// <summary>
/// Configures a <see cref="GrpcChannel"/> to use for communicating with the sidecar process.
/// </summary>
/// <remarks>
/// This builder method allows you to provide your own gRPC channel for communicating with the Durable Task
/// sidecar service. Channels provided using this method won't be disposed when the client is disposed.
/// Rather, the caller remains responsible for shutting down the channel after disposing the client.
/// </remarks>
/// <param name="channel">The gRPC channel to use.</param>
/// <returns>Returns this <see cref="Builder"/> instance.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="channel"/> is <c>null</c>.</exception>
public Builder UseGrpcChannel(GrpcChannel channel)
{
this.channel = channel ?? throw new ArgumentNullException(nameof(channel));
return this;
}
public Builder UseDataConverter(IDataConverter dataConverter)
{
this.dataConverter = dataConverter ?? throw new ArgumentNullException(nameof(dataConverter));
return this;
}
public Builder UseConfiguration(IConfiguration configuration)
{
this.configuration = configuration;
return this;
}
public DurableTaskClient Build() => new DurableTaskGrpcClient(this);
}
}