Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Elastic updates #3623

Merged
merged 11 commits into from
Jan 13, 2023
Prev Previous commit
Next Next commit
Remove rollover hosted service and expose index configurations
  • Loading branch information
sfmskywalker committed Jan 13, 2023
commit 2af3d892b6df477ecbf9fd32ecdad7b55e46f8b9
10 changes: 2 additions & 8 deletions src/modules/Elsa.Elasticsearch/Common/ElasticStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,7 @@ public async Task<long> DeleteManyAsync(IEnumerable<T> documents, CancellationTo
var response = await _elasticClient.BulkAsync(b => b.DeleteMany(documents), cancellationToken);

if (!response.IsSuccess())
{
_logger.LogError("Failed to bulk delete data in Elasticsearch: {Message}", response.ElasticsearchServerError.ToString());
return 0;
}
throw new Exception(response.DebugInformation);

return response.Items.Count(i => i.IsValid);
}
Expand All @@ -90,10 +87,7 @@ public async Task<long> DeleteByQueryAsync(Action<DeleteByQueryRequestDescriptor
var response = await _elasticClient.DeleteByQueryAsync(Indices.All, query, cancellationToken);

if (!response.IsSuccess())
{
_logger.LogError("Failed to delete data in Elasticsearch: {Message}", response.ElasticsearchServerError.ToString());
return 0;
}
throw new Exception(response.DebugInformation);

return response.Deleted ?? 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Elsa.Elasticsearch.Common;
/// <summary>
/// A convenience base class for document type configurations.
/// </summary>
public abstract class ElasticConfiguration<T> : IElasticConfiguration
public abstract class IndexConfiguration<T> : IIndexConfiguration<T>
{
/// <inheritdoc />
public Type DocumentType => typeof(T);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Elsa.Elasticsearch.Services;
using Elsa.Features.Abstractions;
using Elsa.Features.Services;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -25,4 +26,9 @@ protected void AddStore<TModel, TStore>() where TModel : class where TStore : cl
.AddSingleton<ElasticStore<TModel>>()
.AddSingleton<TStore>();
}

/// <summary>
/// Registers an <see cref="IIndexConfiguration"/>.
/// </summary>
protected void AddIndexConfiguration<TDocument>(Func<IServiceProvider, IIndexConfiguration<TDocument>> configuration) => Services.AddSingleton<IIndexConfiguration>(configuration);
}
14 changes: 3 additions & 11 deletions src/modules/Elsa.Elasticsearch/Features/ElasticsearchFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
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;
Expand All @@ -28,34 +30,24 @@ public ElasticsearchFeature(IModule module) : base(module)
/// </summary>
public Action<ElasticsearchOptions> Options { get; set; } = _ => { };

/// <summary>
/// True to enable index name rollovers, false otherwise.
/// </summary>
public bool EnableRollover { get; set; }

/// <inheritdoc />
public override void ConfigureHostedServices()
{
Module.ConfigureHostedService<ConfigureElasticsearchClientHostedService>(-2);
sfmskywalker marked this conversation as resolved.
Show resolved Hide resolved

if (EnableRollover)
Module.ConfigureHostedService<ConfigureIndexRolloverHostedService>(-1);
}

/// <inheritdoc />
public override void Apply()
{
Services.Configure(Options);
Services.AddSingleton(sp => new ElasticsearchClient(GetSettings(sp)));
Services.AddSingleton<IElasticConfiguration, ExecutionLogConfiguration>();
Services.AddSingleton<IElasticConfiguration, WorkflowInstanceConfiguration>();
}

private static ElasticsearchClientSettings GetSettings(IServiceProvider serviceProvider)
{
var options = serviceProvider.GetRequiredService<IOptions<ElasticsearchOptions>>().Value;
var configuration = serviceProvider.GetRequiredService<IConfiguration>();
var configs = serviceProvider.GetServices<IElasticConfiguration>();
var configs = serviceProvider.GetServices<IIndexConfiguration>();
var url = configuration.GetConnectionString(options.Endpoint) ?? options.Endpoint;
var settings = new ElasticsearchClientSettings(new Uri(url)).ConfigureAuthentication(options);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ public class ConfigureElasticsearchClientHostedService : IHostedService
{
private readonly ElasticsearchClient _client;
private readonly ElasticsearchOptions _options;
private readonly IEnumerable<IElasticConfiguration> _configurations;
private readonly IEnumerable<IIndexConfiguration> _configurations;

/// <summary>
/// Constructor.
/// </summary>
public ConfigureElasticsearchClientHostedService(ElasticsearchClient client, IOptions<ElasticsearchOptions> options, IEnumerable<IElasticConfiguration> configurations)
public ConfigureElasticsearchClientHostedService(ElasticsearchClient client, IOptions<ElasticsearchOptions> options, IEnumerable<IIndexConfiguration> configurations)
{
_client = client;
_options = options.Value;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Elsa.Elasticsearch.Common;
using Elsa.Elasticsearch.Features;
using Elsa.Elasticsearch.Services;
using Elsa.Features.Attributes;
using Elsa.Features.Services;
using Elsa.Workflows.Management.Entities;
Expand All @@ -19,6 +20,11 @@ public class ElasticWorkflowInstanceFeature : ElasticPersistenceFeatureBase
public ElasticWorkflowInstanceFeature(IModule module) : base(module)
{
}

/// <summary>
/// A delegate that creates an instance of a concrete implementation if <see cref="IIndexConfiguration"/> for <see cref="WorkflowInstance"/>.
/// </summary>
public Func<IServiceProvider, IIndexConfiguration<WorkflowInstance>> IndexConfiguration { get; set; } = sp => ActivatorUtilities.CreateInstance<WorkflowInstanceConfiguration>(sp);

/// <inheritdoc />
public override void Configure()
Expand All @@ -33,7 +39,8 @@ public override void Configure()
public override void Apply()
{
base.Apply();

AddStore<WorkflowInstance, ElasticWorkflowInstanceStore>();
AddIndexConfiguration(IndexConfiguration);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Elsa.Elasticsearch.Modules.Management;
/// <summary>
/// Configures Elasticsearch with mappings for <see cref="WorkflowInstance"/>.
/// </summary>
public class WorkflowInstanceConfiguration : ElasticConfiguration<WorkflowInstance>
public class WorkflowInstanceConfiguration : IndexConfiguration<WorkflowInstance>
{
private readonly ElasticsearchOptions _options;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Elsa.Workflows.Runtime.Features;
using Elsa.Elasticsearch.Common;
using Elsa.Elasticsearch.Features;
using Elsa.Elasticsearch.Services;
using Microsoft.Extensions.DependencyInjection;

namespace Elsa.Elasticsearch.Modules.Runtime;
Expand All @@ -19,6 +20,11 @@ public class ElasticExecutionLogRecordFeature : ElasticPersistenceFeatureBase
public ElasticExecutionLogRecordFeature(IModule module) : base(module)
{
}

/// <summary>
/// A delegate that creates an instance of a concrete implementation if <see cref="IIndexConfiguration"/> for <see cref="WorkflowExecutionLogRecord"/>.
/// </summary>
public Func<IServiceProvider, IIndexConfiguration<WorkflowExecutionLogRecord>> IndexConfiguration { get; set; } = sp => ActivatorUtilities.CreateInstance<ExecutionLogConfiguration>(sp);

/// <inheritdoc />
public override void Configure()
Expand All @@ -35,5 +41,6 @@ public override void Apply()
base.Apply();

AddStore<WorkflowExecutionLogRecord, ElasticWorkflowExecutionLogStore>();
AddIndexConfiguration(IndexConfiguration);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Elsa.Elasticsearch.Modules.Runtime;
/// <summary>
/// Configures Elasticsearch with mappings for <see cref="WorkflowExecutionLogRecord"/>.
/// </summary>
public class ExecutionLogConfiguration : ElasticConfiguration<WorkflowExecutionLogRecord>
public class ExecutionLogConfiguration : IndexConfiguration<WorkflowExecutionLogRecord>
{
private readonly ElasticsearchOptions _options;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Elsa.Elasticsearch.Services;
/// <summary>
/// Implement this interface to get a chance to configure some aspect of Elasticsearch, such as index mappings.
/// </summary>
public interface IElasticConfiguration
public interface IIndexConfiguration
{
/// <summary>
/// The document type to configure.
Expand All @@ -29,4 +29,11 @@ public interface IElasticConfiguration
/// <param name="client">The <see cref="ElasticsearchClient"/></param> to configure.
/// <param name="cancellationToken">A cancellation token.</param>
ValueTask ConfigureClientAsync(ElasticsearchClient client, CancellationToken cancellationToken);
}

/// <summary>
/// Implement this interface to get a chance to configure some aspect of Elasticsearch, such as index mappings.
/// </summary>
public interface IIndexConfiguration<TDocument> : IIndexConfiguration
{
}