-
Notifications
You must be signed in to change notification settings - Fork 287
/
OrchestrationContext.cs
364 lines (334 loc) · 20.7 KB
/
OrchestrationContext.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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// ----------------------------------------------------------------------------------
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------
namespace DurableTask.Core
{
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using ImpromptuInterface;
/// <summary>
/// Context for an orchestration containing the instance, replay status, orchestration methods and proxy methods
/// </summary>
public abstract class OrchestrationContext
{
/// <summary>
/// Thread-static variable used to signal whether the calling thread is the orchestrator thread.
/// The primary use case is for detecting illegal async usage in orchestration code.
/// </summary>
[ThreadStatic]
public static bool IsOrchestratorThread;
/// <summary>
/// Instance of the currently executing orchestration
/// </summary>
public OrchestrationInstance OrchestrationInstance { get; internal protected set; }
/// <summary>
/// Replay-safe current UTC datetime
/// </summary>
public virtual DateTime CurrentUtcDateTime { get; internal set; }
/// <summary>
/// True if the code is currently replaying, False if code is truly executing for the first time.
/// </summary>
public bool IsReplaying { get; internal protected set; }
/// <summary>
/// Create a proxy client class to schedule remote TaskActivities via a strongly typed interface.
/// </summary>
/// <typeparam name="T">The interface for the proxy client</typeparam>
/// <returns></returns>
public virtual T CreateClient<T>() where T : class
{
return CreateClient<T>(false);
}
/// <summary>
/// Create a proxy client class to schedule remote TaskActivities via a strongly typed interface.
/// </summary>
/// <typeparam name="T">The interface for the proxy client</typeparam>
/// <param name="useFullyQualifiedMethodNames">
/// If true, the method name translation from the interface contains
/// the interface name, if false then only the method name is used
/// </param>
/// <returns></returns>
public virtual T CreateClient<T>(bool useFullyQualifiedMethodNames) where T : class
{
if (!typeof(T).IsInterface)
{
throw new InvalidOperationException("Pass in an interface.");
}
var proxy = new ScheduleProxy(this, typeof(T), useFullyQualifiedMethodNames);
return proxy.ActLike<T>();
}
/// <summary>
/// Creates a proxy client with built-in retry logic.
/// </summary>
/// <typeparam name="T">
/// Task version of the client interface.
/// This is similar to the actual interface implemented by the client but with the
/// return types always of the form Task<TOriginal>
/// where TOriginal was the return
/// type for the same method in the original interface
/// </typeparam>
/// <param name="retryOptions">Retry policies</param>
/// <returns>Dynamic proxy that can be used to schedule the remote tasks</returns>
public virtual T CreateRetryableClient<T>(RetryOptions retryOptions) where T : class
{
return CreateRetryableClient<T>(retryOptions, false);
}
/// <summary>
/// Creates a proxy client with built-in retry logic.
/// </summary>
/// <typeparam name="T">
/// Task version of the client interface.
/// This is similar to the actual interface implemented by the client but with the
/// return types always of the form Task<TOriginal>
/// where TOriginal was the return
/// type for the same method in the original interface
/// </typeparam>
/// <param name="retryOptions">Retry policies</param>
/// <param name="useFullyQualifiedMethodNames">
/// If true, the method name translation from the interface contains
/// the interface name, if false then only the method name is used
/// </param>
/// <returns>Dynamic proxy that can be used to schedule the remote tasks</returns>
public virtual T CreateRetryableClient<T>(RetryOptions retryOptions, bool useFullyQualifiedMethodNames) where T : class
{
if (!typeof(T).IsInterface)
{
throw new InvalidOperationException("Pass in an interface.");
}
var scheduleProxy = new ScheduleProxy(this, typeof(T), useFullyQualifiedMethodNames);
var retryProxy = new RetryProxy<T>(this, retryOptions, scheduleProxy.ActLike<T>());
return retryProxy.ActLike<T>();
}
/// <summary>
/// Schedule a TaskActivity by type. Also retry on failure as per supplied policy.
/// </summary>
/// <typeparam name="T">Return Type of the TaskActivity.Execute method</typeparam>
/// <param name="taskActivityType">Type that devices from TaskActivity class</param>
/// <param name="retryOptions">Retry policy</param>
/// <param name="parameters">Parameters for the TaskActivity.Execute method</param>
/// <returns>Task that represents the execution of the specified TaskActivity</returns>
public virtual Task<T> ScheduleWithRetry<T>(Type taskActivityType, RetryOptions retryOptions,
params object[] parameters)
{
return ScheduleWithRetry<T>(NameVersionHelper.GetDefaultName(taskActivityType),
NameVersionHelper.GetDefaultVersion(taskActivityType),
retryOptions, parameters);
}
/// <summary>
/// Schedule a TaskActivity by name and version. Also retry on failure as per supplied policy.
/// </summary>
/// <typeparam name="T">Return Type of the TaskActivity.Execute method</typeparam>
/// <param name="name">Name of the orchestration as specified by the ObjectCreator</param>
/// <param name="version">Name of the orchestration as specified by the ObjectCreator</param>
/// <param name="retryOptions">Retry policy</param>
/// <param name="parameters">Parameters for the TaskActivity.Execute method</param>
/// <returns>Task that represents the execution of the specified TaskActivity</returns>
public virtual Task<T> ScheduleWithRetry<T>(string name, string version, RetryOptions retryOptions,
params object[] parameters)
{
Task<T> RetryCall() => ScheduleTask<T>(name, version, parameters);
var retryInterceptor = new RetryInterceptor<T>(this, retryOptions, RetryCall);
return retryInterceptor.Invoke();
}
/// <summary>
/// Create a sub-orchestration of the specified type. Also retry on failure as per supplied policy.
/// </summary>
/// <typeparam name="T">Return Type of the TaskOrchestration.RunTask method</typeparam>
/// <param name="orchestrationType">Type of the TaskOrchestration derived class to instantiate</param>
/// <param name="retryOptions">Retry policy</param>
/// <param name="input">Input for the TaskOrchestration.RunTask method</param>
/// <returns>Task that represents the execution of the specified sub-orchestration</returns>
public virtual Task<T> CreateSubOrchestrationInstanceWithRetry<T>(Type orchestrationType,
RetryOptions retryOptions, object input)
{
return CreateSubOrchestrationInstanceWithRetry<T>(NameVersionHelper.GetDefaultName(orchestrationType),
NameVersionHelper.GetDefaultVersion(orchestrationType), retryOptions, input);
}
/// <summary>
/// Create a sub-orchestration of the specified type. Also retry on failure as per supplied policy.
/// </summary>
/// <typeparam name="T">Return Type of the TaskOrchestration.RunTask method</typeparam>
/// <param name="orchestrationType">Type of the TaskOrchestration derived class to instantiate</param>
/// <param name="instanceId">Instance Id of the sub-orchestration</param>
/// <param name="retryOptions">Retry policy</param>
/// <param name="input">Input for the TaskOrchestration.RunTask method</param>
/// <returns>Task that represents the execution of the specified sub-orchestration</returns>
public virtual Task<T> CreateSubOrchestrationInstanceWithRetry<T>(Type orchestrationType, string instanceId,
RetryOptions retryOptions, object input)
{
return CreateSubOrchestrationInstanceWithRetry<T>(NameVersionHelper.GetDefaultName(orchestrationType),
NameVersionHelper.GetDefaultVersion(orchestrationType), instanceId, retryOptions, input);
}
/// <summary>
/// Create a sub-orchestration of the specified name and version. Also retry on failure as per supplied policy.
/// </summary>
/// <typeparam name="T">Return Type of the TaskOrchestration.RunTask method</typeparam>
/// <param name="name">Name of the orchestration as specified by the ObjectCreator</param>
/// <param name="version">Name of the orchestration as specified by the ObjectCreator</param>
/// <param name="retryOptions">Retry policy</param>
/// <param name="input">Input for the TaskOrchestration.RunTask method</param>
/// <returns>Task that represents the execution of the specified sub-orchestration</returns>
public virtual Task<T> CreateSubOrchestrationInstanceWithRetry<T>(string name, string version,
RetryOptions retryOptions, object input)
{
Task<T> RetryCall() => CreateSubOrchestrationInstance<T>(name, version, input);
var retryInterceptor = new RetryInterceptor<T>(this, retryOptions, RetryCall);
return retryInterceptor.Invoke();
}
/// <summary>
/// Create a sub-orchestration of the specified name and version. Also retry on failure as per supplied policy.
/// </summary>
/// <typeparam name="T">Return Type of the TaskOrchestration.RunTask method</typeparam>
/// <param name="name">Name of the orchestration as specified by the ObjectCreator</param>
/// <param name="version">Name of the orchestration as specified by the ObjectCreator</param>
/// <param name="instanceId">Instance Id of the sub-orchestration</param>
/// <param name="retryOptions">Retry policy</param>
/// <param name="input">Input for the TaskOrchestration.RunTask method</param>
/// <returns>Task that represents the execution of the specified sub-orchestration</returns>
public virtual Task<T> CreateSubOrchestrationInstanceWithRetry<T>(string name, string version, string instanceId,
RetryOptions retryOptions, object input)
{
Task<T> RetryCall() => CreateSubOrchestrationInstance<T>(name, version, instanceId, input);
var retryInterceptor = new RetryInterceptor<T>(this, retryOptions, RetryCall);
return retryInterceptor.Invoke();
}
/// <summary>
/// Schedule a TaskActivity by type.
/// </summary>
/// <typeparam name="TResult">Return Type of the TaskActivity.Execute method</typeparam>
/// <param name="activityType">Type that devices from TaskActivity class</param>
/// <param name="parameters">Parameters for the TaskActivity.Execute method</param>
/// <returns>Task that represents the execution of the specified TaskActivity</returns>
public virtual Task<TResult> ScheduleTask<TResult>(Type activityType, params object[] parameters)
{
return ScheduleTask<TResult>(NameVersionHelper.GetDefaultName(activityType),
NameVersionHelper.GetDefaultVersion(activityType), parameters);
}
/// <summary>
/// Schedule a TaskActivity by name and version.
/// </summary>
/// <typeparam name="TResult">Return Type of the TaskActivity.Execute method</typeparam>
/// <param name="name">Name of the orchestration as specified by the ObjectCreator</param>
/// <param name="version">Name of the orchestration as specified by the ObjectCreator</param>
/// <param name="parameters">Parameters for the TaskActivity.Execute method</param>
/// <returns>Task that represents the execution of the specified TaskActivity</returns>
public abstract Task<TResult> ScheduleTask<TResult>(string name, string version, params object[] parameters);
/// <summary>
/// Create a timer that will fire at the specified time and hand back the specified state.
/// </summary>
/// <typeparam name="T">Type of state object</typeparam>
/// <param name="fireAt">Absolute time at which the timer should fire</param>
/// <param name="state">The state to be handed back when the timer fires</param>
/// <returns>Task that represents the async wait on the timer</returns>
public abstract Task<T> CreateTimer<T>(DateTime fireAt, T state);
/// <summary>
/// Create a timer that will fire at the specified time and hand back the specified state.
/// </summary>
/// <typeparam name="T">Type of state object</typeparam>
/// <param name="fireAt">Absolute time at which the timer should fire</param>
/// <param name="state">The state to be handed back when the timer fires</param>
/// <param name="cancelToken">Cancellation token</param>
/// <returns>Task that represents the async wait on the timer</returns>
public abstract Task<T> CreateTimer<T>(DateTime fireAt, T state, CancellationToken cancelToken);
/// <summary>
/// Create a sub-orchestration of the specified type.
/// </summary>
/// <typeparam name="T">Return Type of the TaskOrchestration.RunTask method</typeparam>
/// <param name="orchestrationType">Type of the TaskOrchestration derived class to instantiate</param>
/// <param name="input">Input for the TaskOrchestration.RunTask method</param>
/// <returns>Task that represents the execution of the specified sub-orchestration</returns>
public virtual Task<T> CreateSubOrchestrationInstance<T>(Type orchestrationType, object input)
{
return CreateSubOrchestrationInstance<T>(NameVersionHelper.GetDefaultName(orchestrationType),
NameVersionHelper.GetDefaultVersion(orchestrationType), input);
}
/// <summary>
/// Create a sub-orchestration of the specified type with the specified instance id
/// </summary>
/// <typeparam name="T">Return Type of the TaskOrchestration.RunTask method</typeparam>
/// <param name="orchestrationType">Type of the TaskOrchestration derived class to instantiate</param>
/// <param name="instanceId">InstanceId of the sub-orchestration to create</param>
/// <param name="input">Input for the TaskOrchestration.RunTask method</param>
/// <returns>Task that represents the execution of the specified sub-orchestration</returns>
public virtual Task<T> CreateSubOrchestrationInstance<T>(Type orchestrationType, string instanceId, object input)
{
return CreateSubOrchestrationInstance<T>(NameVersionHelper.GetDefaultName(orchestrationType),
NameVersionHelper.GetDefaultVersion(orchestrationType), instanceId, input);
}
/// <summary>
/// Create a sub-orchestration of the specified name and version.
/// </summary>
/// <typeparam name="T">Return Type of the TaskOrchestration.RunTask method</typeparam>
/// <param name="name">Name of the orchestration as specified by the ObjectCreator</param>
/// <param name="version">Name of the orchestration as specified by the ObjectCreator</param>
/// <param name="input">Input for the TaskOrchestration.RunTask method</param>
/// <returns>Task that represents the execution of the specified sub-orchestration</returns>
public abstract Task<T> CreateSubOrchestrationInstance<T>(string name, string version, object input);
/// <summary>
/// Create a sub-orchestration of the specified name and version with the specific instance id
/// </summary>
/// <typeparam name="T">Return Type of the TaskOrchestration.RunTask method</typeparam>
/// <param name="name">Name of the orchestration as specified by the ObjectCreator</param>
/// <param name="version">Name of the orchestration as specified by the ObjectCreator</param>
/// <param name="instanceId">InstanceId of the sub-orchestration to create</param>
/// <param name="input">Input for the TaskOrchestration.RunTask method</param>
/// <returns>Task that represents the execution of the specified sub-orchestration</returns>
public abstract Task<T> CreateSubOrchestrationInstance<T>(string name, string version, string instanceId,
object input);
/// <summary>
/// Create a sub-orchestration of the specified name and version with the specific instance id
/// </summary>
/// <typeparam name="T">Return Type of the TaskOrchestration.RunTask method</typeparam>
/// <param name="name">Name of the orchestration as specified by the ObjectCreator</param>
/// <param name="version">Name of the orchestration as specified by the ObjectCreator</param>
/// <param name="instanceId">InstanceId of the sub-orchestration to create</param>
/// <param name="input">Input for the TaskOrchestration.RunTask method</param>
/// <param name="tags">Dictionary of key/value tags associated with this instance</param>
/// <returns>Task that represents the execution of the specified sub-orchestration</returns>
public abstract Task<T> CreateSubOrchestrationInstance<T>(string name, string version, string instanceId,
object input, IDictionary<string, string> tags);
/// <summary>
/// Raises an event for the specified orchestration instance, which eventually causes the OnEvent() method in the
/// orchestration to fire.
/// </summary>
/// <param name="orchestrationInstance">Instance in which to raise the event</param>
/// <param name="eventName">Name of the event</param>
/// <param name="eventData">Data for the event</param>
public abstract void SendEvent(OrchestrationInstance orchestrationInstance, string eventName, object eventData);
/// <summary>
/// Checkpoint the orchestration instance by completing the current execution in the ContinueAsNew
/// state and creating a new execution of this instance with the specified input parameter.
/// This is useful in unbounded workflows to ensure that the execution history gets cleaned up regularly and
/// does not overflow the preset size limit.
/// </summary>
/// <param name="input">
/// Input to the new execution of this instance. This is the same type as the one used to start
/// the first execution of this orchestration instance.
/// </param>
public abstract void ContinueAsNew(object input);
/// <summary>
/// Checkpoint the orchestration instance by completing the current execution in the ContinueAsNew
/// state and creating a new execution of this instance with the specified input parameter.
/// This is useful in unbounded workflows to ensure that the execution history gets cleaned up regularly and
/// does not overflow the preset size limit.
/// </summary>
/// <param name="newVersion">
/// New version of the orchestration to start
/// </param>
/// <param name="input">
/// Input to the new execution of this instance. This is the same type as the one used to start
/// the first execution of this orchestration instance.
/// </param>
public abstract void ContinueAsNew(string newVersion, object input);
}
}