|
| 1 | +using Amazon.SQS; |
| 2 | +using Amazon.SQS.Model; |
| 3 | +using Amazon.SimpleNotificationService; |
| 4 | +using Microsoft.Extensions.Hosting; |
| 5 | +using Microsoft.Extensions.Logging; |
| 6 | +using SourceFlow.Cloud.Core.Configuration; |
| 7 | + |
| 8 | +namespace SourceFlow.Cloud.AWS.Infrastructure; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// Hosted service that runs once at application startup to ensure all configured SQS queues |
| 12 | +/// and SNS topics exist in AWS, then resolves short names to full URLs/ARNs and injects them |
| 13 | +/// into <see cref="IBusBootstrapConfiguration"/> via <c>Resolve()</c>. |
| 14 | +/// </summary> |
| 15 | +/// <remarks> |
| 16 | +/// Must be registered as a hosted service <b>before</b> <c>AwsSqsCommandListener</c> and |
| 17 | +/// <c>AwsSnsEventListener</c> so that routing is fully resolved before any polling begins. |
| 18 | +/// </remarks> |
| 19 | +public sealed class AwsBusBootstrapper : IHostedService |
| 20 | +{ |
| 21 | + private readonly IBusBootstrapConfiguration _busConfiguration; |
| 22 | + private readonly IAmazonSQS _sqsClient; |
| 23 | + private readonly IAmazonSimpleNotificationService _snsClient; |
| 24 | + private readonly ILogger<AwsBusBootstrapper> _logger; |
| 25 | + |
| 26 | + public AwsBusBootstrapper( |
| 27 | + IBusBootstrapConfiguration busConfiguration, |
| 28 | + IAmazonSQS sqsClient, |
| 29 | + IAmazonSimpleNotificationService snsClient, |
| 30 | + ILogger<AwsBusBootstrapper> logger) |
| 31 | + { |
| 32 | + _busConfiguration = busConfiguration; |
| 33 | + _sqsClient = sqsClient; |
| 34 | + _snsClient = snsClient; |
| 35 | + _logger = logger; |
| 36 | + } |
| 37 | + |
| 38 | + public async Task StartAsync(CancellationToken cancellationToken) |
| 39 | + { |
| 40 | + _logger.LogInformation("AwsBusBootstrapper: resolving SQS queues and SNS topics."); |
| 41 | + |
| 42 | + // ── 1. Collect all unique queue names ──────────────────────────────── |
| 43 | + |
| 44 | + var allQueueNames = _busConfiguration.CommandTypeToQueueName.Values |
| 45 | + .Concat(_busConfiguration.CommandListeningQueueNames) |
| 46 | + .Distinct(StringComparer.OrdinalIgnoreCase) |
| 47 | + .ToList(); |
| 48 | + |
| 49 | + // ── 2. Resolve (or create) every queue ────────────────────────────── |
| 50 | + |
| 51 | + var queueUrlMap = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); |
| 52 | + |
| 53 | + foreach (var queueName in allQueueNames) |
| 54 | + { |
| 55 | + var url = await GetOrCreateQueueAsync(queueName, cancellationToken); |
| 56 | + queueUrlMap[queueName] = url; |
| 57 | + _logger.LogDebug("AwsBusBootstrapper: queue '{QueueName}' → {Url}", queueName, url); |
| 58 | + } |
| 59 | + |
| 60 | + // ── 3. Collect all unique topic names ──────────────────────────────── |
| 61 | + |
| 62 | + var allTopicNames = _busConfiguration.EventTypeToTopicName.Values |
| 63 | + .Concat(_busConfiguration.SubscribedTopicNames) |
| 64 | + .Distinct(StringComparer.OrdinalIgnoreCase) |
| 65 | + .ToList(); |
| 66 | + |
| 67 | + // ── 4. Resolve (or create) every topic ─────────────────────────────── |
| 68 | + |
| 69 | + var topicArnMap = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); |
| 70 | + |
| 71 | + foreach (var topicName in allTopicNames) |
| 72 | + { |
| 73 | + var arn = await GetOrCreateTopicAsync(topicName, cancellationToken); |
| 74 | + topicArnMap[topicName] = arn; |
| 75 | + _logger.LogDebug("AwsBusBootstrapper: topic '{TopicName}' → {Arn}", topicName, arn); |
| 76 | + } |
| 77 | + |
| 78 | + // ── 5. Build resolved dictionaries ─────────────────────────────────── |
| 79 | + |
| 80 | + var resolvedCommandRoutes = _busConfiguration.CommandTypeToQueueName |
| 81 | + .ToDictionary(kv => kv.Key, kv => queueUrlMap[kv.Value]); |
| 82 | + |
| 83 | + var resolvedEventRoutes = _busConfiguration.EventTypeToTopicName |
| 84 | + .ToDictionary(kv => kv.Key, kv => topicArnMap[kv.Value]); |
| 85 | + |
| 86 | + var resolvedCommandListeningUrls = _busConfiguration.CommandListeningQueueNames |
| 87 | + .Select(name => queueUrlMap[name]) |
| 88 | + .ToList(); |
| 89 | + |
| 90 | + var resolvedSubscribedTopicArns = _busConfiguration.SubscribedTopicNames |
| 91 | + .Select(name => topicArnMap[name]) |
| 92 | + .ToList(); |
| 93 | + |
| 94 | + // ── 6. Inject resolved paths into configuration ─────────────────────── |
| 95 | + |
| 96 | + _busConfiguration.Resolve( |
| 97 | + resolvedCommandRoutes, |
| 98 | + resolvedEventRoutes, |
| 99 | + resolvedCommandListeningUrls, |
| 100 | + resolvedSubscribedTopicArns); |
| 101 | + |
| 102 | + _logger.LogInformation( |
| 103 | + "AwsBusBootstrapper: resolved {CommandCount} command route(s), " + |
| 104 | + "{EventCount} event route(s), {ListenCount} listening queue(s), " + |
| 105 | + "{SubscribeCount} subscribed topic(s).", |
| 106 | + resolvedCommandRoutes.Count, |
| 107 | + resolvedEventRoutes.Count, |
| 108 | + resolvedCommandListeningUrls.Count, |
| 109 | + resolvedSubscribedTopicArns.Count); |
| 110 | + } |
| 111 | + |
| 112 | + public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; |
| 113 | + |
| 114 | + // ── Helpers ────────────────────────────────────────────────────────────── |
| 115 | + |
| 116 | + private async Task<string> GetOrCreateQueueAsync(string queueName, CancellationToken ct) |
| 117 | + { |
| 118 | + try |
| 119 | + { |
| 120 | + var response = await _sqsClient.GetQueueUrlAsync(queueName, ct); |
| 121 | + return response.QueueUrl; |
| 122 | + } |
| 123 | + catch (QueueDoesNotExistException) |
| 124 | + { |
| 125 | + _logger.LogInformation("AwsBusBootstrapper: queue '{QueueName}' not found — creating.", queueName); |
| 126 | + |
| 127 | + var request = new CreateQueueRequest { QueueName = queueName }; |
| 128 | + |
| 129 | + if (queueName.EndsWith(".fifo", StringComparison.OrdinalIgnoreCase)) |
| 130 | + { |
| 131 | + request.Attributes = new Dictionary<string, string> |
| 132 | + { |
| 133 | + [QueueAttributeName.FifoQueue] = "true", |
| 134 | + [QueueAttributeName.ContentBasedDeduplication] = "true" |
| 135 | + }; |
| 136 | + } |
| 137 | + |
| 138 | + var created = await _sqsClient.CreateQueueAsync(request, ct); |
| 139 | + return created.QueueUrl; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + private async Task<string> GetOrCreateTopicAsync(string topicName, CancellationToken ct) |
| 144 | + { |
| 145 | + // CreateTopicAsync is idempotent: returns the existing ARN when the topic already exists. |
| 146 | + var response = await _snsClient.CreateTopicAsync(topicName, ct); |
| 147 | + return response.TopicArn; |
| 148 | + } |
| 149 | +} |
0 commit comments