| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | using System.Reflection; |
| | | 3 | | using Elsa.Common; |
| | | 4 | | using Elsa.Common.DistributedHosting; |
| | | 5 | | using Elsa.Common.Features; |
| | | 6 | | using Elsa.Common.RecurringTasks; |
| | | 7 | | using Elsa.Extensions; |
| | | 8 | | using Elsa.Features.Abstractions; |
| | | 9 | | using Elsa.Features.Attributes; |
| | | 10 | | using Elsa.Features.Services; |
| | | 11 | | using Elsa.Mediator.Contracts; |
| | | 12 | | using Elsa.Workflows.Features; |
| | | 13 | | using Elsa.Workflows.Management; |
| | | 14 | | using Elsa.Workflows.Management.Contracts; |
| | | 15 | | using Elsa.Workflows.Management.Services; |
| | | 16 | | using Elsa.Workflows.Options; |
| | | 17 | | using Elsa.Workflows.Runtime.ActivationValidators; |
| | | 18 | | using Elsa.Workflows.Runtime.Bookmarks; |
| | | 19 | | using Elsa.Workflows.Runtime.Discovery; |
| | | 20 | | using Elsa.Workflows.Runtime.Entities; |
| | | 21 | | using Elsa.Workflows.Runtime.Handlers; |
| | | 22 | | using Elsa.Workflows.Runtime.Models; |
| | | 23 | | using Elsa.Workflows.Runtime.Options; |
| | | 24 | | using Elsa.Workflows.Runtime.Providers; |
| | | 25 | | using Elsa.Workflows.Runtime.Stimuli; |
| | | 26 | | using Elsa.Workflows.Runtime.Stores; |
| | | 27 | | using Elsa.Workflows.Runtime.Tasks; |
| | | 28 | | using Elsa.Workflows.Runtime.UIHints; |
| | | 29 | | using Medallion.Threading; |
| | | 30 | | using Medallion.Threading.FileSystem; |
| | | 31 | | using Microsoft.Extensions.DependencyInjection; |
| | | 32 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 33 | | using Microsoft.Extensions.Options; |
| | | 34 | | using Elsa.Common.Serialization; |
| | | 35 | | |
| | | 36 | | namespace Elsa.Workflows.Runtime.Features; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Installs and configures workflow runtime features. |
| | | 40 | | /// </summary> |
| | | 41 | | [DependsOn(typeof(SystemClockFeature))] |
| | | 42 | | [DependsOn(typeof(WorkflowsFeature))] |
| | 238 | 43 | | public class WorkflowRuntimeFeature(IModule module) : FeatureBase(module) |
| | | 44 | | { |
| | 406 | 45 | | private IDictionary<string, DispatcherChannel> WorkflowDispatcherChannels { get; set; } = new Dictionary<string, Dis |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// A list of workflow builders configured during application startup. |
| | | 49 | | /// </summary> |
| | 840 | 50 | | public IDictionary<string, Func<IServiceProvider, ValueTask<IWorkflow>>> Workflows { get; set; } = new Dictionary<st |
| | 654 | 51 | | private ISet<Type> WorkflowTypes { get; } = new HashSet<Type>(); |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// A factory that instantiates a concrete <see cref="IWorkflowRuntime"/>. |
| | | 55 | | /// </summary> |
| | 616 | 56 | | public Func<IServiceProvider, IWorkflowRuntime> WorkflowRuntime { get; set; } = sp => ActivatorUtilities.CreateInsta |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// A factory that instantiates an <see cref="IWorkflowDispatcher"/>. |
| | | 60 | | /// </summary> |
| | 458 | 61 | | public Func<IServiceProvider, IWorkflowDispatcher> WorkflowDispatcher { get; set; } = sp => |
| | 238 | 62 | | { |
| | 472 | 63 | | var decoratedService = ActivatorUtilities.CreateInstance<BackgroundWorkflowDispatcher>(sp); |
| | 472 | 64 | | var transactionalService = ActivatorUtilities.CreateInstance<TransactionalWorkflowDispatcher>(sp, decoratedServi |
| | 472 | 65 | | return ActivatorUtilities.CreateInstance<ValidatingWorkflowDispatcher>(sp, transactionalService); |
| | 238 | 66 | | }; |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// A factory that instantiates an <see cref="IStimulusDispatcher"/>. |
| | | 70 | | /// </summary> |
| | 469 | 71 | | public Func<IServiceProvider, IStimulusDispatcher> StimulusDispatcher { get; set; } = sp => ActivatorUtilities.Creat |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// A factory that instantiates an <see cref="IWorkflowCancellationDispatcher"/>. |
| | | 75 | | /// </summary> |
| | 461 | 76 | | public Func<IServiceProvider, IWorkflowCancellationDispatcher> WorkflowCancellationDispatcher { get; set; } = sp => |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// A factory that instantiates an <see cref="IWorkflowDispatchOutboxStore"/>. |
| | | 80 | | /// </summary> |
| | 458 | 81 | | public Func<IServiceProvider, IWorkflowDispatchOutboxStore> WorkflowDispatchOutboxStore { get; set; } = sp => Activa |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// A factory that instantiates an <see cref="IBookmarkStore"/>. |
| | | 85 | | /// </summary> |
| | 811 | 86 | | public Func<IServiceProvider, IBookmarkStore> BookmarkStore { get; set; } = sp => sp.GetRequiredService<MemoryBookma |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// A factory that instantiates an <see cref="IBookmarkQueueStore"/>. |
| | | 90 | | /// </summary> |
| | 811 | 91 | | public Func<IServiceProvider, IBookmarkQueueStore> BookmarkQueueStore { get; set; } = sp => sp.GetRequiredService<Me |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// A factory that instantiates an <see cref="IBookmarkQueueDeadLetterStore"/>. |
| | | 95 | | /// </summary> |
| | 632 | 96 | | public Func<IServiceProvider, IBookmarkQueueDeadLetterStore> BookmarkQueueDeadLetterStore { get; set; } = sp => sp.G |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// A factory that instantiates an <see cref="ITriggerStore"/>. |
| | | 100 | | /// </summary> |
| | 709 | 101 | | public Func<IServiceProvider, ITriggerStore> TriggerStore { get; set; } = sp => sp.GetRequiredService<MemoryTriggerS |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// A factory that instantiates an <see cref="IWorkflowExecutionLogStore"/>. |
| | | 105 | | /// </summary> |
| | 711 | 106 | | public Func<IServiceProvider, IWorkflowExecutionLogStore> WorkflowExecutionLogStore { get; set; } = sp => sp.GetRequ |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// A factory that instantiates an <see cref="IActivityExecutionStore"/>. |
| | | 110 | | /// </summary> |
| | 698 | 111 | | public Func<IServiceProvider, IActivityExecutionStore> ActivityExecutionLogStore { get; set; } = sp => sp.GetRequire |
| | | 112 | | |
| | | 113 | | /// <summary> |
| | | 114 | | /// A factory that instantiates an <see cref="IDistributedLockProvider"/>. |
| | | 115 | | /// </summary> |
| | 633 | 116 | | public Func<IServiceProvider, IDistributedLockProvider> DistributedLockProvider { get; set; } = _ => new FileDistrib |
| | | 117 | | |
| | | 118 | | /// <summary> |
| | | 119 | | /// A factory that instantiates an <see cref="ITaskDispatcher"/>. |
| | | 120 | | /// </summary> |
| | 458 | 121 | | public Func<IServiceProvider, ITaskDispatcher> RunTaskDispatcher { get; set; } = sp => sp.GetRequiredService<Backgro |
| | | 122 | | |
| | | 123 | | /// <summary> |
| | | 124 | | /// A factory that instantiates an <see cref="IBackgroundActivityScheduler"/>. |
| | | 125 | | /// </summary> |
| | 633 | 126 | | public Func<IServiceProvider, IBackgroundActivityScheduler> BackgroundActivityScheduler { get; set; } = sp => Activa |
| | | 127 | | |
| | | 128 | | /// <summary> |
| | | 129 | | /// A factory that instantiates a log record sink for an <see cref="ActivityExecutionRecord"/>. |
| | | 130 | | /// </summary> |
| | 971 | 131 | | public Func<IServiceProvider, ILogRecordSink<ActivityExecutionRecord>> ActivityExecutionLogSink { get; set; } = sp = |
| | | 132 | | |
| | | 133 | | /// <summary> |
| | | 134 | | /// A factory that instantiates a log record sink for an <see cref="WorkflowExecutionLogRecord"/>. |
| | | 135 | | /// </summary> |
| | 971 | 136 | | public Func<IServiceProvider, ILogRecordSink<WorkflowExecutionLogRecord>> WorkflowExecutionLogSink { get; set; } = s |
| | | 137 | | |
| | | 138 | | /// <summary> |
| | | 139 | | /// A factory that instantiates an <see cref="ICommandHandler"/>. |
| | | 140 | | /// </summary> |
| | 238 | 141 | | public Func<IServiceProvider, ICommandHandler> DispatchWorkflowCommandHandler { get; set; } = sp => sp.GetRequiredSe |
| | | 142 | | |
| | | 143 | | /// <summary> |
| | | 144 | | /// A factory that instantiates an <see cref="IWorkflowResumer"/>. |
| | | 145 | | /// </summary> |
| | 872 | 146 | | public Func<IServiceProvider, IWorkflowResumer> WorkflowResumer { get; set; } = sp => sp.GetRequiredService<Workflow |
| | | 147 | | |
| | | 148 | | /// <summary> |
| | | 149 | | /// A factory that instantiates an <see cref="IBookmarkQueueWorker"/>. |
| | | 150 | | /// </summary> |
| | 751 | 151 | | public Func<IServiceProvider, IBookmarkQueueWorker> BookmarkQueueWorker { get; set; } = sp => sp.GetRequiredService< |
| | | 152 | | |
| | | 153 | | /// <summary> |
| | | 154 | | /// A delegate to configure the <see cref="DistributedLockingOptions"/>. |
| | | 155 | | /// </summary> |
| | 636 | 156 | | public Action<DistributedLockingOptions> DistributedLockingOptions { get; set; } = _ => { }; |
| | | 157 | | |
| | | 158 | | /// <summary> |
| | | 159 | | /// A delegate to configure the <see cref="WorkflowInboxCleanupOptions"/>. |
| | | 160 | | /// </summary> |
| | | 161 | | [Obsolete("Will be removed in a future version")] |
| | 458 | 162 | | public Action<WorkflowInboxCleanupOptions> WorkflowInboxCleanupOptions { get; set; } = _ => { }; |
| | | 163 | | |
| | | 164 | | /// <summary> |
| | | 165 | | /// A delegate to configure the <see cref="WorkflowDispatcherOptions"/>. |
| | | 166 | | /// </summary> |
| | 626 | 167 | | public Action<WorkflowDispatcherOptions> WorkflowDispatcherOptions { get; set; } = _ => { }; |
| | | 168 | | |
| | | 169 | | /// <summary> |
| | | 170 | | /// A delegate to configure the <see cref="BookmarkQueuePurgeOptions"/>. |
| | | 171 | | /// </summary> |
| | 463 | 172 | | public Action<BookmarkQueuePurgeOptions> BookmarkQueuePurgeOptions { get; set; } = _ => { }; |
| | | 173 | | |
| | | 174 | | /// <summary> |
| | | 175 | | /// Enables the workflow inbox cleanup job. |
| | | 176 | | /// </summary> |
| | | 177 | | public WorkflowRuntimeFeature EnableWorkflowInboxCleanupJob() |
| | | 178 | | { |
| | 0 | 179 | | Services.Configure<WorkflowInboxCleanupOptions>(options => { options.IsEnabled = true; }); |
| | 0 | 180 | | return this; |
| | | 181 | | } |
| | | 182 | | |
| | | 183 | | /// <summary> |
| | | 184 | | /// Disables the workflow inbox cleanup job. |
| | | 185 | | /// </summary> |
| | | 186 | | public WorkflowRuntimeFeature DisableWorkflowInboxCleanupJob() |
| | | 187 | | { |
| | 0 | 188 | | Services.Configure<WorkflowInboxCleanupOptions>(options => { options.IsEnabled = false; }); |
| | 0 | 189 | | return this; |
| | | 190 | | } |
| | | 191 | | |
| | | 192 | | /// <summary> |
| | | 193 | | /// Register the specified workflow type. |
| | | 194 | | /// </summary> |
| | | 195 | | public WorkflowRuntimeFeature AddWorkflow<T>() where T : IWorkflow |
| | | 196 | | { |
| | 22 | 197 | | AddWorkflow(typeof(T)); |
| | 22 | 198 | | return this; |
| | | 199 | | } |
| | | 200 | | |
| | | 201 | | /// <summary> |
| | | 202 | | /// Register the specified workflow type. |
| | | 203 | | /// </summary> |
| | | 204 | | public WorkflowRuntimeFeature AddWorkflow(Type workflowType) |
| | | 205 | | { |
| | 220 | 206 | | Workflows.Add(workflowType); |
| | 216 | 207 | | WorkflowTypes.Add(workflowType); |
| | 216 | 208 | | return this; |
| | | 209 | | } |
| | | 210 | | |
| | | 211 | | /// <summary> |
| | | 212 | | /// Register all workflows in the specified assembly. |
| | | 213 | | /// </summary> |
| | | 214 | | [RequiresUnreferencedCode("The assembly is required to be referenced.")] |
| | | 215 | | public WorkflowRuntimeFeature AddWorkflowsFrom(Assembly assembly) |
| | | 216 | | { |
| | 396 | 217 | | foreach (var workflowType in WorkflowTypeScanner.GetWorkflowTypes(assembly)) |
| | 192 | 218 | | AddWorkflow(workflowType); |
| | | 219 | | |
| | 6 | 220 | | return this; |
| | | 221 | | } |
| | | 222 | | |
| | | 223 | | /// <summary> |
| | | 224 | | /// Adds a dispatcher channel. |
| | | 225 | | /// </summary> |
| | | 226 | | public WorkflowRuntimeFeature AddDispatcherChannel(string channel) |
| | | 227 | | { |
| | 0 | 228 | | return AddDispatcherChannel(new DispatcherChannel |
| | 0 | 229 | | { |
| | 0 | 230 | | Name = channel |
| | 0 | 231 | | }); |
| | | 232 | | } |
| | | 233 | | |
| | | 234 | | /// <summary> |
| | | 235 | | /// Adds a dispatcher channel. |
| | | 236 | | /// </summary> |
| | | 237 | | public WorkflowRuntimeFeature AddDispatcherChannel(DispatcherChannel channel) |
| | | 238 | | { |
| | 0 | 239 | | WorkflowDispatcherChannels[channel.Name] = channel; |
| | 0 | 240 | | return this; |
| | | 241 | | } |
| | | 242 | | |
| | | 243 | | /// <inheritdoc /> |
| | | 244 | | public override void Configure() |
| | | 245 | | { |
| | 220 | 246 | | Module.AddActivitiesFrom<WorkflowRuntimeFeature>(); |
| | 220 | 247 | | Module.Configure<WorkflowsFeature>(workflows => |
| | 220 | 248 | | { |
| | 220 | 249 | | // ExecutionCycleAwareCommitStateHandler decorates DefaultCommitStateHandler — disposes the execution cycle |
| | 220 | 250 | | // commit so the drain orchestrator's await-disposed sequencing can land its Interrupted write last. |
| | 733 | 251 | | workflows.CommitStateHandler = sp => sp.GetRequiredService<Elsa.Workflows.Runtime.Services.ExecutionCycleAwa |
| | 440 | 252 | | }); |
| | | 253 | | |
| | 220 | 254 | | Services.Configure<RecurringTaskOptions>(options => |
| | 220 | 255 | | { |
| | 70 | 256 | | options.Schedule.ConfigureTask<TriggerBookmarkQueueRecurringTask>(TimeSpan.FromSeconds(10)); |
| | 290 | 257 | | }); |
| | 220 | 258 | | } |
| | | 259 | | |
| | | 260 | | /// <inheritdoc /> |
| | | 261 | | /// <summary> |
| | | 262 | | /// Callback that tunes the graceful-shutdown machinery (drain deadline, per-source pause timeout, stimulus-queue ba |
| | | 263 | | /// pause-persistence policy). Applied when <see cref="Apply"/> binds <see cref="GracefulShutdownOptions"/>. |
| | | 264 | | /// </summary> |
| | 225 | 265 | | public Action<GracefulShutdownOptions>? GracefulShutdown { get; set; } |
| | | 266 | | |
| | | 267 | | public override void Apply() |
| | | 268 | | { |
| | | 269 | | // Options. |
| | 220 | 270 | | Services.Configure(DistributedLockingOptions); |
| | 220 | 271 | | Services.Configure(WorkflowInboxCleanupOptions); |
| | 220 | 272 | | Services.Configure(WorkflowDispatcherOptions); |
| | 220 | 273 | | Services.Configure(BookmarkQueuePurgeOptions); |
| | 220 | 274 | | Services.Configure<SerializationTypeOptions>(RegisterWorkflowTypeAliases); |
| | 558 | 275 | | Services.Configure<RuntimeOptions>(options => { options.Workflows = Workflows; }); |
| | 220 | 276 | | Services.Configure<WorkflowDispatcherOptions>(options => |
| | 220 | 277 | | { |
| | 168 | 278 | | options.Channels.AddRange(WorkflowDispatcherChannels.Values); |
| | 388 | 279 | | }); |
| | 220 | 280 | | Services.AddGracefulShutdownOptions(GracefulShutdown); |
| | | 281 | | |
| | | 282 | | // Graceful-shutdown core (US1 — quiescence machinery). |
| | 220 | 283 | | Services |
| | 80 | 284 | | .AddSingleton<IQuiescenceSignal>(sp => new Elsa.Workflows.Runtime.Services.QuiescenceSignal( |
| | 80 | 285 | | sp.GetRequiredService<IOptions<GracefulShutdownOptions>>(), |
| | 80 | 286 | | sp.GetRequiredService<ISystemClock>(), |
| | 80 | 287 | | sp.GetRequiredService<IExecutionCycleRegistry>(), |
| | 80 | 288 | | sp.GetRequiredService<IServiceScopeFactory>())) |
| | 220 | 289 | | .AddSingleton<IIngressSourceRegistry, Elsa.Workflows.Runtime.Services.IngressSourceRegistry>() |
| | 220 | 290 | | .AddSingleton<IExecutionCycleRegistry, Elsa.Workflows.Runtime.Services.ExecutionCycleRegistry>() |
| | 220 | 291 | | // Lazy collection breaks the otherwise-circular DI chain QuiescenceSignal → IExecutionCycleRegistry → |
| | 220 | 292 | | // IIngressSourceRegistry → IEnumerable<IIngressSource> → IQuiescenceSignal. Adapters take a direct |
| | 220 | 293 | | // IQuiescenceSignal dependency; the registry materializes the collection on first read. |
| | 158 | 294 | | .AddSingleton(sp => new Lazy<IEnumerable<IIngressSource>>(sp.GetServices<IIngressSource>)) |
| | 220 | 295 | | // Drain orchestrator + hosted service (US1). See FR-029 / R5 — heartbeat must outlive drain. |
| | 220 | 296 | | .AddSingleton<IDrainOrchestrator, Elsa.Workflows.Runtime.Services.DrainOrchestrator>() |
| | 220 | 297 | | .AddHostedService<Elsa.Workflows.Runtime.HostedServices.DrainOrchestratorHostedService>() |
| | 220 | 298 | | // Domain service that backs all runtime-admin transports (US2). Encapsulates the audit-on-effective- |
| | 220 | 299 | | // transition rule (SC-007) so transports stay thin. Scoped because INotificationSender is scoped. |
| | 220 | 300 | | .AddScoped<IWorkflowRuntimeAdminService, Elsa.Workflows.Runtime.Services.WorkflowRuntimeAdminService>() |
| | 220 | 301 | | // Interrupted-workflow recovery on shell activation (US3). Disjoint from the timeout-based |
| | 220 | 302 | | // RestartInterruptedWorkflowsTask: filter is SubStatus = Interrupted; that task's filter is IsExecuting=tru |
| | 220 | 303 | | .AddScoped<IInterruptedRecoveryScanner, Elsa.Workflows.Runtime.Services.InterruptedRecoveryScanner>() |
| | 220 | 304 | | .AddStartupTask<Elsa.Workflows.Runtime.StartupTasks.RecoverInterruptedWorkflowsStartupTask>() |
| | 220 | 305 | | // Internal bookmark-queue processor surfaced as an ingress source for diagnostic visibility (FR-006). |
| | 220 | 306 | | // Pause behavior is enforced inside BookmarkQueueProcessor via IQuiescenceSignal (FR-024). |
| | 220 | 307 | | .AddSingleton<IIngressSource, Elsa.Workflows.Runtime.IngressSources.InternalBookmarkQueueIngressSource>() |
| | 220 | 308 | | // Re-applies persisted pause state on activation when PausePersistence = AcrossReactivations (FR-028). |
| | 220 | 309 | | .AddStartupTask<Elsa.Workflows.Runtime.StartupTasks.InitializePauseStateStartupTask>(); |
| | | 310 | | |
| | 220 | 311 | | Services |
| | 220 | 312 | | // Core. |
| | 220 | 313 | | .AddScoped<ITriggerIndexer, TriggerIndexer>() |
| | 220 | 314 | | .AddScoped<IWorkflowInstanceFactory, WorkflowInstanceFactory>() |
| | 220 | 315 | | .AddScoped<IWorkflowHostFactory, WorkflowHostFactory>() |
| | 220 | 316 | | .AddScoped<IBackgroundActivityInvoker, BackgroundActivityInvoker>() |
| | 220 | 317 | | .AddScoped(WorkflowRuntime) |
| | 220 | 318 | | .AddScoped(WorkflowDispatcher) |
| | 220 | 319 | | .AddScoped(StimulusDispatcher) |
| | 220 | 320 | | .AddScoped(WorkflowCancellationDispatcher) |
| | 220 | 321 | | .AddScoped(RunTaskDispatcher) |
| | 220 | 322 | | .AddScoped(ActivityExecutionLogSink) |
| | 220 | 323 | | .AddScoped(WorkflowExecutionLogSink) |
| | 220 | 324 | | .AddSingleton(BackgroundActivityScheduler) |
| | 220 | 325 | | .AddSingleton<RandomLongIdentityGenerator>() |
| | 220 | 326 | | .AddSingleton<IBookmarkQueueSignaler, BookmarkQueueSignaler>() |
| | 220 | 327 | | .AddScoped(BookmarkQueueWorker) |
| | 220 | 328 | | .AddScoped<IBookmarkManager, DefaultBookmarkManager>() |
| | 220 | 329 | | .AddScoped<IActivityExecutionManager, DefaultActivityExecutionManager>() |
| | 220 | 330 | | .AddScoped<IActivityExecutionStatsService, ActivityExecutionStatsService>() |
| | 220 | 331 | | .AddScoped<IActivityExecutionMapper, DefaultActivityExecutionMapper>() |
| | 220 | 332 | | .AddScoped<IWorkflowDefinitionStorePopulator, DefaultWorkflowDefinitionStorePopulator>() |
| | 220 | 333 | | .AddScoped<IRegistriesPopulator, DefaultRegistriesPopulator>() |
| | 220 | 334 | | .AddScoped<IWorkflowDefinitionsRefresher, WorkflowDefinitionsRefresher>() |
| | 220 | 335 | | .AddScoped<IWorkflowDefinitionsReloader, WorkflowDefinitionsReloader>() |
| | 220 | 336 | | .AddScoped<IWorkflowRegistry, DefaultWorkflowRegistry>() |
| | 220 | 337 | | .AddScoped<IWorkflowMatcher, WorkflowMatcher>() |
| | 220 | 338 | | .AddScoped<IWorkflowInvoker, WorkflowInvoker>() |
| | 220 | 339 | | .AddScoped<IStimulusSender, StimulusSender>() |
| | 220 | 340 | | .AddScoped<ITriggerBoundWorkflowService, TriggerBoundWorkflowService>() |
| | 220 | 341 | | .AddScoped<IBookmarkBoundWorkflowService, BookmarkBoundWorkflowService>() |
| | 220 | 342 | | .AddScoped<ITaskReporter, TaskReporter>() |
| | 220 | 343 | | .AddScoped<SynchronousTaskDispatcher>() |
| | 220 | 344 | | .AddScoped<BackgroundTaskDispatcher>() |
| | 220 | 345 | | .AddScoped<StoreActivityExecutionLogSink>() |
| | 220 | 346 | | .AddScoped<StoreWorkflowExecutionLogSink>() |
| | 220 | 347 | | .AddScoped<DispatchWorkflowCommandHandler>() |
| | 220 | 348 | | .AddScoped<IEventPublisher, EventPublisher>() |
| | 220 | 349 | | .AddScoped<IBookmarkUpdater, BookmarkUpdater>() |
| | 220 | 350 | | .AddScoped<IBookmarksPersister, BookmarksPersister>() |
| | 220 | 351 | | .AddScoped<IBookmarkResumer, BookmarkResumer>() |
| | 220 | 352 | | .AddScoped<IBookmarkQueue, StoreBookmarkQueue>() |
| | 220 | 353 | | .AddScoped<IBookmarkQueueDeadLetterManager, BookmarkQueueDeadLetterManager>() |
| | 220 | 354 | | .AddScoped(WorkflowResumer) |
| | 220 | 355 | | .AddScoped<WorkflowResumer>() |
| | 220 | 356 | | .AddScoped(BookmarkQueueWorker) |
| | 220 | 357 | | .AddScoped<BookmarkQueueWorker>() |
| | 220 | 358 | | .AddScoped<ITriggerInvoker, TriggerInvoker>() |
| | 220 | 359 | | .AddScoped<IWorkflowCanceler, WorkflowCanceler>() |
| | 220 | 360 | | .AddScoped<IWorkflowCancellationService, WorkflowCancellationService>() |
| | 220 | 361 | | .AddScoped<IWorkflowActivationStrategyEvaluator, DefaultWorkflowActivationStrategyEvaluator>() |
| | 220 | 362 | | .AddScoped<IWorkflowStarter, DefaultWorkflowStarter>() |
| | 220 | 363 | | .AddScoped<IWorkflowRestarter, DefaultWorkflowRestarter>() |
| | 220 | 364 | | .AddScoped<IBookmarkQueuePurger, DefaultBookmarkQueuePurger>() |
| | 220 | 365 | | .AddSingleton<IWorkflowDispatchOutboxAccessor, WorkflowDispatchOutboxAccessor>() |
| | 220 | 366 | | .AddScoped<ILogRecordExtractor<WorkflowExecutionLogRecord>, WorkflowExecutionLogRecordExtractor>() |
| | 220 | 367 | | .AddScoped<IActivityPropertyLogPersistenceEvaluator, ActivityPropertyLogPersistenceEvaluator>() |
| | 220 | 368 | | .AddScoped<IBookmarkQueueProcessor, BookmarkQueueProcessor>() |
| | 220 | 369 | | .AddScoped<DefaultCommitStateHandler>() |
| | 220 | 370 | | .AddScoped<Elsa.Workflows.Runtime.Services.ExecutionCycleAwareCommitStateHandler>() |
| | 220 | 371 | | .AddScoped<WorkflowHeartbeatGeneratorFactory>() |
| | 220 | 372 | | |
| | 220 | 373 | | // Deprecated services. |
| | 220 | 374 | | .AddScoped<IWorkflowInbox, StimulusProxyWorkflowInbox>() |
| | 220 | 375 | | |
| | 220 | 376 | | // Stores. |
| | 220 | 377 | | .AddScoped(BookmarkStore) |
| | 220 | 378 | | .AddScoped(BookmarkQueueStore) |
| | 220 | 379 | | .AddScoped(BookmarkQueueDeadLetterStore) |
| | 220 | 380 | | .AddScoped(TriggerStore) |
| | 220 | 381 | | .AddScoped(WorkflowExecutionLogStore) |
| | 220 | 382 | | .AddScoped(ActivityExecutionLogStore) |
| | 220 | 383 | | |
| | 220 | 384 | | // Lazy services. |
| | 174 | 385 | | .AddScoped<Func<IEnumerable<IWorkflowsProvider>>>(sp => sp.GetServices<IWorkflowsProvider>) |
| | 669 | 386 | | .AddScoped<Func<IEnumerable<IWorkflowMaterializer>>>(sp => sp.GetServices<IWorkflowMaterializer>) |
| | 220 | 387 | | |
| | 220 | 388 | | // Noop stores. |
| | 220 | 389 | | .AddScoped<MemoryWorkflowExecutionLogStore>() |
| | 220 | 390 | | .AddScoped<MemoryActivityExecutionStore>() |
| | 220 | 391 | | |
| | 220 | 392 | | // Memory stores. |
| | 220 | 393 | | .AddMemoryStore<StoredBookmark, MemoryBookmarkStore>() |
| | 220 | 394 | | .AddMemoryStore<StoredTrigger, MemoryTriggerStore>() |
| | 220 | 395 | | .AddMemoryStore<BookmarkQueueItem, MemoryBookmarkQueueStore>() |
| | 220 | 396 | | .AddMemoryStore<BookmarkQueueDeadLetterItem, MemoryBookmarkQueueDeadLetterStore>() |
| | 220 | 397 | | .AddMemoryStore<WorkflowExecutionLogRecord, MemoryWorkflowExecutionLogStore>() |
| | 220 | 398 | | .AddMemoryStore<ActivityExecutionRecord, MemoryActivityExecutionStore>() |
| | 220 | 399 | | |
| | 220 | 400 | | // Startup tasks, background tasks, and recurring tasks. |
| | 220 | 401 | | .AddStartupTask<PopulateRegistriesStartupTask>() |
| | 220 | 402 | | .AddRecurringTask<TriggerBookmarkQueueRecurringTask>(TimeSpan.FromMinutes(1)) |
| | 220 | 403 | | .AddRecurringTask<PurgeBookmarkQueueRecurringTask>(TimeSpan.FromSeconds(10)) |
| | 220 | 404 | | .AddRecurringTask<RestartInterruptedWorkflowsTask>(TimeSpan.FromMinutes(5)) // Same default as the workflow |
| | 220 | 405 | | .AddRecurringTask<ProcessWorkflowDispatchOutboxRecurringTask>(TimeSpan.FromSeconds(10)) |
| | 220 | 406 | | |
| | 220 | 407 | | // Distributed locking. |
| | 220 | 408 | | .AddSingleton(DistributedLockProvider) |
| | 220 | 409 | | |
| | 220 | 410 | | // Workflow providers. |
| | 220 | 411 | | .AddWorkflowsProvider<ClrWorkflowsProvider>() |
| | 220 | 412 | | |
| | 220 | 413 | | // UI property handlers. |
| | 220 | 414 | | .AddScoped<IPropertyUIHandler, DispatcherChannelOptionsProvider>() |
| | 220 | 415 | | |
| | 220 | 416 | | // Domain handlers. |
| | 220 | 417 | | .AddCommandHandler<DispatchWorkflowCommandHandler>() |
| | 220 | 418 | | .AddCommandHandler<DispatchStimulusCommandHandler>() |
| | 220 | 419 | | .AddCommandHandler<CancelWorkflowsCommandHandler>() |
| | 220 | 420 | | .AddNotificationHandler<ResumeDispatchWorkflowActivity>() |
| | 220 | 421 | | .AddNotificationHandler<ResumeBulkDispatchWorkflowActivity>() |
| | 220 | 422 | | .AddNotificationHandler<ProcessWorkflowDispatchOutbox>() |
| | 220 | 423 | | .AddNotificationHandler<ResumeExecuteWorkflowActivity>() |
| | 220 | 424 | | .AddNotificationHandler<IndexTriggers>() |
| | 220 | 425 | | .AddNotificationHandler<CancelBackgroundActivities>() |
| | 220 | 426 | | .AddNotificationHandler<DeleteBookmarks>() |
| | 220 | 427 | | .AddNotificationHandler<DeleteTriggers>() |
| | 220 | 428 | | .AddNotificationHandler<DeleteActivityExecutionLogRecords>() |
| | 220 | 429 | | .AddNotificationHandler<DeleteWorkflowExecutionLogRecords>() |
| | 220 | 430 | | .AddNotificationHandler<RefreshActivityRegistry>() |
| | 220 | 431 | | .AddNotificationHandler<SignalBookmarkQueueWorker>() |
| | 220 | 432 | | .AddNotificationHandler<EvaluateParentLogPersistenceModes>() |
| | 220 | 433 | | .AddNotificationHandler<CaptureActivityExecutionState>() |
| | 220 | 434 | | .AddNotificationHandler<ValidateWorkflowRequestHandler>() |
| | 220 | 435 | | |
| | 220 | 436 | | // Workflow activation strategies. |
| | 220 | 437 | | .AddScoped<IWorkflowActivationStrategy, SingletonStrategy>() |
| | 220 | 438 | | .AddScoped<IWorkflowActivationStrategy, CorrelatedSingletonStrategy>() |
| | 220 | 439 | | .AddScoped<IWorkflowActivationStrategy, CorrelationStrategy>() |
| | 220 | 440 | | ; |
| | | 441 | | |
| | 220 | 442 | | Services.TryAddScoped<IWorkflowDispatchOutbox>(sp => ActivatorUtilities.CreateInstance<WorkflowDispatchOutbox>(s |
| | 220 | 443 | | Services.TryAddScoped(WorkflowDispatchOutboxStore); |
| | 220 | 444 | | Services.TryAddScoped<IWorkflowDispatchOutboxProcessor, WorkflowDispatchOutboxProcessor>(); |
| | 220 | 445 | | } |
| | | 446 | | |
| | | 447 | | private void RegisterWorkflowTypeAliases(SerializationTypeOptions options) |
| | | 448 | | { |
| | 200 | 449 | | WorkflowRuntimeTypeAliasRegistrar.Register(options, GetRegisteredWorkflowTypes()); |
| | 200 | 450 | | } |
| | | 451 | | |
| | | 452 | | private IEnumerable<Type> GetRegisteredWorkflowTypes() |
| | | 453 | | { |
| | 200 | 454 | | return WorkflowTypes |
| | 643 | 455 | | .Concat(Workflows.Keys.Select(TryResolveWorkflowType).Where(type => type != null).Select(type => type!)) |
| | 200 | 456 | | .Distinct(); |
| | | 457 | | } |
| | | 458 | | |
| | | 459 | | private static Type? TryResolveWorkflowType(string typeName) |
| | | 460 | | { |
| | | 461 | | Type? type; |
| | | 462 | | |
| | | 463 | | try |
| | | 464 | | { |
| | 429 | 465 | | type = Type.GetType(typeName, false); |
| | 429 | 466 | | } |
| | 0 | 467 | | catch (Exception e) when (e is ArgumentException or FileLoadException or FileNotFoundException or TypeLoadExcept |
| | | 468 | | { |
| | 0 | 469 | | return null; |
| | | 470 | | } |
| | | 471 | | |
| | 429 | 472 | | return type != null && typeof(IWorkflow).IsAssignableFrom(type) && type is { IsAbstract: false, IsInterface: fal |
| | 429 | 473 | | ? type |
| | 429 | 474 | | : null; |
| | 0 | 475 | | } |
| | | 476 | | } |