| | | 1 | | using AsyncResponse; |
| | | 2 | | using AsyncResponse.Transports.GooglePubSub; |
| | | 3 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 4 | | |
| | | 5 | | namespace Microsoft.Extensions.DependencyInjection; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// DI registration for the Google Pub/Sub AsyncResponse transport. |
| | | 9 | | /// </summary> |
| | | 10 | | public static class GooglePubSubAsyncResponseServiceCollectionExtensions |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Registers Google Pub/Sub as the worker transport and response ingress in one call: |
| | | 14 | | /// <list type="bullet"> |
| | | 15 | | /// <item><description>worker jobs are published to <see cref="GooglePubSubAsyncResponseOptions.WorkerTopicId"/>;</d |
| | | 16 | | /// <item><description>a hosted subscriber consumes <see cref="GooglePubSubAsyncResponseOptions.WorkerSubscriptionId |
| | | 17 | | /// <item><description>a hosted subscriber consumes <see cref="GooglePubSubAsyncResponseOptions.ResponseSubscription |
| | | 18 | | /// </list> |
| | | 19 | | /// Pub/Sub is a transport, not a recovery store: pair it with a channel |
| | | 20 | | /// (<c>.WithInMemoryChannel()</c> for simple apps, or <c>.WithRedisChannel()</c> when late |
| | | 21 | | /// responses must survive redeploys), which provides the waiter side and recovery state. |
| | | 22 | | /// </summary> |
| | | 23 | | public static AsyncResponseRegistrationBuilder WithGooglePubSubTransport( |
| | | 24 | | this AsyncResponseRegistrationBuilder builder, |
| | | 25 | | Action<GooglePubSubAsyncResponseOptions> configure) |
| | | 26 | | { |
| | 3 | 27 | | ArgumentNullException.ThrowIfNull(configure); |
| | | 28 | | |
| | 3 | 29 | | var services = builder.Services; |
| | 3 | 30 | | services.AddOptions(); |
| | 3 | 31 | | services.Configure(configure); |
| | | 32 | | |
| | 3 | 33 | | services.TryAddSingleton<GooglePubSubWorkerTransport>(); |
| | 3 | 34 | | services.Replace(ServiceDescriptor.Singleton<IWorkerTransport>(provider => |
| | 3 | 35 | | provider.GetRequiredService<GooglePubSubWorkerTransport>())); |
| | 3 | 36 | | services.Replace(ServiceDescriptor.Singleton<IAsyncResponseReplyTargetProvider, GooglePubSubReplyTargetProvider> |
| | 3 | 37 | | services.AddSingleton(provider => |
| | 3 | 38 | | { |
| | 3 | 39 | | // Resolved ack modes declared to the Core startup validator, which vetoes early ACK on |
| | 3 | 40 | | // the worker queue durable-flow wake-ups ride (see AsyncResponseStartupValidator). |
| | 3 | 41 | | var options = provider.GetRequiredService<Microsoft.Extensions.Options.IOptions<GooglePubSubAsyncResponseOpt |
| | 3 | 42 | | return new AsyncResponseTransportMarker("GooglePubSub") |
| | 3 | 43 | | { |
| | 3 | 44 | | WorkerSubscriberUsesEarlyAck = options.WorkerSubscriber.AckMode == GooglePubSubAckMode.AckAfterEnqueue, |
| | 3 | 45 | | WorkerAckModePath = $"{nameof(GooglePubSubAsyncResponseOptions)}.{nameof(options.WorkerSubscriber)}.{nam |
| | 3 | 46 | | ResponseSubscriberUsesEarlyAck = options.ResponseSubscriber.AckMode == GooglePubSubAckMode.AckAfterEnque |
| | 3 | 47 | | ResponseAckModePath = $"{nameof(GooglePubSubAsyncResponseOptions)}.{nameof(options.ResponseSubscriber)}. |
| | 3 | 48 | | }; |
| | 3 | 49 | | }); |
| | | 50 | | |
| | 3 | 51 | | services.AddHostedService<GooglePubSubWorkerSubscriber>(); |
| | 3 | 52 | | services.AddHostedService<GooglePubSubResponseIngressSubscriber>(); |
| | | 53 | | |
| | 3 | 54 | | return builder; |
| | | 55 | | } |
| | | 56 | | } |