Skip to content

Commit 0a3129c

Browse files
committed
- Add AWs and Azure bus bootstrap and configuration.
1 parent ef2b9a2 commit 0a3129c

58 files changed

Lines changed: 3495 additions & 768 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎src/SourceFlow.Cloud.AWS/Infrastructure/AwsBusBootstrapper.cs‎

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
using Amazon.SimpleNotificationService;
2+
using Amazon.SimpleNotificationService.Model;
13
using Amazon.SQS;
24
using Amazon.SQS.Model;
3-
using Amazon.SimpleNotificationService;
45
using Microsoft.Extensions.Hosting;
56
using Microsoft.Extensions.Logging;
67
using SourceFlow.Cloud.Core.Configuration;
@@ -39,6 +40,17 @@ public async Task StartAsync(CancellationToken cancellationToken)
3940
{
4041
_logger.LogInformation("AwsBusBootstrapper: resolving SQS queues and SNS topics.");
4142

43+
// ── 0. Validate: subscribing to topics requires at least one command queue ──
44+
45+
if (_busConfiguration.SubscribedTopicNames.Count > 0 &&
46+
_busConfiguration.CommandListeningQueueNames.Count == 0)
47+
{
48+
throw new InvalidOperationException(
49+
"At least one command queue must be configured via .Listen.To.CommandQueue(...) " +
50+
"when subscribing to topics via .Subscribe.To.Topic(...). " +
51+
"SNS topic subscriptions require an SQS queue to receive events.");
52+
}
53+
4254
// ── 1. Collect all unique queue names ────────────────────────────────
4355

4456
var allQueueNames = _busConfiguration.CommandTypeToQueueName.Values
@@ -91,13 +103,34 @@ public async Task StartAsync(CancellationToken cancellationToken)
91103
.Select(name => topicArnMap[name])
92104
.ToList();
93105

94-
// ── 6. Inject resolved paths into configuration ───────────────────────
106+
// ── 6. Subscribe topics to the first command queue ─────────────────
107+
108+
var eventListeningUrls = new List<string>();
109+
110+
if (resolvedSubscribedTopicArns.Count > 0)
111+
{
112+
var targetQueueUrl = resolvedCommandListeningUrls[0];
113+
var targetQueueArn = await GetQueueArnAsync(targetQueueUrl, cancellationToken);
114+
115+
foreach (var topicArn in resolvedSubscribedTopicArns)
116+
{
117+
await SubscribeQueueToTopicAsync(topicArn, targetQueueArn, cancellationToken);
118+
_logger.LogInformation(
119+
"AwsBusBootstrapper: subscribed queue '{QueueArn}' to topic '{TopicArn}'.",
120+
targetQueueArn, topicArn);
121+
}
122+
123+
eventListeningUrls.Add(targetQueueUrl);
124+
}
125+
126+
// ── 7. Inject resolved paths into configuration ───────────────────────
95127

96128
_busConfiguration.Resolve(
97129
resolvedCommandRoutes,
98130
resolvedEventRoutes,
99131
resolvedCommandListeningUrls,
100-
resolvedSubscribedTopicArns);
132+
resolvedSubscribedTopicArns,
133+
eventListeningUrls);
101134

102135
_logger.LogInformation(
103136
"AwsBusBootstrapper: resolved {CommandCount} command route(s), " +
@@ -146,4 +179,26 @@ private async Task<string> GetOrCreateTopicAsync(string topicName, CancellationT
146179
var response = await _snsClient.CreateTopicAsync(topicName, ct);
147180
return response.TopicArn;
148181
}
182+
183+
private async Task<string> GetQueueArnAsync(string queueUrl, CancellationToken ct)
184+
{
185+
var response = await _sqsClient.GetQueueAttributesAsync(new GetQueueAttributesRequest
186+
{
187+
QueueUrl = queueUrl,
188+
AttributeNames = new List<string> { QueueAttributeName.QueueArn }
189+
}, ct);
190+
191+
return response.Attributes[QueueAttributeName.QueueArn];
192+
}
193+
194+
private async Task SubscribeQueueToTopicAsync(string topicArn, string queueArn, CancellationToken ct)
195+
{
196+
// SubscribeAsync is idempotent: returns the existing subscription ARN if already subscribed.
197+
await _snsClient.SubscribeAsync(new SubscribeRequest
198+
{
199+
TopicArn = topicArn,
200+
Protocol = "sqs",
201+
Endpoint = queueArn
202+
}, ct);
203+
}
149204
}

‎src/SourceFlow.Cloud.AWS/Messaging/Commands/AwsSqsCommandDispatcher.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ public AwsSqsCommandDispatcher(
3737
public async Task Dispatch<TCommand>(TCommand command) where TCommand : ICommand
3838
{
3939
// 1. Check if this command type should be routed to AWS
40-
if (!_routingConfig.ShouldRouteToAws<TCommand>())
40+
if (!_routingConfig.ShouldRoute<TCommand>())
4141
return; // Skip this dispatcher
4242

4343
try
4444
{
4545
// 2. Get queue URL for command type
46-
var queueUrl = _routingConfig.GetQueueUrl<TCommand>();
46+
var queueUrl = _routingConfig.GetQueueName<TCommand>();
4747

4848
// 3. Serialize command to JSON
4949
var messageBody = JsonSerializer.Serialize(command, _jsonOptions);

‎src/SourceFlow.Cloud.AWS/Messaging/Commands/AwsSqsCommandDispatcherEnhanced.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ public AwsSqsCommandDispatcherEnhanced(
5959
public async Task Dispatch<TCommand>(TCommand command) where TCommand : ICommand
6060
{
6161
// Check if this command type should be routed to AWS
62-
if (!_routingConfig.ShouldRouteToAws<TCommand>())
62+
if (!_routingConfig.ShouldRoute<TCommand>())
6363
return;
6464

6565
var commandType = typeof(TCommand).Name;
66-
var queueUrl = _routingConfig.GetQueueUrl<TCommand>();
66+
var queueUrl = _routingConfig.GetQueueName<TCommand>();
6767
var sw = Stopwatch.StartNew();
6868

6969
// Start distributed trace activity

‎src/SourceFlow.Cloud.AWS/Messaging/Events/AwsSnsEventDispatcher.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ public AwsSnsEventDispatcher(
3737
public async Task Dispatch<TEvent>(TEvent @event) where TEvent : IEvent
3838
{
3939
// 1. Check if this event type should be routed to AWS
40-
if (!_routingConfig.ShouldRouteToAws<TEvent>())
40+
if (!_routingConfig.ShouldRoute<TEvent>())
4141
return; // Skip this dispatcher
4242

4343
try
4444
{
4545
// 2. Get topic ARN for event type
46-
var topicArn = _routingConfig.GetTopicArn<TEvent>();
46+
var topicArn = _routingConfig.GetTopicName<TEvent>();
4747

4848
// 3. Serialize event to JSON
4949
var messageBody = JsonSerializer.Serialize(@event, _jsonOptions);

‎src/SourceFlow.Cloud.AWS/Messaging/Events/AwsSnsEventDispatcherEnhanced.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ public AwsSnsEventDispatcherEnhanced(
5959
public async Task Dispatch<TEvent>(TEvent @event) where TEvent : IEvent
6060
{
6161
// Check if this event type should be routed to AWS
62-
if (!_routingConfig.ShouldRouteToAws<TEvent>())
62+
if (!_routingConfig.ShouldRoute<TEvent>())
6363
return;
6464

6565
var eventType = typeof(TEvent).Name;
66-
var topicArn = _routingConfig.GetTopicArn<TEvent>();
66+
var topicArn = _routingConfig.GetTopicName<TEvent>();
6767
var sw = Stopwatch.StartNew();
6868

6969
// Start distributed trace activity

‎src/SourceFlow.Cloud.Azure/Attributes/AzureCommandRoutingAttribute.cs‎

Lines changed: 0 additions & 16 deletions
This file was deleted.

‎src/SourceFlow.Cloud.Azure/Configuration/ConfigurationBasedAzureCommandRouting.cs‎

Lines changed: 0 additions & 84 deletions
This file was deleted.

‎src/SourceFlow.Cloud.Azure/Configuration/ConfigurationBasedAzureEventRouting.cs‎

Lines changed: 0 additions & 90 deletions
This file was deleted.

‎src/SourceFlow.Cloud.Azure/Configuration/IAzureCommandRoutingConfiguration.cs‎

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)