| | | 1 | | using CShells.Features; |
| | | 2 | | using Elsa.Common.RecurringTasks; |
| | | 3 | | using Elsa.Extensions; |
| | | 4 | | using Elsa.Mediator.Contracts; |
| | | 5 | | using Elsa.Workflows.CommitStates; |
| | | 6 | | using Elsa.Workflows.Management; |
| | | 7 | | using Elsa.Workflows.Management.Contracts; |
| | | 8 | | using Elsa.Workflows.Management.Services; |
| | | 9 | | using Elsa.Workflows.Runtime.ActivationValidators; |
| | | 10 | | using Elsa.Workflows.Runtime.Entities; |
| | | 11 | | using Elsa.Workflows.Runtime.Handlers; |
| | | 12 | | using Elsa.Workflows.Runtime.Options; |
| | | 13 | | using Elsa.Workflows.Runtime.Providers; |
| | | 14 | | using Elsa.Workflows.Runtime.Stores; |
| | | 15 | | using Elsa.Workflows.Runtime.Tasks; |
| | | 16 | | using Elsa.Workflows.Runtime.UIHints; |
| | | 17 | | using Medallion.Threading; |
| | | 18 | | using Medallion.Threading.FileSystem; |
| | | 19 | | using Microsoft.Extensions.DependencyInjection; |
| | | 20 | | |
| | | 21 | | namespace Elsa.Workflows.Runtime.ShellFeatures; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Installs and configures workflow runtime features. |
| | | 25 | | /// </summary> |
| | | 26 | | [ShellFeature( |
| | | 27 | | DisplayName = "Workflow Runtime", |
| | | 28 | | Description = "Provides workflow execution runtime and scheduling capabilities", |
| | | 29 | | DependsOn = ["Workflows"])] |
| | | 30 | | public class WorkflowRuntimeFeature : IShellFeature |
| | | 31 | | { |
| | 0 | 32 | | private IDictionary<string, DispatcherChannel> WorkflowDispatcherChannels { get; set; } = new Dictionary<string, Dis |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// A list of workflow builders configured during application startup. |
| | | 36 | | /// </summary> |
| | 0 | 37 | | public IDictionary<string, Func<IServiceProvider, ValueTask<IWorkflow>>> Workflows { get; set; } = new Dictionary<st |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// A factory that instantiates a concrete <see cref="IWorkflowRuntime"/>. |
| | | 41 | | /// </summary> |
| | 0 | 42 | | public Func<IServiceProvider, IWorkflowRuntime> WorkflowRuntime { get; set; } = sp => ActivatorUtilities.CreateInsta |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// A factory that instantiates an <see cref="IWorkflowDispatcher"/>. |
| | | 46 | | /// </summary> |
| | 0 | 47 | | public Func<IServiceProvider, IWorkflowDispatcher> WorkflowDispatcher { get; set; } = sp => |
| | 0 | 48 | | { |
| | 0 | 49 | | var decoratedService = ActivatorUtilities.CreateInstance<BackgroundWorkflowDispatcher>(sp); |
| | 0 | 50 | | return ActivatorUtilities.CreateInstance<ValidatingWorkflowDispatcher>(sp, decoratedService); |
| | 0 | 51 | | }; |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// A factory that instantiates an <see cref="IStimulusDispatcher"/>. |
| | | 55 | | /// </summary> |
| | 0 | 56 | | public Func<IServiceProvider, IStimulusDispatcher> StimulusDispatcher { get; set; } = sp => ActivatorUtilities.Creat |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// A factory that instantiates an <see cref="IWorkflowCancellationDispatcher"/>. |
| | | 60 | | /// </summary> |
| | 0 | 61 | | public Func<IServiceProvider, IWorkflowCancellationDispatcher> WorkflowCancellationDispatcher { get; set; } = sp => |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// A factory that instantiates an <see cref="IBookmarkStore"/>. |
| | | 65 | | /// </summary> |
| | 0 | 66 | | public Func<IServiceProvider, IBookmarkStore> BookmarkStore { get; set; } = sp => sp.GetRequiredService<MemoryBookma |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// A factory that instantiates an <see cref="IBookmarkQueueStore"/>. |
| | | 70 | | /// </summary> |
| | 0 | 71 | | public Func<IServiceProvider, IBookmarkQueueStore> BookmarkQueueStore { get; set; } = sp => sp.GetRequiredService<Me |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// A factory that instantiates an <see cref="ITriggerStore"/>. |
| | | 75 | | /// </summary> |
| | 0 | 76 | | public Func<IServiceProvider, ITriggerStore> TriggerStore { get; set; } = sp => sp.GetRequiredService<MemoryTriggerS |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// A factory that instantiates an <see cref="IWorkflowExecutionLogStore"/>. |
| | | 80 | | /// </summary> |
| | 0 | 81 | | public Func<IServiceProvider, IWorkflowExecutionLogStore> WorkflowExecutionLogStore { get; set; } = sp => sp.GetRequ |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// A factory that instantiates an <see cref="IActivityExecutionStore"/>. |
| | | 85 | | /// </summary> |
| | 0 | 86 | | public Func<IServiceProvider, IActivityExecutionStore> ActivityExecutionLogStore { get; set; } = sp => sp.GetRequire |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// A factory that instantiates an <see cref="IDistributedLockProvider"/>. |
| | | 90 | | /// </summary> |
| | 0 | 91 | | public Func<IServiceProvider, IDistributedLockProvider> DistributedLockProvider { get; set; } = _ => new FileDistrib |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// A factory that instantiates an <see cref="ITaskDispatcher"/>. |
| | | 95 | | /// </summary> |
| | 0 | 96 | | public Func<IServiceProvider, ITaskDispatcher> RunTaskDispatcher { get; set; } = sp => sp.GetRequiredService<Backgro |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// A factory that instantiates an <see cref="IBackgroundActivityScheduler"/>. |
| | | 100 | | /// </summary> |
| | 0 | 101 | | public Func<IServiceProvider, IBackgroundActivityScheduler> BackgroundActivityScheduler { get; set; } = sp => Activa |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// A factory that instantiates a log record sink for an <see cref="ActivityExecutionRecord"/>. |
| | | 105 | | /// </summary> |
| | 0 | 106 | | public Func<IServiceProvider, ILogRecordSink<ActivityExecutionRecord>> ActivityExecutionLogSink { get; set; } = sp = |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// A factory that instantiates a log record sink for an <see cref="WorkflowExecutionLogRecord"/>. |
| | | 110 | | /// </summary> |
| | 0 | 111 | | public Func<IServiceProvider, ILogRecordSink<WorkflowExecutionLogRecord>> WorkflowExecutionLogSink { get; set; } = s |
| | | 112 | | |
| | | 113 | | /// <summary> |
| | | 114 | | /// A factory that instantiates an <see cref="ICommandHandler"/>. |
| | | 115 | | /// </summary> |
| | 0 | 116 | | public Func<IServiceProvider, ICommandHandler> DispatchWorkflowCommandHandler { get; set; } = sp => sp.GetRequiredSe |
| | | 117 | | |
| | | 118 | | /// <summary> |
| | | 119 | | /// A factory that instantiates an <see cref="IWorkflowResumer"/>. |
| | | 120 | | /// </summary> |
| | 0 | 121 | | public Func<IServiceProvider, IWorkflowResumer> WorkflowResumer { get; set; } = sp => sp.GetRequiredService<Workflow |
| | | 122 | | |
| | | 123 | | /// <summary> |
| | | 124 | | /// A factory that instantiates an <see cref="IBookmarkQueueWorker"/>. |
| | | 125 | | /// </summary> |
| | 0 | 126 | | public Func<IServiceProvider, IBookmarkQueueWorker> BookmarkQueueWorker { get; set; } = sp => sp.GetRequiredService< |
| | | 127 | | |
| | | 128 | | |
| | | 129 | | public void ConfigureServices(IServiceCollection services) |
| | | 130 | | { |
| | | 131 | | // Options. |
| | 0 | 132 | | services.Configure<RuntimeOptions>(options => { options.Workflows = Workflows; }); |
| | 0 | 133 | | services.Configure<WorkflowDispatcherOptions>(options => |
| | 0 | 134 | | { |
| | 0 | 135 | | options.Channels.AddRange(WorkflowDispatcherChannels.Values); |
| | 0 | 136 | | }); |
| | | 137 | | |
| | 0 | 138 | | services |
| | 0 | 139 | | // Core. |
| | 0 | 140 | | .AddScoped<ITriggerIndexer, TriggerIndexer>() |
| | 0 | 141 | | .AddScoped<IWorkflowInstanceFactory, WorkflowInstanceFactory>() |
| | 0 | 142 | | .AddScoped<IWorkflowHostFactory, WorkflowHostFactory>() |
| | 0 | 143 | | .AddScoped<IBackgroundActivityInvoker, BackgroundActivityInvoker>() |
| | 0 | 144 | | .AddScoped(WorkflowRuntime) |
| | 0 | 145 | | .AddScoped(WorkflowDispatcher) |
| | 0 | 146 | | .AddScoped(StimulusDispatcher) |
| | 0 | 147 | | .AddScoped(WorkflowCancellationDispatcher) |
| | 0 | 148 | | .AddScoped(RunTaskDispatcher) |
| | 0 | 149 | | .AddScoped(ActivityExecutionLogSink) |
| | 0 | 150 | | .AddScoped(WorkflowExecutionLogSink) |
| | 0 | 151 | | .AddSingleton(BackgroundActivityScheduler) |
| | 0 | 152 | | .AddSingleton<RandomLongIdentityGenerator>() |
| | 0 | 153 | | .AddSingleton<IBookmarkQueueSignaler, BookmarkQueueSignaler>() |
| | 0 | 154 | | .AddScoped(BookmarkQueueWorker) |
| | 0 | 155 | | .AddScoped<IBookmarkManager, DefaultBookmarkManager>() |
| | 0 | 156 | | .AddScoped<IActivityExecutionManager, DefaultActivityExecutionManager>() |
| | 0 | 157 | | .AddScoped<IActivityExecutionStatsService, ActivityExecutionStatsService>() |
| | 0 | 158 | | .AddScoped<IActivityExecutionMapper, DefaultActivityExecutionMapper>() |
| | 0 | 159 | | .AddScoped<IWorkflowDefinitionStorePopulator, DefaultWorkflowDefinitionStorePopulator>() |
| | 0 | 160 | | .AddScoped<IRegistriesPopulator, DefaultRegistriesPopulator>() |
| | 0 | 161 | | .AddScoped<IWorkflowDefinitionsRefresher, WorkflowDefinitionsRefresher>() |
| | 0 | 162 | | .AddScoped<IWorkflowDefinitionsReloader, WorkflowDefinitionsReloader>() |
| | 0 | 163 | | .AddScoped<IWorkflowRegistry, DefaultWorkflowRegistry>() |
| | 0 | 164 | | .AddScoped<IWorkflowMatcher, WorkflowMatcher>() |
| | 0 | 165 | | .AddScoped<IWorkflowInvoker, WorkflowInvoker>() |
| | 0 | 166 | | .AddScoped<IStimulusSender, StimulusSender>() |
| | 0 | 167 | | .AddScoped<ITriggerBoundWorkflowService, TriggerBoundWorkflowService>() |
| | 0 | 168 | | .AddScoped<IBookmarkBoundWorkflowService, BookmarkBoundWorkflowService>() |
| | 0 | 169 | | .AddScoped<ITaskReporter, TaskReporter>() |
| | 0 | 170 | | .AddScoped<SynchronousTaskDispatcher>() |
| | 0 | 171 | | .AddScoped<BackgroundTaskDispatcher>() |
| | 0 | 172 | | .AddScoped<StoreActivityExecutionLogSink>() |
| | 0 | 173 | | .AddScoped<StoreWorkflowExecutionLogSink>() |
| | 0 | 174 | | .AddScoped<DispatchWorkflowCommandHandler>() |
| | 0 | 175 | | .AddScoped<IEventPublisher, EventPublisher>() |
| | 0 | 176 | | .AddScoped<IBookmarkUpdater, BookmarkUpdater>() |
| | 0 | 177 | | .AddScoped<IBookmarksPersister, BookmarksPersister>() |
| | 0 | 178 | | .AddScoped<IBookmarkResumer, BookmarkResumer>() |
| | 0 | 179 | | .AddScoped<IBookmarkQueue, StoreBookmarkQueue>() |
| | 0 | 180 | | .AddScoped<WorkflowResumer>() |
| | 0 | 181 | | .AddScoped<BookmarkQueueWorker>() |
| | 0 | 182 | | .AddScoped(WorkflowResumer) |
| | 0 | 183 | | .AddScoped<ITriggerInvoker, TriggerInvoker>() |
| | 0 | 184 | | .AddScoped<IWorkflowCanceler, WorkflowCanceler>() |
| | 0 | 185 | | .AddScoped<IWorkflowCancellationService, WorkflowCancellationService>() |
| | 0 | 186 | | .AddScoped<IWorkflowActivationStrategyEvaluator, DefaultWorkflowActivationStrategyEvaluator>() |
| | 0 | 187 | | .AddScoped<IWorkflowStarter, DefaultWorkflowStarter>() |
| | 0 | 188 | | .AddScoped<IWorkflowRestarter, DefaultWorkflowRestarter>() |
| | 0 | 189 | | .AddScoped<IBookmarkQueuePurger, DefaultBookmarkQueuePurger>() |
| | 0 | 190 | | .AddScoped<ILogRecordExtractor<WorkflowExecutionLogRecord>, WorkflowExecutionLogRecordExtractor>() |
| | 0 | 191 | | .AddScoped<IActivityPropertyLogPersistenceEvaluator, ActivityPropertyLogPersistenceEvaluator>() |
| | 0 | 192 | | .AddScoped<IBookmarkQueueProcessor, BookmarkQueueProcessor>() |
| | 0 | 193 | | .AddScoped<ICommitStateHandler, DefaultCommitStateHandler>() |
| | 0 | 194 | | .AddScoped<WorkflowHeartbeatGeneratorFactory>() |
| | 0 | 195 | | |
| | 0 | 196 | | // Deprecated services. |
| | 0 | 197 | | .AddScoped<IWorkflowInbox, StimulusProxyWorkflowInbox>() |
| | 0 | 198 | | |
| | 0 | 199 | | // Stores. |
| | 0 | 200 | | .AddScoped(BookmarkStore) |
| | 0 | 201 | | .AddScoped(BookmarkQueueStore) |
| | 0 | 202 | | .AddScoped(TriggerStore) |
| | 0 | 203 | | .AddScoped(WorkflowExecutionLogStore) |
| | 0 | 204 | | .AddScoped(ActivityExecutionLogStore) |
| | 0 | 205 | | |
| | 0 | 206 | | // Lazy services. |
| | 0 | 207 | | .AddScoped<Func<IEnumerable<IWorkflowsProvider>>>(sp => sp.GetServices<IWorkflowsProvider>) |
| | 0 | 208 | | .AddScoped<Func<IEnumerable<IWorkflowMaterializer>>>(sp => sp.GetServices<IWorkflowMaterializer>) |
| | 0 | 209 | | |
| | 0 | 210 | | // Noop stores. |
| | 0 | 211 | | .AddScoped<MemoryWorkflowExecutionLogStore>() |
| | 0 | 212 | | .AddScoped<MemoryActivityExecutionStore>() |
| | 0 | 213 | | |
| | 0 | 214 | | // Memory stores. |
| | 0 | 215 | | .AddMemoryStore<StoredBookmark, MemoryBookmarkStore>() |
| | 0 | 216 | | .AddMemoryStore<StoredTrigger, MemoryTriggerStore>() |
| | 0 | 217 | | .AddMemoryStore<BookmarkQueueItem, MemoryBookmarkQueueStore>() |
| | 0 | 218 | | .AddMemoryStore<WorkflowExecutionLogRecord, MemoryWorkflowExecutionLogStore>() |
| | 0 | 219 | | .AddMemoryStore<ActivityExecutionRecord, MemoryActivityExecutionStore>() |
| | 0 | 220 | | |
| | 0 | 221 | | // Startup tasks, background tasks, and recurring tasks. |
| | 0 | 222 | | .AddStartupTask<PopulateRegistriesStartupTask>() |
| | 0 | 223 | | .AddRecurringTask<TriggerBookmarkQueueRecurringTask>(TimeSpan.FromMinutes(1)) |
| | 0 | 224 | | .AddRecurringTask<PurgeBookmarkQueueRecurringTask>(TimeSpan.FromSeconds(10)) |
| | 0 | 225 | | .AddRecurringTask<RestartInterruptedWorkflowsTask>(TimeSpan.FromMinutes(5)) // Same default as the workflow |
| | 0 | 226 | | |
| | 0 | 227 | | // Distributed locking. |
| | 0 | 228 | | .AddSingleton(DistributedLockProvider) |
| | 0 | 229 | | |
| | 0 | 230 | | // Workflow definition providers. |
| | 0 | 231 | | .AddWorkflowDefinitionProvider<ClrWorkflowsProvider>() |
| | 0 | 232 | | |
| | 0 | 233 | | // UI property handlers. |
| | 0 | 234 | | .AddScoped<IPropertyUIHandler, DispatcherChannelOptionsProvider>() |
| | 0 | 235 | | |
| | 0 | 236 | | // Domain handlers. |
| | 0 | 237 | | .AddCommandHandler<DispatchWorkflowCommandHandler>() |
| | 0 | 238 | | .AddCommandHandler<DispatchStimulusCommandHandler>() |
| | 0 | 239 | | .AddCommandHandler<CancelWorkflowsCommandHandler>() |
| | 0 | 240 | | .AddNotificationHandler<ResumeDispatchWorkflowActivity>() |
| | 0 | 241 | | .AddNotificationHandler<ResumeBulkDispatchWorkflowActivity>() |
| | 0 | 242 | | .AddNotificationHandler<ResumeExecuteWorkflowActivity>() |
| | 0 | 243 | | .AddNotificationHandler<IndexTriggers>() |
| | 0 | 244 | | .AddNotificationHandler<CancelBackgroundActivities>() |
| | 0 | 245 | | .AddNotificationHandler<DeleteBookmarks>() |
| | 0 | 246 | | .AddNotificationHandler<DeleteTriggers>() |
| | 0 | 247 | | .AddNotificationHandler<DeleteActivityExecutionLogRecords>() |
| | 0 | 248 | | .AddNotificationHandler<DeleteWorkflowExecutionLogRecords>() |
| | 0 | 249 | | .AddNotificationHandler<RefreshActivityRegistry>() |
| | 0 | 250 | | .AddNotificationHandler<SignalBookmarkQueueWorker>() |
| | 0 | 251 | | .AddNotificationHandler<EvaluateParentLogPersistenceModes>() |
| | 0 | 252 | | .AddNotificationHandler<CaptureActivityExecutionState>() |
| | 0 | 253 | | .AddNotificationHandler<ValidateWorkflowRequestHandler>() |
| | 0 | 254 | | |
| | 0 | 255 | | // Workflow activation strategies. |
| | 0 | 256 | | .AddScoped<IWorkflowActivationStrategy, SingletonStrategy>() |
| | 0 | 257 | | .AddScoped<IWorkflowActivationStrategy, CorrelatedSingletonStrategy>() |
| | 0 | 258 | | .AddScoped<IWorkflowActivationStrategy, CorrelationStrategy>() |
| | 0 | 259 | | ; |
| | 0 | 260 | | } |
| | | 261 | | } |