-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGrpcClientOrchestrationSession.cs
168 lines (133 loc) · 6.71 KB
/
GrpcClientOrchestrationSession.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using DurableTask.Core;
using DurableTaskGrpc;
using Microsoft.Extensions.Logging;
using TaskOrchestrationStream = Grpc.Core.AsyncDuplexStreamingCall<DurableTaskGrpc.TaskOrchestrationRequest, DurableTaskGrpc.TaskOrchestrationResponse>;
using TaskOrchestrationWorkItem = DurableTask.Core.TaskOrchestrationWorkItem;
namespace LLL.DurableTask.Server.Client;
public class GrpcClientOrchestrationSession : IOrchestrationSession
{
private static readonly TimeSpan _renewResponseTimeout = TimeSpan.FromSeconds(20);
private static readonly TimeSpan _completeResponseTimeout = TimeSpan.FromSeconds(20);
private static readonly TimeSpan _fetchResponseTimeout = TimeSpan.FromHours(1);
private static readonly TimeSpan _releaseResponseTimeout = TimeSpan.FromSeconds(20);
private static readonly TimeSpan _abandonResponseTimeout = TimeSpan.FromSeconds(20);
private readonly GrpcClientOrchestrationServiceOptions _options;
private readonly TaskOrchestrationStream _stream;
private readonly ILogger _logger;
public GrpcClientOrchestrationSession(
GrpcClientOrchestrationServiceOptions options,
TaskOrchestrationStream stream,
ILogger logger)
{
_options = options;
_stream = stream;
_logger = logger;
}
public async Task Renew(TaskOrchestrationWorkItem workItem)
{
var request = new TaskOrchestrationRequest
{
RenewRequest = new RenewTaskOrchestrationWorkItemLockRequest()
};
await _stream.RequestStream.WriteAsync(request);
var cts = new CancellationTokenSource(_renewResponseTimeout);
if (!await _stream.ResponseStream.MoveNext(cts.Token))
throw new Exception("Session aborted");
if (_stream.ResponseStream.Current.MessageCase != TaskOrchestrationResponse.MessageOneofCase.RenewResponse)
throw new Exception("Unexpected response");
var renewResponse = _stream.ResponseStream.Current.RenewResponse;
workItem.LockedUntilUtc = renewResponse.LockedUntilUtc.ToDateTime();
}
public async Task Complete(
TaskOrchestrationWorkItem workItem,
OrchestrationRuntimeState newOrchestrationRuntimeState,
IList<TaskMessage> outboundMessages,
IList<TaskMessage> orchestratorMessages,
IList<TaskMessage> timerMessages,
TaskMessage continuedAsNewMessage,
OrchestrationState orchestrationState)
{
var request = new TaskOrchestrationRequest
{
CompleteRequest = new CompleteTaskOrchestrationWorkItemRequest
{
NewEvents = { workItem.OrchestrationRuntimeState.NewEvents.Select(_options.DataConverter.Serialize) },
NewStatus = workItem.OrchestrationRuntimeState.Status,
NewOrchestrationEvents = { newOrchestrationRuntimeState == workItem.OrchestrationRuntimeState
? Enumerable.Empty<string>()
: newOrchestrationRuntimeState.NewEvents.Select(_options.DataConverter.Serialize) },
NewOrchestrationStatus = newOrchestrationRuntimeState == workItem.OrchestrationRuntimeState
? null
: newOrchestrationRuntimeState.Status,
OutboundMessages = { outboundMessages.Select(_options.DataConverter.Serialize) },
OrchestratorMessages = { orchestratorMessages.Select(_options.DataConverter.Serialize) },
TimerMessages = { timerMessages.Select(_options.DataConverter.Serialize) },
ContinuedAsNewMessage = continuedAsNewMessage == null
? string.Empty
: _options.DataConverter.Serialize(continuedAsNewMessage),
OrchestrationState = _options.DataConverter.Serialize(orchestrationState)
}
};
await _stream.RequestStream.WriteAsync(request);
var cts = new CancellationTokenSource(_completeResponseTimeout);
if (!await _stream.ResponseStream.MoveNext(cts.Token))
throw new Exception("Session aborted");
if (_stream.ResponseStream.Current.MessageCase != TaskOrchestrationResponse.MessageOneofCase.CompleteResponse)
throw new Exception("Unexpected response");
}
public async Task<IList<TaskMessage>> FetchNewOrchestrationMessagesAsync(
TaskOrchestrationWorkItem workItem)
{
var request = new TaskOrchestrationRequest
{
FetchRequest = new FetchNewOrchestrationMessagesRequest()
};
await _stream.RequestStream.WriteAsync(request);
var cts = new CancellationTokenSource(_fetchResponseTimeout);
if (!await _stream.ResponseStream.MoveNext(cts.Token))
throw new Exception("Session aborted");
if (_stream.ResponseStream.Current.MessageCase != TaskOrchestrationResponse.MessageOneofCase.FetchResponse)
throw new Exception("Unexpected response");
var fetchResponse = _stream.ResponseStream.Current.FetchResponse;
if (fetchResponse.NewMessages == null)
return null;
return fetchResponse.NewMessages.Messages
.Select(x => _options.DataConverter.Deserialize<TaskMessage>(x))
.ToArray();
}
public async Task Abandon(TaskOrchestrationWorkItem workItem)
{
var request = new TaskOrchestrationRequest
{
AbandonRequest = new AbandonTaskOrchestrationWorkItemLockRequest()
};
await _stream.RequestStream.WriteAsync(request);
var cts = new CancellationTokenSource(_abandonResponseTimeout);
if (!await _stream.ResponseStream.MoveNext(cts.Token))
throw new Exception("Session aborted");
if (_stream.ResponseStream.Current.MessageCase != TaskOrchestrationResponse.MessageOneofCase.AbandonResponse)
throw new Exception("Unexpected response");
}
public async Task Release(TaskOrchestrationWorkItem workItem)
{
var request = new TaskOrchestrationRequest
{
ReleaseRequest = new ReleaseTaskOrchestrationWorkItemRequest()
};
await _stream.RequestStream.WriteAsync(request);
var cts = new CancellationTokenSource(_releaseResponseTimeout);
if (!await _stream.ResponseStream.MoveNext(cts.Token))
throw new Exception("Session aborted");
if (_stream.ResponseStream.Current.MessageCase != TaskOrchestrationResponse.MessageOneofCase.ReleaseResponse)
throw new Exception("Unexpected response");
await _stream.RequestStream.CompleteAsync();
// Last read to close stream
await _stream.ResponseStream.MoveNext(cts.Token);
_stream.Dispose();
}
}