-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathFlowsModule.cs
More file actions
115 lines (96 loc) · 3.76 KB
/
Copy pathFlowsModule.cs
File metadata and controls
115 lines (96 loc) · 3.76 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
using System;
using System.Collections.Generic;
using Cleipnir.ResilientFunctions.CoreRuntime;
using Cleipnir.ResilientFunctions.CoreRuntime.Invocation;
using Cleipnir.ResilientFunctions.Domain;
using Cleipnir.ResilientFunctions.Helpers;
using Cleipnir.ResilientFunctions.Storage;
using Microsoft.Extensions.DependencyInjection;
namespace Cleipnir.Flows.AspNet;
public static class FlowsModule
{
public static IServiceCollection AddFlows(this IServiceCollection services, Func<FlowsConfigurator, FlowsConfigurator> configure)
{
var configurator = new FlowsConfigurator(services);
configure(configurator);
if (configurator.OptionsFunc is null)
services.AddSingleton(new Settings());
else
services.AddSingleton(configurator.OptionsFunc);
services.AddSingleton<FlowsContainer>();
services.AddTransient<Workflow>(_ => CurrentFlow.Workflow ?? throw new InvalidOperationException("Workflow is not present outside Flow"));
services.AddTransient<Effect>(_ => CurrentFlow.Workflow?.Effect ?? throw new InvalidOperationException("Effect is not present outside Flow"));
services.AddHostedService(
s => new FlowsHostedService(s, configurator.FlowsTypes, configurator.EnableGracefulShutdown)
);
return services;
}
}
public class FlowsConfigurator(IServiceCollection services)
{
internal bool EnableGracefulShutdown = false;
internal readonly HashSet<Type> FlowsTypes = new();
internal Func<IServiceProvider, Settings>? OptionsFunc;
public IServiceCollection Services { get; } = services;
public FlowsConfigurator UseInMemoryStore(InMemoryFunctionStore? store = null)
{
Services.AddSingleton<IFunctionStore>(store ?? new InMemoryFunctionStore());
return this;
}
public FlowsConfigurator UseStore(IFunctionStore store)
{
Services.AddSingleton(store);
return this;
}
public FlowsConfigurator WithOptions(Settings settings)
=> WithOptions(_ => settings);
public FlowsConfigurator WithOptions(Func<IServiceProvider, Settings> optionsFunc)
{
OptionsFunc = optionsFunc;
return this;
}
public FlowsConfigurator RegisterFlow<TFlow>() where TFlow : Flow
{
var added = FlowsTypes.Add(typeof(Flows<TFlow>));
if (!added) return this;
Services.AddScoped<TFlow>();
Services.AddTransient(sp =>
new Flows<TFlow>(
flowName: typeof(TFlow).SimpleQualifiedName(),
sp.GetRequiredService<FlowsContainer>()
)
);
return this;
}
public FlowsConfigurator RegisterFlow<TFlow, TParam>() where TFlow : Flow<TParam> where TParam : notnull
{
var added = FlowsTypes.Add(typeof(Flows<TFlow, TParam>));
if (!added) return this;
Services.AddScoped<TFlow>();
Services.AddTransient(sp =>
new Flows<TFlow, TParam>(
flowName: typeof(TFlow).SimpleQualifiedName(),
sp.GetRequiredService<FlowsContainer>()
)
);
return this;
}
public FlowsConfigurator RegisterFlow<TFlow, TParam, TResult>() where TFlow : Flow<TParam, TResult> where TParam : notnull
{
var added = FlowsTypes.Add(typeof(Flows<TFlow, TParam, TResult>));
if (!added) return this;
Services.AddScoped<TFlow>();
Services.AddTransient(sp =>
new Flows<TFlow, TParam, TResult>(
flowName: typeof(TFlow).SimpleQualifiedName(),
sp.GetRequiredService<FlowsContainer>()
)
);
return this;
}
public FlowsConfigurator GracefulShutdown(bool enable)
{
EnableGracefulShutdown = enable;
return this;
}
}