forked from elsa-workflows/elsa-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
233 additions
and
158 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/modules/Elsa.Elasticsearch/Common/PersistanceFeatureBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using Elasticsearch.Net; | ||
using Elsa.Elasticsearch.Extensions; | ||
using Elsa.Elasticsearch.HostedServices; | ||
using Elsa.Elasticsearch.Models; | ||
using Elsa.Elasticsearch.Options; | ||
using Elsa.Elasticsearch.Scheduling; | ||
using Elsa.Elasticsearch.Services; | ||
using Elsa.Features.Abstractions; | ||
using Elsa.Features.Services; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Nest; | ||
|
||
namespace Elsa.Elasticsearch.Common; | ||
|
||
public abstract class ElasticPersistanceFeatureBase : FeatureBase | ||
{ | ||
public ElasticPersistanceFeatureBase(IModule module) : base(module) | ||
{ | ||
} | ||
|
||
protected void AddStore<TModel, TStore>() where TModel : class where TStore : class | ||
{ | ||
Services | ||
.AddSingleton<ElasticStore<TModel>>() | ||
.AddSingleton<TStore>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/modules/Elsa.Elasticsearch/Extensions/ModuleExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using Elsa.Elasticsearch.Common; | ||
using Elsa.Elasticsearch.Features; | ||
using Elsa.Elasticsearch.Models; | ||
using Elsa.Elasticsearch.Options; | ||
using Elsa.Features.Services; | ||
|
||
namespace Elsa.Elasticsearch.Extensions; | ||
|
||
public static class ModuleExtensions | ||
{ | ||
/// <summary> | ||
/// Enables the <see cref="ElasticsearchFeature"/> feature. | ||
/// </summary> | ||
public static IModule UseElasticsearch( | ||
this IModule module, | ||
ElasticsearchOptions options, | ||
IndexRolloverStrategy? rolloverStrategy = default, | ||
IDictionary<string,string>? indexConfig = default, | ||
Action<ElasticsearchFeature>? configure = default) | ||
{ | ||
configure += f => | ||
{ | ||
f.Options = options; | ||
f.IndexRolloverStrategy = rolloverStrategy; | ||
f.IndexConfig = Utils.ResolveAliasConfig(f.IndexConfig, options.IndexConfig, indexConfig); | ||
}; | ||
|
||
module.Configure(configure); | ||
return module; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 0 additions & 51 deletions
51
src/modules/Elsa.Elasticsearch/HostedServices/ConfigureElasticsearchHostedService.cs
This file was deleted.
Oops, something went wrong.
31 changes: 31 additions & 0 deletions
31
src/modules/Elsa.Elasticsearch/HostedServices/ConfigureIndexRolloverHostedService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using Elsa.Elasticsearch.Scheduling; | ||
using Elsa.Jobs.Schedules; | ||
using Elsa.Jobs.Services; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
namespace Elsa.Elasticsearch.HostedServices; | ||
|
||
public class ConfigureIndexRolloverHostedService : IHostedService | ||
{ | ||
private readonly IJobScheduler _jobScheduler; | ||
|
||
public ConfigureIndexRolloverHostedService(IJobScheduler jobScheduler) | ||
{ | ||
_jobScheduler = jobScheduler; | ||
} | ||
|
||
public async Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
var job = new ConfigureIndexRolloverJob(); | ||
var schedule = new CronSchedule | ||
{ | ||
// At the beginning of every month | ||
//CronExpression = "0 0 1 * *" | ||
CronExpression = "*/5 * * * *" | ||
}; | ||
|
||
await _jobScheduler.ScheduleAsync(job, GetType().Name, schedule, cancellationToken: cancellationToken); | ||
} | ||
|
||
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; | ||
} |
27 changes: 27 additions & 0 deletions
27
src/modules/Elsa.Elasticsearch/HostedServices/ConfigureMappingHostedService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using Elsa.Workflows.Management.Entities; | ||
using Microsoft.Extensions.Hosting; | ||
using Nest; | ||
|
||
namespace Elsa.Elasticsearch.HostedServices; | ||
|
||
public class ConfigureMappingHostedService : IHostedService | ||
{ | ||
private readonly ElasticClient _elasticClient; | ||
|
||
public ConfigureMappingHostedService(ElasticClient elasticClient) | ||
{ | ||
_elasticClient = elasticClient; | ||
} | ||
|
||
public async Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
await _elasticClient.Indices.PutMappingAsync<WorkflowInstance>( | ||
descriptor => descriptor | ||
.Properties(p => p | ||
.Flattened(d => d | ||
.Name(p => p.WorkflowState.Properties))), | ||
cancellationToken); | ||
} | ||
|
||
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; | ||
} |
35 changes: 35 additions & 0 deletions
35
...odules/Elsa.Elasticsearch/Implementations/IndexNamingStrategies/NamingWithYearAndMonth.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using Elsa.Elasticsearch.Common; | ||
using Elsa.Elasticsearch.Services; | ||
using Nest; | ||
|
||
namespace Elsa.Elasticsearch.Implementations.IndexNamingStrategies; | ||
|
||
public class NamingWithYearAndMonth : IIndexNamingStrategy | ||
{ | ||
private readonly ElasticClient _client; | ||
|
||
public NamingWithYearAndMonth(ElasticClient client) | ||
{ | ||
_client = client; | ||
} | ||
|
||
public void Apply(IEnumerable<Type> typesToConfigure, IDictionary<Type, string> aliasConfig) | ||
{ | ||
foreach (var type in typesToConfigure) | ||
{ | ||
var aliasName = aliasConfig[type]; | ||
var indexName = Utils.GenerateIndexName(aliasName); | ||
|
||
var indexExists = _client.Indices.Exists(indexName).Exists; | ||
|
||
if (indexExists) continue; | ||
|
||
var response = _client.Indices.Create(indexName, s => s | ||
.Aliases(a => a.Alias(aliasName)) | ||
.Map(m => m.AutoMap(type))); | ||
|
||
if (response.IsValid) continue; | ||
throw response.OriginalException; | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/modules/Elsa.Elasticsearch/Models/IndexNamingStrategy.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Elsa.Elasticsearch.Implementations.IndexNamingStrategies; | ||
|
||
namespace Elsa.Elasticsearch.Models; | ||
|
||
public class IndexNamingStrategy | ||
{ | ||
private IndexNamingStrategy(Type value) { Value = value; } | ||
|
||
public Type Value { get; private set; } | ||
|
||
public static IndexNamingStrategy NamingWithYearAndMonth => new (typeof(NamingWithYearAndMonth)); | ||
|
||
public override string ToString() | ||
{ | ||
return Value.Name; | ||
} | ||
} |
17 changes: 10 additions & 7 deletions
17
src/modules/Elsa.Elasticsearch/Models/IndexRolloverStrategy.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,20 @@ | ||
using System.Reflection.Emit; | ||
using Elsa.Elasticsearch.Implementations.IndexNamingStrategies; | ||
using Elsa.Elasticsearch.Implementations.RolloverStrategies; | ||
using Elsa.Elasticsearch.Services; | ||
|
||
namespace Elsa.Elasticsearch.Models; | ||
|
||
public class IndexRolloverStrategy | ||
{ | ||
private IndexRolloverStrategy(Type value) { Value = value; } | ||
private IndexRolloverStrategy(Type value, Type indexNamingStrategy) | ||
{ | ||
Value = value; | ||
IndexNamingStrategy = indexNamingStrategy; | ||
} | ||
|
||
public Type Value { get; private set; } | ||
public Type IndexNamingStrategy { get; private set; } | ||
|
||
public static IndexRolloverStrategy RolloverOnMonthlyBasis => new (typeof(RolloverOnMonthlyBasis)); | ||
|
||
public override string ToString() | ||
{ | ||
return Value.Name; | ||
} | ||
public static IndexRolloverStrategy RolloverOnMonthlyBasis => new (typeof(RolloverOnMonthlyBasis),typeof(NamingWithYearAndMonth)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.