|
1 | 1 | namespace Shared.EventStore.Extensions; |
2 | 2 |
|
| 3 | +using EventHandling; |
| 4 | +using global::EventStore.Client; |
| 5 | +using Microsoft.AspNetCore.Builder; |
| 6 | +using Microsoft.Extensions.Configuration; |
| 7 | +using Shared.EventStore.EventStore; |
| 8 | +using Shared.TraceHandler; |
| 9 | +using SubscriptionWorker; |
3 | 10 | using System; |
4 | 11 | using System.Collections.Generic; |
5 | 12 | using System.Diagnostics; |
6 | 13 | using System.Diagnostics.CodeAnalysis; |
7 | 14 | using System.Linq; |
8 | 15 | using System.Threading; |
9 | 16 | using System.Threading.Tasks; |
10 | | -using EventHandling; |
11 | | -using global::EventStore.Client; |
12 | | -using Microsoft.AspNetCore.Builder; |
13 | | -using SubscriptionWorker; |
14 | 17 |
|
15 | 18 | public static class IApplicationBuilderExtensions |
16 | 19 | { |
@@ -51,52 +54,80 @@ public static async Task ConfigureSubscriptionService(this IApplicationBuilder a |
51 | 54 | } |
52 | 55 | } |
53 | 56 |
|
| 57 | + internal static List<SubscriptionWorker> GetDomainWorker(Dictionary<String, IDomainEventHandlerResolver> eventHandlerResolvers, |
| 58 | + ISubscriptionRepository subscriptionRepository, |
| 59 | + SubscriptionWorkersRoot configuration, |
| 60 | + String eventStoreConnectionString, |
| 61 | + SubscriptionWorkerConfig configurationSubscriptionWorker, |
| 62 | + Action<TraceEventType, String, String> traceHandler) { |
| 63 | + KeyValuePair<String, IDomainEventHandlerResolver> ehr = eventHandlerResolvers.SingleOrDefault(e => e.Key == "Domain"); |
| 64 | + List<SubscriptionWorker> workers = new(); |
| 65 | + if (ehr.Value == null) |
| 66 | + return workers; |
| 67 | + for (Int32 i = 0; i < configurationSubscriptionWorker.InstanceCount; i++) { |
| 68 | + SubscriptionWorker worker = ConfigureSubscriptionWorker(subscriptionRepository, configuration, eventStoreConnectionString, traceHandler, ehr, configurationSubscriptionWorker, "DOMAIN"); |
| 69 | + |
| 70 | + workers.Add(worker); |
| 71 | + } |
| 72 | + |
| 73 | + return workers; |
| 74 | + } |
| 75 | + |
| 76 | + internal static List<SubscriptionWorker> GetOrderedWorkers(Dictionary<String, IDomainEventHandlerResolver> eventHandlerResolvers, |
| 77 | + ISubscriptionRepository subscriptionRepository, |
| 78 | + SubscriptionWorkersRoot configuration, |
| 79 | + String eventStoreConnectionString, |
| 80 | + SubscriptionWorkerConfig configurationSubscriptionWorker, |
| 81 | + Action<TraceEventType, String, String> traceHandler) { |
| 82 | + KeyValuePair<String, IDomainEventHandlerResolver> ehr = eventHandlerResolvers.SingleOrDefault(e => e.Key == "Ordered"); |
| 83 | + List<SubscriptionWorker> workers = new(); |
| 84 | + if (ehr.Value == null) |
| 85 | + return workers; |
| 86 | + SubscriptionWorker worker = ConfigureSubscriptionWorker(subscriptionRepository, configuration, |
| 87 | + eventStoreConnectionString, traceHandler, ehr, configurationSubscriptionWorker, "ORDERED"); |
| 88 | + workers.Add(worker); |
| 89 | + return workers; |
| 90 | + } |
| 91 | + |
| 92 | + internal static List<SubscriptionWorker> GetMainWorkers(Dictionary<String, IDomainEventHandlerResolver> eventHandlerResolvers, |
| 93 | + ISubscriptionRepository subscriptionRepository, |
| 94 | + SubscriptionWorkersRoot configuration, |
| 95 | + String eventStoreConnectionString, |
| 96 | + SubscriptionWorkerConfig configurationSubscriptionWorker, |
| 97 | + Action<TraceEventType, String, String> traceHandler) { |
| 98 | + List<SubscriptionWorker> workers = new(); |
| 99 | + KeyValuePair<String, IDomainEventHandlerResolver> ehr = eventHandlerResolvers.SingleOrDefault(e => e.Key == "Main"); |
| 100 | + if (ehr.Value == null) |
| 101 | + return workers; |
| 102 | + for (Int32 i = 0; i < configurationSubscriptionWorker.InstanceCount; i++) |
| 103 | + { |
| 104 | + SubscriptionWorker worker = ConfigureSubscriptionWorker(subscriptionRepository, configuration, |
| 105 | + eventStoreConnectionString, traceHandler, ehr, configurationSubscriptionWorker, "MAIN"); |
| 106 | + |
| 107 | + workers.Add(worker); |
| 108 | + } |
| 109 | + |
| 110 | + return workers; |
| 111 | + } |
| 112 | + |
54 | 113 | internal static List<SubscriptionWorker> ConfigureSubscriptions(ISubscriptionRepository subscriptionRepository, |
55 | | - SubscriptionWorkersRoot configuration, |
56 | | - String eventStoreConnectionString, |
57 | | - Dictionary<String, IDomainEventHandlerResolver> eventHandlerResolvers, |
58 | | - Action<TraceEventType, String, String> traceHandler) { |
| 114 | + SubscriptionWorkersRoot configuration, |
| 115 | + String eventStoreConnectionString, |
| 116 | + Dictionary<String, IDomainEventHandlerResolver> eventHandlerResolvers, |
| 117 | + Action<TraceEventType, String, String> traceHandler) { |
59 | 118 | List<SubscriptionWorker> workers = new(); |
60 | 119 |
|
61 | 120 | foreach (SubscriptionWorkerConfig configurationSubscriptionWorker in configuration.SubscriptionWorkers) { |
62 | 121 | if (!configurationSubscriptionWorker.Enabled) |
63 | 122 | continue; |
64 | 123 |
|
65 | | - if (configurationSubscriptionWorker.IsOrdered) { |
66 | | - KeyValuePair<String, IDomainEventHandlerResolver> ehr = eventHandlerResolvers.SingleOrDefault(e => e.Key == "Ordered"); |
| 124 | + List<SubscriptionWorker> workersList = configurationSubscriptionWorker switch { |
| 125 | + _ when configurationSubscriptionWorker.IsDomainOnly => GetDomainWorker(eventHandlerResolvers, subscriptionRepository, configuration, eventStoreConnectionString, configurationSubscriptionWorker, traceHandler), |
| 126 | + _ when configurationSubscriptionWorker.IsOrdered => GetOrderedWorkers(eventHandlerResolvers, subscriptionRepository, configuration, eventStoreConnectionString, configurationSubscriptionWorker, traceHandler), |
| 127 | + _ => GetMainWorkers(eventHandlerResolvers, subscriptionRepository, configuration, eventStoreConnectionString, configurationSubscriptionWorker, traceHandler), |
| 128 | + }; |
67 | 129 |
|
68 | | - if (ehr.Value != null) { |
69 | | - SubscriptionWorker worker = ConfigureSubscriptionWorker(subscriptionRepository, configuration, |
70 | | - eventStoreConnectionString, traceHandler, ehr, configurationSubscriptionWorker, "ORDERED"); |
71 | | - workers.Add(worker); |
72 | | - } |
73 | | - } |
74 | | - else if (configurationSubscriptionWorker.IsDomainOnly) { |
75 | | - KeyValuePair<String, IDomainEventHandlerResolver> ehr = eventHandlerResolvers.SingleOrDefault(e => e.Key == "Domain"); |
76 | | - |
77 | | - if (ehr.Value != null) |
78 | | - { |
79 | | - for (Int32 i = 0; i < configurationSubscriptionWorker.InstanceCount; i++) { |
80 | | - SubscriptionWorker worker = ConfigureSubscriptionWorker(subscriptionRepository, configuration, |
81 | | - eventStoreConnectionString, traceHandler, ehr, configurationSubscriptionWorker, "DOMAIN"); |
82 | | - |
83 | | - workers.Add(worker); |
84 | | - } |
85 | | - } |
86 | | - } |
87 | | - else { |
88 | | - KeyValuePair<String, IDomainEventHandlerResolver> ehr = eventHandlerResolvers.SingleOrDefault(e => e.Key == "Main"); |
89 | | - if (ehr.Value != null) |
90 | | - { |
91 | | - for (Int32 i = 0; i < configurationSubscriptionWorker.InstanceCount; i++) |
92 | | - { |
93 | | - SubscriptionWorker worker = ConfigureSubscriptionWorker(subscriptionRepository, configuration, |
94 | | - eventStoreConnectionString, traceHandler, ehr, configurationSubscriptionWorker, "MAIN"); |
95 | | - |
96 | | - workers.Add(worker); |
97 | | - } |
98 | | - } |
99 | | - } |
| 130 | + workers.AddRange(workersList); |
100 | 131 | } |
101 | 132 |
|
102 | 133 | return workers; |
|
0 commit comments