From 8ec2aa5b36845a478214eb002f6ddfb2c4c12919 Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Fri, 13 Jan 2023 19:53:37 +0100 Subject: [PATCH 1/2] Remove elastic hosted service for alias/rollover --- .../Features/ElasticsearchFeature.cs | 11 ---- .../ConfigureAliasesHostedService.cs | 62 ------------------- 2 files changed, 73 deletions(-) delete mode 100644 src/modules/Elsa.Elasticsearch/HostedServices/ConfigureAliasesHostedService.cs diff --git a/src/modules/Elsa.Elasticsearch/Features/ElasticsearchFeature.cs b/src/modules/Elsa.Elasticsearch/Features/ElasticsearchFeature.cs index 497bc5687..3f1261415 100644 --- a/src/modules/Elsa.Elasticsearch/Features/ElasticsearchFeature.cs +++ b/src/modules/Elsa.Elasticsearch/Features/ElasticsearchFeature.cs @@ -1,14 +1,9 @@ using Elastic.Clients.Elasticsearch; using Elsa.Elasticsearch.Extensions; -using Elsa.Elasticsearch.HostedServices; -using Elsa.Elasticsearch.Modules.Management; -using Elsa.Elasticsearch.Modules.Runtime; using Elsa.Elasticsearch.Options; using Elsa.Elasticsearch.Services; using Elsa.Features.Abstractions; using Elsa.Features.Services; -using Elsa.Workflows.Management.Entities; -using Elsa.Workflows.Runtime.Entities; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; @@ -30,12 +25,6 @@ public class ElasticsearchFeature : FeatureBase /// public Action Options { get; set; } = _ => { }; - /// - public override void ConfigureHostedServices() - { - Module.ConfigureHostedService(-2); - } - /// public override void Apply() { diff --git a/src/modules/Elsa.Elasticsearch/HostedServices/ConfigureAliasesHostedService.cs b/src/modules/Elsa.Elasticsearch/HostedServices/ConfigureAliasesHostedService.cs deleted file mode 100644 index 761e01baa..000000000 --- a/src/modules/Elsa.Elasticsearch/HostedServices/ConfigureAliasesHostedService.cs +++ /dev/null @@ -1,62 +0,0 @@ -using Elastic.Clients.Elasticsearch; -using Elastic.Clients.Elasticsearch.IndexManagement; -using Elsa.Elasticsearch.Options; -using Elsa.Elasticsearch.Services; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Options; - -namespace Elsa.Elasticsearch.HostedServices; - -/// -/// Configures aliases. -/// -public class ConfigureAliasesHostedService : IHostedService -{ - private readonly ElasticsearchClient _client; - private readonly ElasticsearchOptions _options; - private readonly IEnumerable _configurations; - - /// - /// Constructor. - /// - public ConfigureAliasesHostedService(ElasticsearchClient client, IOptions options, IEnumerable configurations) - { - _client = client; - _options = options.Value; - _configurations = configurations; - } - - /// - public async Task StartAsync(CancellationToken cancellationToken) - { - await ConfigureClientAsync(cancellationToken); - } - - /// - public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; - - private async Task ConfigureClientAsync(CancellationToken cancellationToken) - { - foreach (var configuration in _configurations) - await configuration.ConfigureClientAsync(_client, cancellationToken); - - foreach (var configuration in _configurations) - { - var alias = _options.GetIndexNameFor(configuration.DocumentType); - var indexName = configuration.IndexNamingStrategy.GenerateName(alias); - var indexExists = (await _client.Indices.ExistsAsync(indexName, cancellationToken)).Exists; - - if (indexExists) - continue; - - var response = await _client.Indices.CreateAsync(indexName, c => c - .Aliases(a => a.Add(alias, new Alias { IsWriteIndex = true })), cancellationToken); - - if (response.IsValidResponse) - continue; - - if (response.TryGetOriginalException(out var exception)) - throw exception!; - } - } -} \ No newline at end of file From c6ceb247e1eb78f6aef447331ae978a3e754726a Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Fri, 13 Jan 2023 20:10:05 +0100 Subject: [PATCH 2/2] Fix dependency issues --- ...oreWorkflowManagementPersistenceFeature.cs | 2 +- .../Modules/Runtime/Extensions.cs | 11 -------- .../Features/WorkflowManagementFeature.cs | 1 + .../Extensions/ModuleExtensions.cs | 6 +++++ .../AsyncWorkflowStateExporterFeature.cs | 26 +++++++++++++++++++ .../Features/WorkflowRuntimeFeature.cs | 4 +-- .../Elsa.Samples.TelnyxIntegration/Program.cs | 2 +- 7 files changed, 36 insertions(+), 16 deletions(-) create mode 100644 src/modules/Elsa.Workflows.Runtime/Features/AsyncWorkflowStateExporterFeature.cs diff --git a/src/modules/Elsa.EntityFrameworkCore/Modules/Management/EFCoreWorkflowManagementPersistenceFeature.cs b/src/modules/Elsa.EntityFrameworkCore/Modules/Management/EFCoreWorkflowManagementPersistenceFeature.cs index 0f84f93a6..06be22263 100644 --- a/src/modules/Elsa.EntityFrameworkCore/Modules/Management/EFCoreWorkflowManagementPersistenceFeature.cs +++ b/src/modules/Elsa.EntityFrameworkCore/Modules/Management/EFCoreWorkflowManagementPersistenceFeature.cs @@ -33,6 +33,6 @@ public class EFCoreWorkflowManagementPersistenceFeature : PersistenceFeatureBase base.Apply(); AddStore(); - AddStore(); + AddStore(); } } \ No newline at end of file diff --git a/src/modules/Elsa.EntityFrameworkCore/Modules/Runtime/Extensions.cs b/src/modules/Elsa.EntityFrameworkCore/Modules/Runtime/Extensions.cs index 42805a759..40beb7b49 100644 --- a/src/modules/Elsa.EntityFrameworkCore/Modules/Runtime/Extensions.cs +++ b/src/modules/Elsa.EntityFrameworkCore/Modules/Runtime/Extensions.cs @@ -1,6 +1,4 @@ using Elsa.Workflows.Runtime.Features; -using Elsa.Workflows.Runtime.Implementations; -using Microsoft.Extensions.DependencyInjection; namespace Elsa.EntityFrameworkCore.Modules.Runtime; @@ -26,13 +24,4 @@ public static class Extensions feature.Module.Configure(configure); return feature; } - - /// - /// Configures the workflow runtime to send workflow state to the . - /// - public static WorkflowRuntimeFeature UseAsyncWorkflowStateExporter(this WorkflowRuntimeFeature feature) - { - feature.WorkflowStateExporter = sp => ActivatorUtilities.CreateInstance(sp); - return feature; - } } \ No newline at end of file diff --git a/src/modules/Elsa.Workflows.Management/Features/WorkflowManagementFeature.cs b/src/modules/Elsa.Workflows.Management/Features/WorkflowManagementFeature.cs index 9fc1782a0..0c089e079 100644 --- a/src/modules/Elsa.Workflows.Management/Features/WorkflowManagementFeature.cs +++ b/src/modules/Elsa.Workflows.Management/Features/WorkflowManagementFeature.cs @@ -29,6 +29,7 @@ namespace Elsa.Workflows.Management.Features; [DependsOn(typeof(MediatorFeature))] [DependsOn(typeof(SystemClockFeature))] [DependsOn(typeof(WorkflowsFeature))] +[DependsOn(typeof(WorkflowDefinitionsFeature))] public class WorkflowManagementFeature : FeatureBase { private const string PrimitivesCategory = "Primitives"; diff --git a/src/modules/Elsa.Workflows.Runtime/Extensions/ModuleExtensions.cs b/src/modules/Elsa.Workflows.Runtime/Extensions/ModuleExtensions.cs index 42d09d2cc..3a95c04ce 100644 --- a/src/modules/Elsa.Workflows.Runtime/Extensions/ModuleExtensions.cs +++ b/src/modules/Elsa.Workflows.Runtime/Extensions/ModuleExtensions.cs @@ -39,4 +39,10 @@ public static class ModuleExtensions feature.Module.Configure(configure); return feature; } + + public static WorkflowRuntimeFeature UseAsyncWorkflowStateExporter(this WorkflowRuntimeFeature feature, Action? configure = default) + { + feature.Module.Configure(configure); + return feature; + } } \ No newline at end of file diff --git a/src/modules/Elsa.Workflows.Runtime/Features/AsyncWorkflowStateExporterFeature.cs b/src/modules/Elsa.Workflows.Runtime/Features/AsyncWorkflowStateExporterFeature.cs new file mode 100644 index 000000000..a38e7f300 --- /dev/null +++ b/src/modules/Elsa.Workflows.Runtime/Features/AsyncWorkflowStateExporterFeature.cs @@ -0,0 +1,26 @@ +using Elsa.Features.Abstractions; +using Elsa.Features.Attributes; +using Elsa.Features.Services; +using Elsa.Workflows.Management.Features; +using Elsa.Workflows.Runtime.Implementations; +using Microsoft.Extensions.DependencyInjection; + +namespace Elsa.Workflows.Runtime.Features; + +/// +/// Configures and enables . +/// +[DependsOn(typeof(WorkflowInstancesFeature))] +public class AsyncWorkflowStateExporterFeature : FeatureBase +{ + /// + public AsyncWorkflowStateExporterFeature(IModule module) : base(module) + { + } + + /// + public override void Configure() + { + Module.Configure(workflowRuntime => workflowRuntime.WorkflowStateExporter = sp => ActivatorUtilities.CreateInstance(sp)); + } +} \ No newline at end of file diff --git a/src/modules/Elsa.Workflows.Runtime/Features/WorkflowRuntimeFeature.cs b/src/modules/Elsa.Workflows.Runtime/Features/WorkflowRuntimeFeature.cs index b2de895d1..8ceefaee4 100644 --- a/src/modules/Elsa.Workflows.Runtime/Features/WorkflowRuntimeFeature.cs +++ b/src/modules/Elsa.Workflows.Runtime/Features/WorkflowRuntimeFeature.cs @@ -72,7 +72,7 @@ public class WorkflowRuntimeFeature : FeatureBase /// /// A factory that instantiates an . /// - public Func WorkflowStateExporter { get; set; } = sp => sp.GetRequiredService(); + public Func WorkflowStateExporter { get; set; } = ActivatorUtilities.GetServiceOrCreateInstance; /// /// A factory that instantiates an . @@ -145,8 +145,6 @@ public class WorkflowRuntimeFeature : FeatureBase .AddWorkflowDefinitionProvider() // Workflow state exporter. - .AddSingleton() - .AddSingleton() .AddSingleton(WorkflowStateExporter) // Domain handlers. diff --git a/src/samples/aspnet/Elsa.Samples.TelnyxIntegration/Program.cs b/src/samples/aspnet/Elsa.Samples.TelnyxIntegration/Program.cs index eee300b04..024e942f2 100644 --- a/src/samples/aspnet/Elsa.Samples.TelnyxIntegration/Program.cs +++ b/src/samples/aspnet/Elsa.Samples.TelnyxIntegration/Program.cs @@ -63,7 +63,7 @@ services runtime.UseDefaultRuntime(dr => dr.UseEntityFrameworkCore(ef => ef.UseSqlite(sqliteConnectionString))); runtime.UseExecutionLogRecords(d => d.UseEntityFrameworkCore(ef => ef.UseSqlite())); runtime.UseExecutionLogRecords(e => e.UseEntityFrameworkCore(ef => ef.UseSqlite(sqliteConnectionString))); - runtime.WorkflowStateExporter = sp => sp.GetRequiredService(); + runtime.UseAsyncWorkflowStateExporter(); }) .UseActivityDefinitions(feature => feature.UseEntityFrameworkCore(ef => ef.UseSqlite(sqliteConnectionString))) .UseJobs(jobs => jobs.ConfigureOptions = options => options.WorkerCount = 10)