2022-04-10 12:35:53 +00:00
|
|
|
using System.Threading.Channels;
|
2022-01-04 08:42:12 +00:00
|
|
|
using Elsa.Expressions;
|
|
|
|
|
using Elsa.Mediator.Extensions;
|
|
|
|
|
using Elsa.Runtime.HostedServices;
|
2022-04-25 09:43:23 +00:00
|
|
|
using Elsa.Runtime.Implementations;
|
2022-01-04 08:42:12 +00:00
|
|
|
using Elsa.Runtime.Interpreters;
|
2022-04-10 12:35:53 +00:00
|
|
|
using Elsa.Runtime.Models;
|
2022-01-04 08:42:12 +00:00
|
|
|
using Elsa.Runtime.Options;
|
|
|
|
|
using Elsa.Runtime.Services;
|
|
|
|
|
using Elsa.Runtime.Stimuli.Handlers;
|
|
|
|
|
using Elsa.Runtime.WorkflowProviders;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2022-04-10 12:35:53 +00:00
|
|
|
using Microsoft.Extensions.Options;
|
2022-01-04 08:42:12 +00:00
|
|
|
|
|
|
|
|
namespace Elsa.Runtime.Extensions;
|
|
|
|
|
|
|
|
|
|
public static class ServiceCollectionExtensions
|
|
|
|
|
{
|
|
|
|
|
public static IServiceCollection AddElsaRuntime(this IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
services.AddOptions<WorkflowRuntimeOptions>();
|
|
|
|
|
|
|
|
|
|
return services
|
|
|
|
|
// Core.
|
|
|
|
|
.AddSingleton<IWorkflowRegistry, WorkflowRegistry>()
|
|
|
|
|
.AddSingleton<IStimulusInterpreter, StimulusInterpreter>()
|
|
|
|
|
.AddSingleton<IWorkflowInstructionExecutor, WorkflowInstructionExecutor>()
|
|
|
|
|
.AddSingleton<ITriggerIndexer, TriggerIndexer>()
|
2022-02-22 11:51:59 +00:00
|
|
|
.AddSingleton<IBookmarkManager, BookmarkManager>()
|
2022-04-10 12:35:53 +00:00
|
|
|
.AddSingleton(sp => sp.GetRequiredService<IOptions<WorkflowRuntimeOptions>>().Value.WorkflowInvokerFactory(sp))
|
|
|
|
|
.AddSingleton(sp => sp.GetRequiredService<IOptions<WorkflowRuntimeOptions>>().Value.WorkflowDispatcherFactory(sp))
|
2022-01-04 08:42:12 +00:00
|
|
|
|
|
|
|
|
// Stimulus handlers.
|
|
|
|
|
.AddStimulusHandler<TriggerWorkflowsStimulusHandler>()
|
|
|
|
|
.AddStimulusHandler<ResumeWorkflowsStimulusHandler>()
|
|
|
|
|
|
|
|
|
|
// Instruction interpreters.
|
|
|
|
|
.AddInstructionInterpreter<TriggerWorkflowInstructionInterpreter>()
|
|
|
|
|
.AddInstructionInterpreter<ResumeWorkflowInstructionInterpreter>()
|
|
|
|
|
|
|
|
|
|
// Workflow providers.
|
|
|
|
|
.AddWorkflowProvider<ConfigurationWorkflowProvider>()
|
|
|
|
|
.AddWorkflowProvider<DatabaseWorkflowProvider>()
|
|
|
|
|
|
|
|
|
|
// Workflow engine.
|
2022-03-22 13:33:15 +00:00
|
|
|
.AddSingleton<IWorkflowService, WorkflowService>()
|
2022-01-04 08:42:12 +00:00
|
|
|
|
2022-01-18 17:24:52 +00:00
|
|
|
// Domain event handlers.
|
2022-01-04 08:42:12 +00:00
|
|
|
.AddNotificationHandlersFrom(typeof(ServiceCollectionExtensions))
|
2022-04-10 12:35:53 +00:00
|
|
|
|
2022-01-18 17:24:52 +00:00
|
|
|
// Hosted Services.
|
2022-02-22 11:51:59 +00:00
|
|
|
.AddHostedService<RegisterDescriptors>()
|
2022-04-10 12:35:53 +00:00
|
|
|
.AddHostedService<RegisterExpressionSyntaxDescriptors>()
|
|
|
|
|
.AddHostedService<DispatchedWorkflowDefinitionWorker>()
|
|
|
|
|
.AddHostedService<DispatchedWorkflowInstanceWorker>()
|
|
|
|
|
|
|
|
|
|
// Channels for dispatching workflows in-memory.
|
|
|
|
|
.CreateChannel<DispatchWorkflowDefinitionRequest>()
|
|
|
|
|
.CreateChannel<DispatchWorkflowInstanceRequest>()
|
2022-01-04 08:42:12 +00:00
|
|
|
;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-10 12:35:53 +00:00
|
|
|
private static IServiceCollection CreateChannel<T>(this IServiceCollection services) =>
|
|
|
|
|
services
|
|
|
|
|
.AddSingleton(CreateChannel<T>())
|
|
|
|
|
.AddSingleton(CreateChannelReader<T>)
|
|
|
|
|
.AddSingleton(CreateChannelWriter<T>);
|
|
|
|
|
|
|
|
|
|
private static Channel<T> CreateChannel<T>() => Channel.CreateUnbounded<T>(new UnboundedChannelOptions());
|
|
|
|
|
private static ChannelReader<T> CreateChannelReader<T>(IServiceProvider serviceProvider) => serviceProvider.GetRequiredService<Channel<T>>().Reader;
|
|
|
|
|
private static ChannelWriter<T> CreateChannelWriter<T>(IServiceProvider serviceProvider) => serviceProvider.GetRequiredService<Channel<T>>().Writer;
|
|
|
|
|
|
2022-04-11 12:19:55 +00:00
|
|
|
public static IServiceCollection AddDefaultExpressionHandlers(this IServiceCollection services) =>
|
2022-01-04 08:42:12 +00:00
|
|
|
services
|
|
|
|
|
.AddExpressionHandler<LiteralExpressionHandler, LiteralExpression>()
|
|
|
|
|
.AddExpressionHandler<DelegateExpressionHandler, DelegateExpression>()
|
|
|
|
|
.AddExpressionHandler<VariableExpressionHandler, VariableExpression>()
|
2022-04-27 20:32:40 +00:00
|
|
|
.AddExpressionHandler<JsonExpressionHandler, JsonExpression>()
|
2022-04-11 12:19:55 +00:00
|
|
|
.AddExpressionHandler<OutputExpressionHandler, OutputExpression>()
|
2022-01-04 08:42:12 +00:00
|
|
|
.AddExpressionHandler<ElsaExpressionHandler, ElsaExpression>();
|
|
|
|
|
|
|
|
|
|
public static IServiceCollection AddWorkflowProvider<T>(this IServiceCollection services) where T : class, IWorkflowProvider => services.AddSingleton<IWorkflowProvider, T>();
|
|
|
|
|
public static IServiceCollection AddStimulusHandler<T>(this IServiceCollection services) where T : class, IStimulusHandler => services.AddSingleton<IStimulusHandler, T>();
|
|
|
|
|
public static IServiceCollection AddInstructionInterpreter<T>(this IServiceCollection services) where T : class, IWorkflowInstructionInterpreter => services.AddSingleton<IWorkflowInstructionInterpreter, T>();
|
|
|
|
|
public static IServiceCollection ConfigureWorkflowRuntime(this IServiceCollection services, Action<WorkflowRuntimeOptions> configure) => services.Configure(configure);
|
2022-02-22 11:51:59 +00:00
|
|
|
public static IServiceCollection IndexWorkflowTriggers(this IServiceCollection services) => services.AddHostedService<IndexWorkflowTriggers>();
|
2022-01-04 08:42:12 +00:00
|
|
|
}
|