-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathEffect.cs
More file actions
306 lines (253 loc) · 13.8 KB
/
Copy pathEffect.cs
File metadata and controls
306 lines (253 loc) · 13.8 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Cleipnir.ResilientFunctions.CoreRuntime;
using Cleipnir.ResilientFunctions.Helpers;
using Cleipnir.ResilientFunctions.Queuing;
using Cleipnir.ResilientFunctions.Storage;
namespace Cleipnir.ResilientFunctions.Domain;
public enum ResiliencyLevel
{
AtLeastOnce,
AtMostOnce,
AtLeastOnceDelayFlush
}
public class Effect
{
private readonly EffectResults effectResults;
private readonly UtcNow utcNow;
private readonly FlowTimeouts flowTimeouts;
private readonly FlowState flowState;
internal Effect(EffectResults effectResults, UtcNow utcNow, FlowTimeouts flowTimeouts, FlowState flowState)
{
this.effectResults = effectResults;
this.utcNow = utcNow;
this.flowTimeouts = flowTimeouts;
this.flowState = flowState;
}
internal bool Contains(int id) => Contains(CreateEffectId(id));
internal bool Contains(EffectId effectId) => effectResults.Contains(effectId);
internal IEnumerable<EffectId> EffectIds => effectResults.EffectIds;
internal FlowTimeouts FlowTimeouts => flowTimeouts;
internal WorkStatus? GetStatus(int id)
{
var effectId = CreateEffectId(id);
var storedEffect = effectResults.GetOrValueDefault(effectId);
return storedEffect?.WorkStatus;
}
internal async Task<bool> Mark(bool flush) => await Mark(EffectContext.CurrentContext.NextEffectId(), flush);
internal async Task<bool> Mark(EffectId effectId, bool flush)
{
if (effectResults.Contains(effectId))
return false;
var storedEffect = StoredEffect.CreateCompleted(effectId, alias: null);
await effectResults.Set(storedEffect, flush);
return true;
}
internal async Task<T> CreateOrGet<T>(string alias, T value, bool flush = true)
{
var effectId = effectResults.GetEffectId(alias)
?? CreateNextImplicitId();
return await CreateOrGet(
effectId,
value,
alias,
flush
);
}
internal Task<T> CreateOrGet<T>(EffectId effectId, T value, string? alias, bool flush) => effectResults.CreateOrGet(effectId, value, alias, flush);
internal async Task Upsert<T>(string alias, T value, bool flush = true)
=> await Upsert(
CreateNextImplicitId(),
value,
alias,
flush
);
internal Task Upsert<T>(EffectId effectId, T value, string? alias, bool flush) => effectResults.Upsert(effectId, alias, value, flush);
internal void FlushlessUpsert<T>(EffectId effectId, T value, string? alias) => effectResults.FlushlessUpsert(effectId, alias, value);
internal Task Upserts(IEnumerable<EffectResult> values, bool flush)
=> effectResults.Upserts(values, flush);
internal void FlushlessUpserts(IEnumerable<EffectResult> values)
=> effectResults.FlushlessUpserts(values);
internal bool TryGet<T>(string alias, out T? value)
{
var effectId = effectResults.GetEffectId(alias);
if (effectId == null)
{
value = default;
return false;
}
return effectResults.TryGet(effectId, out value);
}
internal bool TryGet<T>(EffectId effectId, out T? value) => effectResults.TryGet(effectId, out value);
internal bool TryGet(EffectId effectId, Type type, out object? value) => effectResults.TryGet(effectId, type, out value);
internal T Get<T>(string alias) => Get<T>(
effectResults.GetEffectId(alias) ?? throw new InvalidOperationException($"Unknown alias: '{alias}'")
);
internal T Get<T>(EffectId effectId)
{
if (!TryGet<T>(effectId, out var value))
throw new InvalidOperationException($"No value exists for id: '{effectId}'");
return value!;
}
internal IReadOnlyList<EffectId> GetChildren(EffectId effectId) => effectResults.GetChildren(effectId);
#region Implicit ids
public Task Capture(Action work, ResiliencyLevel resiliency = ResiliencyLevel.AtLeastOnce)
=> Capture(id: EffectContext.CurrentContext.NextEffectId(), alias: null, work, resiliency);
public Task<T> Capture<T>(Func<T> work, ResiliencyLevel resiliency = ResiliencyLevel.AtLeastOnce)
=> Capture(id: EffectContext.CurrentContext.NextEffectId(), alias: null, work: () => work().ToTask(), resiliency);
public Task Capture(Func<Task> work, ResiliencyLevel resiliency = ResiliencyLevel.AtLeastOnce)
=> Capture(id: EffectContext.CurrentContext.NextEffectId(), alias: null, work, resiliency);
public Task<T> Capture<T>(Func<Task<T>> work, ResiliencyLevel resiliency = ResiliencyLevel.AtLeastOnce)
=> Capture(id: EffectContext.CurrentContext.NextEffectId(), alias: null, work, resiliency);
public Task Capture(Action work, RetryPolicy retryPolicy, bool flush = true)
=> Capture(id: EffectContext.CurrentContext.NextEffectId(), alias: null, work, retryPolicy, flush);
public Task<T> Capture<T>(Func<T> work, RetryPolicy retryPolicy, bool flush = true)
=> Capture(id: EffectContext.CurrentContext.NextEffectId(), alias: null, work: () => work().ToTask(), retryPolicy, flush);
public Task Capture(Func<Task> work, RetryPolicy retryPolicy, bool flush = true)
=> Capture(id: EffectContext.CurrentContext.NextEffectId(), alias: null, work, retryPolicy, flush);
public Task<T> Capture<T>(Func<Task<T>> work, RetryPolicy retryPolicy, bool flush = true)
=> Capture(id: EffectContext.CurrentContext.NextEffectId(), alias: null, work, retryPolicy, flush);
public Task Capture(string alias, Action work, ResiliencyLevel resiliency = ResiliencyLevel.AtLeastOnce)
=> Capture(id: EffectContext.CurrentContext.NextEffectId(), alias, work, resiliency);
public Task<T> Capture<T>(string alias, Func<T> work, ResiliencyLevel resiliency = ResiliencyLevel.AtLeastOnce)
=> Capture(id: EffectContext.CurrentContext.NextEffectId(), alias, work: () => work().ToTask(), resiliency);
public Task Capture(string alias, Func<Task> work, ResiliencyLevel resiliency = ResiliencyLevel.AtLeastOnce)
=> Capture(id: EffectContext.CurrentContext.NextEffectId(), alias, work, resiliency);
public Task<T> Capture<T>(string alias, Func<Task<T>> work, ResiliencyLevel resiliency = ResiliencyLevel.AtLeastOnce)
=> Capture(id: EffectContext.CurrentContext.NextEffectId(), alias, work, resiliency);
public Task Capture(string alias, Action work, RetryPolicy retryPolicy, bool flush = true)
=> Capture(id: EffectContext.CurrentContext.NextEffectId(), alias, work, retryPolicy, flush);
public Task<T> Capture<T>(string alias, Func<T> work, RetryPolicy retryPolicy, bool flush = true)
=> Capture(id: EffectContext.CurrentContext.NextEffectId(), alias, work: () => work().ToTask(), retryPolicy, flush);
public Task Capture(string alias, Func<Task> work, RetryPolicy retryPolicy, bool flush = true)
=> Capture(id: EffectContext.CurrentContext.NextEffectId(), alias, work, retryPolicy, flush);
public Task<T> Capture<T>(string alias, Func<Task<T>> work, RetryPolicy retryPolicy, bool flush = true)
=> Capture(id: EffectContext.CurrentContext.NextEffectId(), alias, work, retryPolicy, flush);
#endregion
private Task Capture(EffectId id, string? alias, Action work, ResiliencyLevel resiliency = ResiliencyLevel.AtLeastOnce)
=> Capture(id, alias, work: () => { work(); return Task.CompletedTask; }, resiliency);
private Task<T> Capture<T>(EffectId id, string? alias, Func<T> work, ResiliencyLevel resiliency = ResiliencyLevel.AtLeastOnce)
=> Capture(id, alias, work: () => work().ToTask(), resiliency);
private async Task Capture(EffectId id, string? alias, Func<Task> work, ResiliencyLevel resiliency = ResiliencyLevel.AtLeastOnce)
=> await InnerCapture(id, alias, work, resiliency, EffectContext.CurrentContext, retryPolicy: null);
private async Task<T> Capture<T>(EffectId id, string? alias, Func<Task<T>> work, ResiliencyLevel resiliency = ResiliencyLevel.AtLeastOnce)
=> await InnerCapture(id, alias, work, resiliency, EffectContext.CurrentContext, retryPolicy: null);
private Task Capture(EffectId id, string? alias, Action work, RetryPolicy retryPolicy, bool flush = true)
=> Capture(id, alias, work: () => { work(); return Task.CompletedTask; }, retryPolicy, flush);
private Task<T> Capture<T>(EffectId id, string? alias, Func<T> work, RetryPolicy retryPolicy, bool flush = true)
=> Capture(id, alias, work: () => work().ToTask(), retryPolicy, flush);
private async Task Capture(EffectId id, string? alias, Func<Task> work, RetryPolicy retryPolicy, bool flush = true)
=> await InnerCapture(id, alias, work, flush ? ResiliencyLevel.AtLeastOnce : ResiliencyLevel.AtLeastOnceDelayFlush, EffectContext.CurrentContext, retryPolicy);
private async Task<T> Capture<T>(EffectId id, string? alias, Func<Task<T>> work, RetryPolicy retryPolicy, bool flush = true)
=> await InnerCapture(id, alias, work, flush ? ResiliencyLevel.AtLeastOnce : ResiliencyLevel.AtLeastOnceDelayFlush, EffectContext.CurrentContext, retryPolicy);
private async Task InnerCapture(EffectId id, string? alias, Func<Task> work, ResiliencyLevel resiliency, EffectContext effectContext, RetryPolicy? retryPolicy)
{
if (retryPolicy != null && resiliency == ResiliencyLevel.AtMostOnce)
throw new InvalidOperationException("Retry policy cannot be used with AtMostOnce resiliency");
if (retryPolicy == null)
await effectResults.InnerCapture(id, alias, work, resiliency, effectContext);
else
await effectResults.InnerCapture(
id,
alias,
work: () => retryPolicy.Invoke(work, effect: this, utcNow, flowTimeouts),
resiliency,
effectContext
);
}
private async Task<T> InnerCapture<T>(EffectId id, string? alias, Func<Task<T>> work, ResiliencyLevel resiliency, EffectContext effectContext, RetryPolicy? retryPolicy)
{
if (retryPolicy != null && resiliency == ResiliencyLevel.AtMostOnce)
throw new InvalidOperationException("Retry policy cannot be used with AtMostOnce resiliency");
if (retryPolicy == null)
return await effectResults.InnerCapture(id, alias, work, resiliency, effectContext);
return await effectResults.InnerCapture(
id,
alias,
work: () => retryPolicy.Invoke(work, effect: this, utcNow, flowTimeouts),
resiliency,
effectContext
);
}
internal int TakeNextImplicitId() => EffectContext.CurrentContext.NextImplicitId();
internal EffectId CreateEffectId(int id)
{
var parent = EffectContext.CurrentContext.Parent;
if (parent == null)
return id.ToEffectId();
return new EffectId([..parent.Value, id]);
}
internal EffectId CreateNextImplicitId()
{
var id = EffectContext.CurrentContext.NextImplicitId();
var parent = EffectContext.CurrentContext.Parent;
if (parent == null)
return id.ToEffectId();
return new EffectId([..parent.Value, id]);
}
public Task Flush() => effectResults.Flush();
public async Task ForEach<T>(IEnumerable<T> elms, Func<T, Task> handler, string? alias = null)
{
var id = EffectContext.CurrentContext.NextEffectId();
var atIndex = await CreateOrGet(id, value: 0, alias, flush: false);
foreach (var elm in elms.Skip(atIndex))
{
var prevParent = id.CreateChild(atIndex - 1);
await effectResults.Clear(prevParent, flush: true);
var parent = id.CreateChild(atIndex);
await Capture(
parent,
alias: null,
work: () => handler(elm)
);
atIndex++;
await Upsert(id, atIndex, alias, flush: false);
}
var lastChild = id.CreateChild(atIndex - 1);
await effectResults.Clear(lastChild, flush: false);
}
internal async Task Clear(EffectId id, bool flush) => await effectResults.Clear(id, flush);
internal void FlushlessClear(EffectId id) => effectResults.FlushlessClear(id);
internal bool IsDirty(EffectId id) => effectResults.IsDirty(id);
public async Task<TSeed> AggregateEach<T, TSeed>(
IEnumerable<T> elms,
TSeed seed,
Func<T, TSeed, Task<TSeed>> handler,
string? alias = null)
{
var id = EffectContext.CurrentContext.NextEffectId();
var atIndexAndAkk = await CreateOrGet(id, value: Tuple.Create(0, seed), alias, flush: false);
var atIndex = atIndexAndAkk.Item1;
var akk = atIndexAndAkk.Item2;
foreach (var elm in elms.Skip(atIndex))
{
var prevParent = id.CreateChild(atIndex - 1);
await effectResults.Clear(prevParent, flush: true);
var parent = id.CreateChild(atIndex);
akk = await Capture(
parent,
alias: null,
work: () => handler(elm, akk)
);
atIndex++;
await Upsert(id, Tuple.Create(atIndex, akk), alias, flush: false);
}
var lastChild = id.CreateChild(atIndex - 1);
await effectResults.Clear(lastChild, flush: false);
return akk;
}
internal void RegisterQueueManager(QueueManager queueManager) => effectResults.QueueManager = queueManager;
internal string ExecutionTree() => EffectPrinter.Print(effectResults);
public Task<T> RunParallelle<T>(Func<Task<T>> work)
{
flowState.SubflowStarted();
var task = Capture(() => Task.Run(work));
return task.ContinueWith(t =>
{
flowState.SubflowCompleted();
return t.GetAwaiter().GetResult();
});
}
}