| | | 1 | | using AsyncResponse; |
| | | 2 | | using AsyncResponse.Transports.SQS; |
| | | 3 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 4 | | |
| | | 5 | | namespace Microsoft.Extensions.DependencyInjection; |
| | | 6 | | |
| | | 7 | | /// <summary>DI registration for the AWS SQS AsyncResponse transport.</summary> |
| | | 8 | | public static class SqsAsyncResponseServiceCollectionExtensions |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Registers AWS SQS as the worker transport and response ingress in one call: |
| | | 12 | | /// <list type="bullet"> |
| | | 13 | | /// <item><description>worker jobs are published to <see cref="SqsAsyncResponseOptions.WorkerQueue"/>;</description> |
| | | 14 | | /// <item><description>a hosted subscriber long-polls that worker queue and executes worker jobs;</description></ite |
| | | 15 | | /// <item><description>a hosted subscriber long-polls <see cref="SqsAsyncResponseOptions.ResponseQueue"/> and feeds |
| | | 16 | | /// </list> |
| | | 17 | | /// Redelivery and dead-lettering stay native: a failed handler leaves the message for the |
| | | 18 | | /// visibility timeout, and the queue's redrive policy moves it to the dead-letter queue after |
| | | 19 | | /// <c>maxReceiveCount</c> receives (provision both with |
| | | 20 | | /// <see cref="SqsAsyncResponseOptions.CreateQueues"/> or your infrastructure tooling). SQS is a |
| | | 21 | | /// transport, not a recovery store: pair it with a channel (<c>.WithInMemoryChannel()</c> for |
| | | 22 | | /// simple apps, or a durable channel such as <c>.WithRedisChannel()</c>, |
| | | 23 | | /// <c>.WithPostgreSqlChannel()</c>, or <c>.WithNatsChannel()</c> when late responses must |
| | | 24 | | /// survive redeploys). An application-registered <c>Amazon.SQS.IAmazonSQS</c> singleton (for |
| | | 25 | | /// example from <c>AWSSDK.Extensions.NETCore.Setup</c>) is reused when present. |
| | | 26 | | /// </summary> |
| | | 27 | | public static AsyncResponseRegistrationBuilder WithSqsTransport( |
| | | 28 | | this AsyncResponseRegistrationBuilder builder, |
| | | 29 | | Action<SqsAsyncResponseOptions> configure) |
| | | 30 | | { |
| | 3 | 31 | | ArgumentNullException.ThrowIfNull(configure); |
| | | 32 | | |
| | 3 | 33 | | var services = builder.Services; |
| | 3 | 34 | | services.AddOptions(); |
| | 3 | 35 | | services.Configure(configure); |
| | | 36 | | |
| | 3 | 37 | | services.TryAddSingleton(SqsClientResolver.Create); |
| | 3 | 38 | | services.TryAddSingleton(provider => new SqsWorkerTransport( |
| | 3 | 39 | | provider.GetRequiredService<Microsoft.Extensions.Options.IOptions<SqsAsyncResponseOptions>>(), |
| | 3 | 40 | | provider.GetRequiredService<ISqsClient>())); |
| | 3 | 41 | | services.Replace(ServiceDescriptor.Singleton<IWorkerTransport>(provider => |
| | 3 | 42 | | provider.GetRequiredService<SqsWorkerTransport>())); |
| | 3 | 43 | | services.Replace(ServiceDescriptor.Singleton<IAsyncResponseReplyTargetProvider, SqsReplyTargetProvider>()); |
| | 3 | 44 | | services.AddSingleton(provider => |
| | 3 | 45 | | { |
| | 3 | 46 | | // Resolved ack modes declared to the Core startup validator, which vetoes early ACK on |
| | 3 | 47 | | // the worker queue durable-flow wake-ups ride (see AsyncResponseStartupValidator). |
| | 3 | 48 | | var options = provider.GetRequiredService<Microsoft.Extensions.Options.IOptions<SqsAsyncResponseOptions>>(). |
| | 3 | 49 | | return new AsyncResponseTransportMarker(SqsAsyncResponseOptions.TransportName) |
| | 3 | 50 | | { |
| | 3 | 51 | | WorkerSubscriberUsesEarlyAck = options.WorkerSubscriber.AckMode == SqsAckMode.AckAfterEnqueue, |
| | 3 | 52 | | WorkerAckModePath = $"{nameof(SqsAsyncResponseOptions)}.{nameof(options.WorkerSubscriber)}.{nameof(optio |
| | 3 | 53 | | ResponseSubscriberUsesEarlyAck = options.ResponseSubscriber.AckMode == SqsAckMode.AckAfterEnqueue, |
| | 3 | 54 | | ResponseAckModePath = $"{nameof(SqsAsyncResponseOptions)}.{nameof(options.ResponseSubscriber)}.{nameof(o |
| | 3 | 55 | | }; |
| | 3 | 56 | | }); |
| | | 57 | | |
| | | 58 | | // Registered before the subscribers: hosted services start in registration order, so |
| | | 59 | | // CreateQueues provisioning completes before the first ReceiveMessage loop begins. |
| | 3 | 60 | | services.AddHostedService<SqsQueueProvisioningService>(); |
| | 3 | 61 | | services.AddHostedService<SqsWorkerSubscriber>(); |
| | 3 | 62 | | services.AddHostedService<SqsResponseIngressSubscriber>(); |
| | | 63 | | |
| | 3 | 64 | | return builder; |
| | | 65 | | } |
| | | 66 | | } |