| | | 1 | | using Amazon.SQS; |
| | | 2 | | using Amazon.SQS.Model; |
| | | 3 | | using Microsoft.Extensions.Hosting; |
| | | 4 | | using Microsoft.Extensions.Logging; |
| | | 5 | | using Microsoft.Extensions.Options; |
| | | 6 | | using System.Text.Json; |
| | | 7 | | |
| | | 8 | | namespace AsyncResponse.Transports.SQS; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Provisions the worker and response queues (each with a dead-letter queue wired through a native |
| | | 12 | | /// redrive policy) when <see cref="SqsAsyncResponseOptions.CreateQueues"/> is enabled. Registered |
| | | 13 | | /// before the subscribers so provisioning completes before consumption starts; queues configured as |
| | | 14 | | /// URLs are assumed to exist and are skipped. |
| | | 15 | | /// </summary> |
| | 214 | 16 | | internal sealed class SqsQueueProvisioningService( |
| | 214 | 17 | | IOptions<SqsAsyncResponseOptions> options, |
| | 214 | 18 | | ISqsClient client, |
| | 214 | 19 | | ILogger<SqsQueueProvisioningService> logger) : IHostedService |
| | | 20 | | { |
| | | 21 | | private const int MaxAttempts = 40; |
| | | 22 | | |
| | | 23 | | /// <summary>Creates the configured queues and their dead-letter pairs.</summary> |
| | | 24 | | public async Task StartAsync(CancellationToken cancellationToken) |
| | | 25 | | { |
| | 210 | 26 | | var o = options.Value; |
| | 210 | 27 | | if (!o.CreateQueues) |
| | 2 | 28 | | return; |
| | | 29 | | |
| | 208 | 30 | | SqsOptionsValidator.ValidateCommon(o); |
| | | 31 | | |
| | 1248 | 32 | | foreach (var queue in new[] { o.WorkerQueue, o.ResponseQueue }) |
| | | 33 | | { |
| | 416 | 34 | | if (SqsQueueAddress.IsUrl(queue)) |
| | | 35 | | { |
| | 2 | 36 | | logger.LogInformation("SQS queue {Queue} is configured as a URL; skipping provisioning.", queue); |
| | 2 | 37 | | continue; |
| | | 38 | | } |
| | | 39 | | |
| | 414 | 40 | | await EnsureQueueWithDeadLetterAsync(o, queue, cancellationToken).ConfigureAwait(false); |
| | | 41 | | } |
| | 210 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary>No-op.</summary> |
| | 196 | 45 | | public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; |
| | | 46 | | |
| | | 47 | | internal async Task EnsureQueueWithDeadLetterAsync( |
| | | 48 | | SqsAsyncResponseOptions o, |
| | | 49 | | string queueName, |
| | | 50 | | CancellationToken cancellationToken) |
| | | 51 | | { |
| | 416 | 52 | | var fifo = SqsQueueAddress.IsFifo(queueName); |
| | | 53 | | // A FIFO queue's dead-letter queue must itself be FIFO, and FIFO names must end in ".fifo". |
| | 416 | 54 | | var deadLetterQueueName = SqsQueueAddress.DeriveDeadLetterQueueName(queueName, o.DeadLetterQueueSuffix); |
| | | 55 | | |
| | 416 | 56 | | var fifoAttributes = fifo |
| | 416 | 57 | | ? new Dictionary<string, string>(StringComparer.Ordinal) { [QueueAttributeName.FifoQueue] = "true" } |
| | 416 | 58 | | : new Dictionary<string, string>(StringComparer.Ordinal); |
| | | 59 | | |
| | 416 | 60 | | var deadLetterQueueUrl = await CreateOrUpdateQueueAsync(deadLetterQueueName, fifoAttributes, cancellationToken) |
| | 416 | 61 | | .ConfigureAwait(false); |
| | 416 | 62 | | var deadLetterQueueArn = await RetryAsync( |
| | 416 | 63 | | () => client.GetQueueArnAsync(deadLetterQueueUrl, cancellationToken), |
| | 416 | 64 | | cancellationToken).ConfigureAwait(false); |
| | | 65 | | |
| | 416 | 66 | | var attributes = new Dictionary<string, string>(fifoAttributes, StringComparer.Ordinal) |
| | 416 | 67 | | { |
| | 416 | 68 | | [QueueAttributeName.RedrivePolicy] = AsyncResponseJson.Serialize(new Dictionary<string, string> |
| | 416 | 69 | | { |
| | 416 | 70 | | ["deadLetterTargetArn"] = deadLetterQueueArn, |
| | 416 | 71 | | ["maxReceiveCount"] = o.MaxReceiveCount.ToString() |
| | 416 | 72 | | }) |
| | 416 | 73 | | }; |
| | | 74 | | |
| | 416 | 75 | | await CreateOrUpdateQueueAsync(queueName, attributes, cancellationToken).ConfigureAwait(false); |
| | | 76 | | |
| | 416 | 77 | | logger.LogInformation( |
| | 416 | 78 | | "SQS queue {Queue} provisioned with dead-letter queue {DeadLetterQueue} (maxReceiveCount={MaxReceiveCount}). |
| | 416 | 79 | | queueName, |
| | 416 | 80 | | deadLetterQueueName, |
| | 416 | 81 | | o.MaxReceiveCount); |
| | 416 | 82 | | } |
| | | 83 | | |
| | | 84 | | private async Task<string> CreateOrUpdateQueueAsync( |
| | | 85 | | string queueName, |
| | | 86 | | IReadOnlyDictionary<string, string> attributes, |
| | | 87 | | CancellationToken cancellationToken) |
| | | 88 | | { |
| | 832 | 89 | | return await RetryAsync(CreateAsync, cancellationToken).ConfigureAwait(false); |
| | | 90 | | |
| | | 91 | | async Task<string> CreateAsync() |
| | | 92 | | { |
| | | 93 | | try |
| | | 94 | | { |
| | 836 | 95 | | return await client.CreateQueueAsync(queueName, attributes, cancellationToken).ConfigureAwait(false); |
| | | 96 | | } |
| | | 97 | | catch (QueueNameExistsException) |
| | | 98 | | { |
| | | 99 | | // The queue exists with different attributes (for example a redrive policy set by an |
| | | 100 | | // earlier run against a different DLQ ARN): converge by re-applying the attributes. |
| | | 101 | | // FifoQueue is create-only — SetQueueAttributes rejects it with InvalidAttributeName, |
| | | 102 | | // and RetryAsync then burned every attempt on that deterministic error before |
| | | 103 | | // aborting host startup (the FIFO dead-letter queue first, since it is created |
| | | 104 | | // with that attribute alone) — so it is dropped from the update; the queue is |
| | | 105 | | // already FIFO by the fact of its name. |
| | 14 | 106 | | var queueUrl = await client.GetQueueUrlAsync(queueName, cancellationToken).ConfigureAwait(false); |
| | 14 | 107 | | var updatable = new Dictionary<string, string>(StringComparer.Ordinal); |
| | 60 | 108 | | foreach (var attribute in attributes) |
| | | 109 | | { |
| | 16 | 110 | | if (!string.Equals(attribute.Key, QueueAttributeName.FifoQueue.Value, StringComparison.Ordinal)) |
| | 8 | 111 | | updatable[attribute.Key] = attribute.Value; |
| | | 112 | | } |
| | | 113 | | |
| | 14 | 114 | | if (updatable.Count > 0) |
| | 8 | 115 | | await client.SetQueueAttributesAsync(queueUrl, updatable, cancellationToken).ConfigureAwait(false); |
| | 14 | 116 | | return queueUrl; |
| | | 117 | | } |
| | 0 | 118 | | } |
| | 1664 | 119 | | } |
| | | 120 | | |
| | | 121 | | private async Task<T> RetryAsync<T>(Func<Task<T>> operation, CancellationToken cancellationToken) |
| | | 122 | | { |
| | 1248 | 123 | | var o = options.Value; |
| | 1252 | 124 | | for (var attempt = 1; ; attempt++) |
| | | 125 | | { |
| | | 126 | | try |
| | | 127 | | { |
| | 1252 | 128 | | return await operation().ConfigureAwait(false); |
| | | 129 | | } |
| | 4 | 130 | | catch (Exception ex) when (attempt < MaxAttempts && !cancellationToken.IsCancellationRequested) |
| | | 131 | | { |
| | | 132 | | // Startup ordering against a fresh endpoint (LocalStack still booting, transient |
| | | 133 | | // networking) resolves within a few retries; anything persistent still surfaces. |
| | 4 | 134 | | var delay = AsyncResponseRetry.Backoff(attempt, o.SubscriberRetryBaseDelay, o.SubscriberRetryMaxDelay); |
| | 4 | 135 | | logger.LogWarning(ex, "SQS queue provisioning attempt {Attempt} failed; retrying in {Delay}.", attempt, |
| | 4 | 136 | | await Task.Delay(delay, cancellationToken).ConfigureAwait(false); |
| | | 137 | | } |
| | | 138 | | } |
| | 1248 | 139 | | } |
| | | 140 | | } |