| | | 1 | | using AsyncResponse; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 3 | | using Microsoft.Extensions.Options; |
| | | 4 | | using System.Diagnostics.CodeAnalysis; |
| | | 5 | | |
| | | 6 | | namespace Microsoft.Extensions.DependencyInjection; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Core registrations for AsyncResponse. Everything is configured through the fluent builder |
| | | 10 | | /// returned by <see cref="AddAsyncResponse"/>: chain exactly one channel and exactly one worker |
| | | 11 | | /// transport, and exactly one durable-flow state store. |
| | | 12 | | /// </summary> |
| | | 13 | | public static class AsyncResponseCoreServiceCollectionExtensions |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Registers the channel-agnostic AsyncResponse engine (fluent waiter builder, transport-neutral |
| | | 17 | | /// ingress, worker-job executor, and the recovery watchdog) and returns a builder to configure |
| | | 18 | | /// the rest. It deliberately registers <em>no</em> response channel: chain exactly one |
| | | 19 | | /// (<see cref="WithInMemoryChannel"/> or the Redis channel package's <c>WithRedisChannel</c>) and |
| | | 20 | | /// exactly one worker transport (<see cref="WithInMemoryTransport"/> or a broker transport |
| | | 21 | | /// package such as <c>WithGooglePubSubTransport</c> / <c>WithRabbitMqTransport</c>), and exactly |
| | | 22 | | /// one durable-flow state store (<see cref="WithInMemoryDurableFlows"/> or a provider package). |
| | | 23 | | /// An app that starts without any of these choices fails fast at host startup. |
| | | 24 | | /// </summary> |
| | | 25 | | public static AsyncResponseRegistrationBuilder AddAsyncResponse( |
| | | 26 | | this IServiceCollection services, |
| | | 27 | | Action<AsyncResponseOptions>? configure = null) |
| | | 28 | | { |
| | 3394 | 29 | | services.AddOptions(); |
| | 3394 | 30 | | if (configure is not null) |
| | | 31 | | { |
| | 2235 | 32 | | services.Configure(configure); |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | // The engine's single clock. Every Core component that reads time or arms a timer resolves |
| | | 36 | | // this TimeProvider, so a test host (AsyncResponse.Testing's VirtualTimeProvider) can make |
| | | 37 | | // waits, timeouts, leases, timers, and schedules run on virtual time. Defaults to the |
| | | 38 | | // system clock; TryAdd lets a host (or the test harness) pre-register its own. |
| | 3394 | 39 | | services.TryAddSingleton(TimeProvider.System); |
| | | 40 | | |
| | | 41 | | // Channel-agnostic engine. |
| | 3394 | 42 | | services.TryAddSingleton<AsyncResponseContextPropagation>(); |
| | 3394 | 43 | | services.TryAddSingleton<WorkerJobExecutor>(); |
| | 3394 | 44 | | services.TryAddSingleton<IAsyncResponseIngress, AsyncResponseIngress>(); |
| | 3394 | 45 | | services.TryAddSingleton<IAsyncResponseBuilder>(provider => new AsyncResponseBuilder( |
| | 3394 | 46 | | provider.GetRequiredService<IAsyncResponseSubscriber>(), |
| | 3394 | 47 | | provider.GetService<IWorkerTransport>(), |
| | 3394 | 48 | | provider.GetService<IAsyncResponseReplyTargetProvider>(), |
| | 3394 | 49 | | provider.GetRequiredService<AsyncResponseContextPropagation>(), |
| | 3394 | 50 | | provider.GetService<TimeProvider>(), |
| | 3394 | 51 | | // The producer-side mirror of the ingress's inbound size budget (WorkerJobTooLargeException). |
| | 3394 | 52 | | provider.GetService<IOptions<AsyncResponseOptions>>())); |
| | | 53 | | |
| | | 54 | | // Fail fast before background services do any real work if the required channel, |
| | | 55 | | // transport, and durable-flow store choices were not made explicitly. TryAddEnumerable |
| | | 56 | | // (keyed by implementation type) keeps a second AddAsyncResponse() call from registering a |
| | | 57 | | // second validator or watchdog instance. |
| | 3394 | 58 | | services.TryAddEnumerable(ServiceDescriptor.Singleton<Microsoft.Extensions.Hosting.IHostedService, AsyncResponse |
| | | 59 | | |
| | | 60 | | // The recovery watchdog is part of the engine and runs by default for whatever channel is |
| | | 61 | | // registered (scanning + liveness go through IRecoveryStateScanner / IActiveSubscriberProbe). |
| | 3394 | 62 | | services.TryAddSingleton<AsyncResponseWatchdogState>(); |
| | 3394 | 63 | | services.TryAddEnumerable(ServiceDescriptor.Singleton<Microsoft.Extensions.Hosting.IHostedService, AsyncResponse |
| | | 64 | | |
| | 3394 | 65 | | return new AsyncResponseRegistrationBuilder(services); |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Uses an application-owned store for durable-flow state. The store must implement atomic |
| | | 70 | | /// creation, revision-checked updates, and execution leases. Use |
| | | 71 | | /// <see cref="WithInMemoryDurableFlows"/> explicitly for a process-local development or test |
| | | 72 | | /// store; <see cref="AddAsyncResponse"/> does not select one implicitly. |
| | | 73 | | /// </summary> |
| | | 74 | | public static AsyncResponseRegistrationBuilder WithDurableFlows<[DynamicallyAccessedMembers(DynamicallyAccessedMembe |
| | | 75 | | this AsyncResponseRegistrationBuilder builder, |
| | | 76 | | Action<DurableFlowOptions>? configure = null) |
| | | 77 | | where TFlowStateStore : class, IFlowStateStore |
| | 850 | 78 | | => builder.WithDurableFlows<TFlowStateStore, DurableFlowOptions>(configure); |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Registers an application or provider-owned durable-flow store whose options combine the |
| | | 82 | | /// common <see cref="DurableFlowOptions"/> settings with store-specific settings. Provider |
| | | 83 | | /// packages use this overload to expose one cohesive <c>With*DurableFlows(...)</c> callback. |
| | | 84 | | /// </summary> |
| | | 85 | | public static AsyncResponseRegistrationBuilder WithDurableFlows<[DynamicallyAccessedMembers(DynamicallyAccessedMembe |
| | | 86 | | this AsyncResponseRegistrationBuilder builder, |
| | | 87 | | Action<TOptions>? configure = null) |
| | | 88 | | where TFlowStateStore : class, IFlowStateStore |
| | | 89 | | where TOptions : DurableFlowOptions |
| | | 90 | | { |
| | 2692 | 91 | | builder.Services.AddOptions<TOptions>(); |
| | 2692 | 92 | | if (configure is not null) |
| | 2427 | 93 | | builder.Services.Configure(configure); |
| | | 94 | | |
| | | 95 | | // The engine consumes the same configured instance as the provider store, viewed through |
| | | 96 | | // the common base type. This keeps all durable-flow settings in one callback without a |
| | | 97 | | // second options object or per-execution adaptation/allocation. |
| | 2692 | 98 | | builder.Services.AddSingleton<DurableFlowOptions>(provider => |
| | 5256 | 99 | | provider.GetRequiredService<Microsoft.Extensions.Options.IOptions<TOptions>>().Value); |
| | | 100 | | |
| | | 101 | | // Forward the interface to the concrete registration so resolving either yields the same |
| | | 102 | | // instance within a scope. TryAdd lets callers (and the DurableFlows.* packages) pre-register |
| | | 103 | | // the concrete type with a different lifetime — e.g. singleton for stores whose dependencies |
| | | 104 | | // are all singletons — without this scoped default overriding it. The forward MIRRORS the |
| | | 105 | | // concrete registration's lifetime: a scoped factory that returns a root-owned singleton |
| | | 106 | | // would be captured into every flow-execution scope's disposable list, so the first scope's |
| | | 107 | | // disposal would dispose the store (and any connections it owns) for the whole process. |
| | | 108 | | // The mirror is a REGISTRATION-TIME snapshot — a concrete registration added after this |
| | | 109 | | // call with a different lifetime silently re-opens that hazard, so the marker carries the |
| | | 110 | | // snapshot and the startup validator re-checks it against the final collection. |
| | 2692 | 111 | | builder.Services.TryAddScoped<TFlowStateStore>(); |
| | 16941 | 112 | | var storeLifetime = builder.Services.Last(d => d.ServiceType == typeof(TFlowStateStore)).Lifetime; |
| | 2692 | 113 | | builder.Services.Add(ServiceDescriptor.Describe( |
| | 2692 | 114 | | typeof(IFlowStateStore), |
| | 2576 | 115 | | static provider => provider.GetRequiredService<TFlowStateStore>(), |
| | 2692 | 116 | | storeLifetime)); |
| | 2692 | 117 | | builder.Services.AddSingleton(new AsyncResponseDurableFlowStoreMarker(typeof(TFlowStateStore), storeLifetime, bu |
| | 2692 | 118 | | AddDurableFlowEngine(builder.Services); |
| | 2692 | 119 | | return builder; |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Registers a durable flow class for execution: adds it to DI (scoped, unless the app |
| | | 124 | | /// pre-registered it with another lifetime) and records a statically-typed execution route the |
| | | 125 | | /// flow executor prefers over reflection-based type-name resolution. Registration is optional |
| | | 126 | | /// on JIT deployments (unregistered flows resolve reflectively as before) and required for |
| | | 127 | | /// flows executed in trimmed/Native AOT apps, where persisted type names cannot root code. |
| | | 128 | | /// </summary> |
| | | 129 | | /// <typeparam name="TFlow">The flow class; its full name is the persisted <see cref="FlowState.FlowTypeName"/>.</ty |
| | | 130 | | /// <typeparam name="TInput">The flow input type, persisted as JSON with the flow state.</typeparam> |
| | | 131 | | public static AsyncResponseRegistrationBuilder WithDurableFlow<[DynamicallyAccessedMembers(DynamicallyAccessedMember |
| | | 132 | | this AsyncResponseRegistrationBuilder builder) |
| | | 133 | | where TFlow : class, IDurableFlow<TInput> |
| | | 134 | | { |
| | 2274 | 135 | | builder.Services.TryAddScoped<TFlow>(); |
| | 2274 | 136 | | builder.Services.AddSingleton(new DurableFlowRegistration |
| | 2274 | 137 | | { |
| | 2274 | 138 | | FlowTypeFullName = typeof(TFlow).FullName |
| | 2274 | 139 | | ?? throw new InvalidOperationException("Durable flow classes must have a FullName."), |
| | 2274 | 140 | | InputTypeFullName = typeof(TInput).FullName |
| | 2274 | 141 | | ?? throw new InvalidOperationException("Durable flow input types must have a FullName."), |
| | 2274 | 142 | | FlowType = typeof(TFlow), |
| | 1771 | 143 | | DeserializeInput = static json => JsonSafety.SafeDeserialize<TInput>(json), |
| | 2274 | 144 | | ExecuteAsync = static (flow, context, input) => |
| | 1773 | 145 | | ((TFlow)flow).ExecuteAsync(context, input is null ? default! : (TInput)input) |
| | 2274 | 146 | | }); |
| | 2274 | 147 | | return builder; |
| | | 148 | | } |
| | | 149 | | |
| | | 150 | | /// <summary> |
| | | 151 | | /// Starts <typeparamref name="TFlow"/> on a cron schedule. Occurrences carry deterministic run |
| | | 152 | | /// ids (<c>sched:{name}:{occurrenceUtc}</c>), so any number of replicas can run the scheduler |
| | | 153 | | /// and the flow store's atomic create guarantees exactly one run per occurrence — no leader |
| | | 154 | | /// election. Occurrences missed while no replica was up are skipped (at-most-once schedule). |
| | | 155 | | /// The flow type is also registered for statically-typed (AOT-safe) execution, exactly like |
| | | 156 | | /// <see cref="WithDurableFlow{TFlow, TInput}"/>. |
| | | 157 | | /// </summary> |
| | | 158 | | /// <param name="builder">The registration builder.</param> |
| | | 159 | | /// <param name="name"> |
| | | 160 | | /// Unique schedule name, embedded in every occurrence's flow id. Keep it short and stable — |
| | | 161 | | /// renaming orphans no state but changes the ids future occurrences dedup on. |
| | | 162 | | /// </param> |
| | | 163 | | /// <param name="cron"> |
| | | 164 | | /// Five-field cron expression (<c>minute hour day-of-month month day-of-week</c>); see |
| | | 165 | | /// <see cref="CronSchedule"/> for the supported syntax and DST semantics. Validated here, so a |
| | | 166 | | /// typo fails at registration rather than silently never firing. |
| | | 167 | | /// </param> |
| | | 168 | | /// <param name="input"> |
| | | 169 | | /// Builds the flow input for an occurrence (its scheduled UTC instant is passed in). Must be |
| | | 170 | | /// deterministic across replicas: every replica computes the same occurrence, and the |
| | | 171 | | /// idempotent-start check compares the input value. |
| | | 172 | | /// </param> |
| | | 173 | | /// <param name="configure">Optional per-schedule options (time zone, enabled).</param> |
| | | 174 | | public static AsyncResponseRegistrationBuilder WithScheduledFlow<[DynamicallyAccessedMembers(DynamicallyAccessedMemb |
| | | 175 | | this AsyncResponseRegistrationBuilder builder, |
| | | 176 | | string name, |
| | | 177 | | string cron, |
| | | 178 | | Func<DateTimeOffset, TInput> input, |
| | | 179 | | Action<ScheduledFlowOptions>? configure = null) |
| | | 180 | | where TFlow : class, IDurableFlow<TInput> |
| | | 181 | | { |
| | 40 | 182 | | ArgumentException.ThrowIfNullOrWhiteSpace(name); |
| | 40 | 183 | | ArgumentException.ThrowIfNullOrWhiteSpace(cron); |
| | 40 | 184 | | ArgumentNullException.ThrowIfNull(input); |
| | | 185 | | |
| | | 186 | | // Names key the deterministic occurrence ids (sched:{name}:{occurrence}); a duplicate |
| | | 187 | | // would make two schedules dedup against each other's runs. Fail at the registration call |
| | | 188 | | // site — a BackgroundService fault at startup surfaces far less directly. |
| | 40 | 189 | | if (builder.Services.Any(descriptor => |
| | 1208 | 190 | | descriptor.ServiceType == typeof(ScheduledFlowRegistration) |
| | 1208 | 191 | | && descriptor.ImplementationInstance is ScheduledFlowRegistration existing |
| | 1208 | 192 | | && string.Equals(existing.Name, name, StringComparison.Ordinal))) |
| | | 193 | | { |
| | 2 | 194 | | throw new InvalidOperationException( |
| | 2 | 195 | | $"A scheduled flow named '{name}' is already registered. Schedule names key the deterministic occurrence |
| | 2 | 196 | | "(sched:{name}:{occurrence}), so each WithScheduledFlow registration needs a unique name."); |
| | | 197 | | } |
| | | 198 | | |
| | 38 | 199 | | var options = new ScheduledFlowOptions(); |
| | 38 | 200 | | configure?.Invoke(options); |
| | 38 | 201 | | ArgumentNullException.ThrowIfNull(options.TimeZone, $"{nameof(ScheduledFlowOptions)}.{nameof(ScheduledFlowOption |
| | | 202 | | // The re-drive interval arms a Task.Delay, so it gets the timer ceiling. The startup window |
| | | 203 | | // needs none: it is subtracted from "now" (clamped at the epoch) and bounds a look-back whose |
| | | 204 | | // cost follows the occurrences the probe keeps, not the window's length — see |
| | | 205 | | // ScheduledFlowService.RecentOccurrences. Zero disables the probe. |
| | 38 | 206 | | AsyncResponseChannelOptions.EnsureTimerBacked(options.RedriveInterval, nameof(ScheduledFlowOptions), nameof(Sche |
| | 34 | 207 | | if (options.StartupRedriveWindow < TimeSpan.Zero) |
| | 2 | 208 | | throw new ArgumentException($"{nameof(ScheduledFlowOptions)}.{nameof(ScheduledFlowOptions.StartupRedriveWind |
| | | 209 | | |
| | | 210 | | // Validate the FINAL occurrence id against the WHOLE portable contract now — length, |
| | | 211 | | // bytes, characters, surrounding spaces — by running the id the scheduler will actually |
| | | 212 | | // mint through the same check the store's create uses. Duplicating one of those rules here |
| | | 213 | | // let a name containing '/' (or enough multi-byte characters) register cleanly and then |
| | | 214 | | // fail on every occurrence at 3 a.m., logged and dropped. |
| | 32 | 215 | | var occurrenceId = ScheduledFlowService.OccurrenceFlowId(name, DateTimeOffset.UnixEpoch); |
| | 32 | 216 | | if (FlowStateConcurrency.FlowIdNotPortable(occurrenceId) is { } rejection) |
| | | 217 | | { |
| | 8 | 218 | | throw new ArgumentException( |
| | 8 | 219 | | $"The scheduled flow name '{name}' produces occurrence ids (sched:{{name}}:{{timestamp}}) that are not p |
| | 8 | 220 | | nameof(name)); |
| | | 221 | | } |
| | | 222 | | |
| | | 223 | | // Parse in the schedule's own time zone AND probe for a real next occurrence: a well-formed |
| | | 224 | | // but unsatisfiable expression ("0 0 30 2 *") would otherwise register cleanly and its loop |
| | | 225 | | // would die at startup with one warning — the silent 3 a.m. failure this validation exists |
| | | 226 | | // to prevent. The runtime null-check in ScheduledFlowService stays as the backstop. |
| | | 227 | | // Deliberate deviation from the engine's TimeProvider seam: registration runs before any |
| | | 228 | | // provider (and thus any injected clock) exists, and the probe's 400-year scan (a full |
| | | 229 | | // Gregorian cycle — a completeness proof) means the system clock only answers "can this |
| | | 230 | | // expression ever fire", not "when" — the runtime loop computes actual occurrences from |
| | | 231 | | // the injected TimeProvider as usual. |
| | 24 | 232 | | var probe = CronSchedule.Parse(cron, options.TimeZone); |
| | 22 | 233 | | if (probe.GetNextOccurrence(TimeProvider.System.GetUtcNow()) is null) |
| | | 234 | | { |
| | 2 | 235 | | throw new ArgumentException( |
| | 2 | 236 | | $"The cron expression '{cron}' for scheduled flow '{name}' has no future occurrence (an unsatisfiable da |
| | 2 | 237 | | nameof(cron)); |
| | | 238 | | } |
| | | 239 | | |
| | 20 | 240 | | builder.WithDurableFlow<TFlow, TInput>(); |
| | 20 | 241 | | builder.Services.AddSingleton(new ScheduledFlowRegistration |
| | 20 | 242 | | { |
| | 20 | 243 | | Name = name, |
| | 20 | 244 | | CronExpression = cron, |
| | 20 | 245 | | Options = options, |
| | 20 | 246 | | StartOccurrenceAsync = (flows, flowId, occurrence, cancellationToken) => |
| | 18 | 247 | | flows.StartAsync<TFlow, TInput>(input(occurrence), flowId, cancellationToken) |
| | 20 | 248 | | }); |
| | 20 | 249 | | builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<Microsoft.Extensions.Hosting.IHostedService, Sched |
| | 20 | 250 | | return builder; |
| | | 251 | | } |
| | | 252 | | |
| | | 253 | | /// <summary> |
| | | 254 | | /// Uses an atomic process-local flow-state store. Intended for development, tests, and |
| | | 255 | | /// single-process apps; choose a DurableFlows provider package for multi-replica execution. |
| | | 256 | | /// </summary> |
| | | 257 | | public static AsyncResponseRegistrationBuilder WithInMemoryDurableFlows( |
| | | 258 | | this AsyncResponseRegistrationBuilder builder, |
| | | 259 | | Action<DurableFlowOptions>? configure = null) |
| | | 260 | | { |
| | 832 | 261 | | builder.Services.TryAddSingleton<InMemoryFlowStateStore>(); |
| | 832 | 262 | | return builder.WithDurableFlows<InMemoryFlowStateStore>(configure); |
| | | 263 | | } |
| | | 264 | | |
| | | 265 | | // The executor's public methods are lost-subscriber callback targets persisted by name |
| | | 266 | | // (RecoverAsync / FailAsync / ExecuteAsync); root them explicitly so the reflective dispatch |
| | | 267 | | // finds them in trimmed apps without any user action. |
| | | 268 | | [DynamicDependency(DynamicallyAccessedMemberTypes.PublicMethods, typeof(IDurableFlowExecutor))] |
| | | 269 | | [DynamicDependency(DynamicallyAccessedMemberTypes.PublicMethods, typeof(DurableFlowExecutor))] |
| | | 270 | | private static void AddDurableFlowEngine(IServiceCollection services) |
| | | 271 | | { |
| | | 272 | | // Observers are resolved ONCE by the executor factory below, from the root provider, and |
| | | 273 | | // held for the singleton executor's lifetime. A scoped/transient registration would |
| | | 274 | | // surface as an opaque "Cannot resolve scoped service ... from root provider" thrown per |
| | | 275 | | // flow job inside the transport's retry loop (ValidateOnBuild does not catch it). The |
| | | 276 | | // audit — evaluated by AsyncResponseStartupValidator at host start, then released — fails |
| | | 277 | | // with an error naming the offending registration and the fix instead, without the |
| | | 278 | | // executor factory rooting the service collection for the app's lifetime. |
| | 2692 | 279 | | services.TryAddSingleton(new DurableFlowObserverLifetimeAudit(services)); |
| | 4259 | 280 | | services.TryAddSingleton<IDurableFlowExecutor>(provider => new DurableFlowExecutor( |
| | 4259 | 281 | | provider.GetRequiredService<IServiceScopeFactory>(), |
| | 4259 | 282 | | provider.GetRequiredService<IAsyncResponseBuilder>(), |
| | 4259 | 283 | | provider.GetRequiredService<IAsyncResponseSubscriber>(), |
| | 4259 | 284 | | provider.GetService<IRecoverableAsyncResponseSubscriber>(), |
| | 4259 | 285 | | provider.GetRequiredService<AsyncResponseContextPropagation>(), |
| | 4259 | 286 | | provider.GetRequiredService<DurableFlowOptions>(), |
| | 4259 | 287 | | provider.GetRequiredService<Microsoft.Extensions.Logging.ILogger<DurableFlowExecutor>>(), |
| | 4259 | 288 | | provider.GetServices<DurableFlowRegistration>(), |
| | 4259 | 289 | | provider.GetService<Microsoft.Extensions.Hosting.IHostApplicationLifetime>(), |
| | 4259 | 290 | | provider.GetService<TimeProvider>(), |
| | 4259 | 291 | | provider.GetServices<IDurableFlowExecutionObserver>(), |
| | 4259 | 292 | | provider.GetService<IWorkerTransport>(), |
| | 4259 | 293 | | // The registered channel's declared default waiter timeout (the startup validator |
| | 4259 | 294 | | // enforces exactly one channel); null when the channel does not declare one. |
| | 4259 | 295 | | provider.GetServices<AsyncResponseChannelMarker>() |
| | 1567 | 296 | | .Select(marker => marker.EffectiveDefaultWaitTimeout) |
| | 5826 | 297 | | .LastOrDefault(timeout => timeout is not null))); |
| | 4281 | 298 | | services.TryAddSingleton<IDurableFlows>(provider => new DurableFlowService( |
| | 4281 | 299 | | provider.GetRequiredService<IServiceScopeFactory>(), |
| | 4281 | 300 | | provider.GetRequiredService<IAsyncResponseBuilder>(), |
| | 4281 | 301 | | provider.GetRequiredService<AsyncResponseContextPropagation>(), |
| | 4281 | 302 | | provider.GetRequiredService<DurableFlowOptions>(), |
| | 4281 | 303 | | provider.GetRequiredService<Microsoft.Extensions.Logging.ILogger<DurableFlowService>>(), |
| | 4281 | 304 | | provider.GetService<TimeProvider>())); |
| | 2692 | 305 | | } |
| | | 306 | | |
| | | 307 | | /// <summary> |
| | | 308 | | /// Registers an application <see cref="IAsyncResponseContextPropagator"/> that carries ambient |
| | | 309 | | /// context (trace id, principal, tenant, …) across the serialization boundary into worker jobs |
| | | 310 | | /// and lost-subscriber recovery callbacks. In-process hops flow ambient state automatically via |
| | | 311 | | /// the captured <see cref="System.Threading.ExecutionContext"/>; propagators are only needed for |
| | | 312 | | /// context that must survive serialization (broker-backed workers, recovery after a redeploy). |
| | | 313 | | /// Register one per concern (e.g. a trace propagator and a principal propagator). |
| | | 314 | | /// </summary> |
| | | 315 | | public static AsyncResponseRegistrationBuilder WithContextPropagator<[DynamicallyAccessedMembers(DynamicallyAccessed |
| | | 316 | | where TPropagator : class, IAsyncResponseContextPropagator |
| | | 317 | | { |
| | 201 | 318 | | builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IAsyncResponseContextPropagator, TPropagator>()); |
| | 201 | 319 | | return builder; |
| | | 320 | | } |
| | | 321 | | |
| | | 322 | | /// <summary> |
| | | 323 | | /// Registers the process-local response channel and recovery store. Waiters, subscriptions, |
| | | 324 | | /// and recovery state all live in memory and disappear when the process exits — the simplest |
| | | 325 | | /// setup, with no durable recovery. Pair with <see cref="WithInMemoryTransport"/> for a fully |
| | | 326 | | /// in-memory setup including background worker jobs. |
| | | 327 | | /// </summary> |
| | | 328 | | public static AsyncResponseRegistrationBuilder WithInMemoryChannel( |
| | | 329 | | this AsyncResponseRegistrationBuilder builder, |
| | | 330 | | Action<InMemoryAsyncResponseOptions>? configure = null) |
| | | 331 | | { |
| | 1342 | 332 | | var services = builder.Services; |
| | 1342 | 333 | | services.AddOptions(); |
| | 1342 | 334 | | if (configure is not null) |
| | | 335 | | { |
| | 919 | 336 | | services.Configure(configure); |
| | | 337 | | } |
| | | 338 | | |
| | 1342 | 339 | | services.TryAddSingleton<InMemoryRecoveryStateStore>(); |
| | 2574 | 340 | | services.TryAddSingleton<IRecoveryStateStore>(provider => provider.GetRequiredService<InMemoryRecoveryStateStore |
| | 2134 | 341 | | services.TryAddSingleton<IRecoveryStateScanner>(provider => provider.GetRequiredService<InMemoryRecoveryStateSto |
| | | 342 | | |
| | 1342 | 343 | | services.TryAddSingleton<InMemoryAsyncResponseChannel>(); |
| | 2171 | 344 | | services.TryAddSingleton<IAsyncResponsePublisher>(provider => provider.GetRequiredService<InMemoryAsyncResponseC |
| | 1908 | 345 | | services.TryAddSingleton<IRawAsyncResponsePublisher>(provider => provider.GetRequiredService<InMemoryAsyncRespon |
| | 2086 | 346 | | services.TryAddSingleton<IAsyncResponseSubscriber>(provider => provider.GetRequiredService<InMemoryAsyncResponse |
| | 2241 | 347 | | services.TryAddSingleton<IRecoverableAsyncResponseSubscriber>(provider => provider.GetRequiredService<InMemoryAs |
| | 2182 | 348 | | services.TryAddSingleton<IActiveSubscriberProbe>(provider => provider.GetRequiredService<InMemoryAsyncResponseCh |
| | | 349 | | |
| | | 350 | | // Full recovery capability, exactly like the durable channel packages: the recoverable |
| | | 351 | | // fluent builder is available, durable flows register their lost-subscriber callbacks, and |
| | | 352 | | // the lost-subscriber dispatcher routes late responses through them. The store is |
| | | 353 | | // process-local, so this recovery spans waiter loss within one process lifetime (and the |
| | | 354 | | // simulated restarts of AsyncResponse.Testing) — not a real process exit. |
| | 2229 | 355 | | services.Replace(ServiceDescriptor.Singleton<IRecoverableAsyncResponseBuilder>(provider => new RecoverableAsyncR |
| | 2229 | 356 | | provider.GetRequiredService<IRecoverableAsyncResponseSubscriber>(), |
| | 2229 | 357 | | provider.GetService<IWorkerTransport>(), |
| | 2229 | 358 | | provider.GetService<IAsyncResponseReplyTargetProvider>(), |
| | 2229 | 359 | | provider.GetRequiredService<AsyncResponseContextPropagation>(), |
| | 2229 | 360 | | provider.GetService<TimeProvider>(), |
| | 2229 | 361 | | // The producer-side mirror of the ingress's inbound size budget (WorkerJobTooLargeException). |
| | 2229 | 362 | | provider.GetService<IOptions<AsyncResponseOptions>>()))); |
| | 2215 | 363 | | services.Replace(ServiceDescriptor.Singleton<IAsyncResponseBuilder>(provider => provider.GetRequiredService<IRec |
| | | 364 | | |
| | | 365 | | // The resolved default waiter timeout is declared through the marker so the startup |
| | | 366 | | // validator can require the durable-flow ledger TTL to out-live a timeout-less awaited |
| | | 367 | | // step, and the flow engine can extend a parked ledger by it — without either referencing |
| | | 368 | | // channel option types. |
| | 1342 | 369 | | services.AddSingleton(provider => |
| | 1342 | 370 | | { |
| | 908 | 371 | | var options = provider.GetRequiredService<Microsoft.Extensions.Options.IOptions<InMemoryAsyncResponseOptions |
| | 908 | 372 | | return new AsyncResponseChannelMarker("InMemory") |
| | 908 | 373 | | { |
| | 908 | 374 | | EffectiveDefaultWaitTimeout = options.DefaultTimeout ?? options.RecoveryStateExpiry |
| | 908 | 375 | | }; |
| | 1342 | 376 | | }); |
| | 1342 | 377 | | return builder; |
| | | 378 | | } |
| | | 379 | | |
| | | 380 | | /// <summary> |
| | | 381 | | /// Registers the in-memory (in-process) worker transport and its background consumer. Jobs run |
| | | 382 | | /// in the current process and survive only as long as it does — suitable for development, tests, |
| | | 383 | | /// and single-node deployments. Chain exactly one transport after <see cref="AddAsyncResponse"/>; |
| | | 384 | | /// for distributed, durable execution use a full broker-backed transport package such as |
| | | 385 | | /// <c>WithGooglePubSubTransport</c> or <c>WithRabbitMqTransport</c>. |
| | | 386 | | /// </summary> |
| | | 387 | | public static AsyncResponseRegistrationBuilder WithInMemoryTransport( |
| | | 388 | | this AsyncResponseRegistrationBuilder builder, |
| | | 389 | | Action<InMemoryWorkerTransportOptions>? configure = null) |
| | | 390 | | { |
| | 652 | 391 | | var services = builder.Services; |
| | 652 | 392 | | services.AddOptions(); |
| | 652 | 393 | | if (configure is not null) |
| | 422 | 394 | | services.Configure(configure); |
| | 652 | 395 | | services.TryAddSingleton<WorkerJobExecutor>(); |
| | 652 | 396 | | services.TryAddSingleton<InMemoryWorkerTransport>(); |
| | 1218 | 397 | | services.TryAddSingleton<IWorkerTransport>(provider => provider.GetRequiredService<InMemoryWorkerTransport>()); |
| | 652 | 398 | | services.AddHostedService<InMemoryWorkerHost>(); |
| | 652 | 399 | | services.AddSingleton(new AsyncResponseTransportMarker("InMemory")); |
| | 652 | 400 | | return builder; |
| | | 401 | | } |
| | | 402 | | } |