From 6feb8f4a7785d210819513f56d758cc9b35d88bf Mon Sep 17 00:00:00 2001 From: axeleron007 Date: Fri, 2 Jul 2021 14:37:45 +0300 Subject: [PATCH] YesSQL persistence for Webhooks YesSQL persistence for Webhooks --- .../Documents/WebhookDefinitionDocument.cs | 2 +- .../Elsa.Webhooks.Persistence.YesSql.csproj | 2 +- .../WebhookServiceCollectionExtensions.cs | 29 ++++++++++++------- .../Indexes/WebhookDefinitionIndex.cs | 4 +-- .../Mapping/AutoMapperProfile.cs | 3 +- .../Migrations.cs | 2 +- .../Services/DatabaseInitializer.cs | 25 ++++++++++++++++ .../Stores/YesSqlWebhookDefinitionStore.cs | 5 ++-- .../Stores/YesSqlStore.cs | 4 +-- 9 files changed, 55 insertions(+), 21 deletions(-) create mode 100644 src/activities/webhooks/Elsa.Webhooks.Persistence.YesSql/Services/DatabaseInitializer.cs diff --git a/src/activities/webhooks/Elsa.Webhooks.Persistence.YesSql/Documents/WebhookDefinitionDocument.cs b/src/activities/webhooks/Elsa.Webhooks.Persistence.YesSql/Documents/WebhookDefinitionDocument.cs index 3f51036c7..e41525192 100644 --- a/src/activities/webhooks/Elsa.Webhooks.Persistence.YesSql/Documents/WebhookDefinitionDocument.cs +++ b/src/activities/webhooks/Elsa.Webhooks.Persistence.YesSql/Documents/WebhookDefinitionDocument.cs @@ -4,7 +4,7 @@ namespace Elsa.Webhooks.Persistence.YesSql.Documents { public class WebhookDefinitionDocument : YesSqlDocument { - public string WebhookDefinitionId { get; set; } = default!; + public string WebhookId { get; set; } = default!; public string? TenantId { get; set; } public string Name { get; set; } = default!; public string Path { get; set; } = default!; diff --git a/src/activities/webhooks/Elsa.Webhooks.Persistence.YesSql/Elsa.Webhooks.Persistence.YesSql.csproj b/src/activities/webhooks/Elsa.Webhooks.Persistence.YesSql/Elsa.Webhooks.Persistence.YesSql.csproj index 276cdc71e..51288f19c 100644 --- a/src/activities/webhooks/Elsa.Webhooks.Persistence.YesSql/Elsa.Webhooks.Persistence.YesSql.csproj +++ b/src/activities/webhooks/Elsa.Webhooks.Persistence.YesSql/Elsa.Webhooks.Persistence.YesSql.csproj @@ -1,4 +1,4 @@ - + diff --git a/src/activities/webhooks/Elsa.Webhooks.Persistence.YesSql/Extensions/WebhookServiceCollectionExtensions.cs b/src/activities/webhooks/Elsa.Webhooks.Persistence.YesSql/Extensions/WebhookServiceCollectionExtensions.cs index 6c8b27ebb..b5ef98cc6 100644 --- a/src/activities/webhooks/Elsa.Webhooks.Persistence.YesSql/Extensions/WebhookServiceCollectionExtensions.cs +++ b/src/activities/webhooks/Elsa.Webhooks.Persistence.YesSql/Extensions/WebhookServiceCollectionExtensions.cs @@ -1,10 +1,11 @@ using System; using System.Data; using Elsa.Activities.Webhooks; +using Elsa.Webhooks.Persistence.YesSql.Services; using Elsa.Persistence.YesSql; -using Elsa.Persistence.YesSql.Data; -using Elsa.Persistence.YesSql.Mapping; -using Elsa.Persistence.YesSql.Services; +//using Elsa.Persistence.YesSql.Data; +//using Elsa.Persistence.YesSql.Mapping; +//using Elsa.Persistence.YesSql.Services; using Elsa.Runtime; using Elsa.Webhooks.Persistence.YesSql.Indexes; using Elsa.Webhooks.Persistence.YesSql.Stores; @@ -12,6 +13,8 @@ using Microsoft.Extensions.DependencyInjection; using YesSql; using YesSql.Indexes; using YesSql.Provider.Sqlite; +using Elsa.Persistence.YesSql.Data; +using Elsa.Webhooks.Persistence.YesSql.Mapping; namespace Elsa.Webhooks.Persistence.YesSql.Extensions { @@ -25,22 +28,28 @@ namespace Elsa.Webhooks.Persistence.YesSql.Extensions webhookOptions.Services .AddScoped() .AddSingleton(sp => CreateStore(sp, configure)) - .AddSingleton() - .AddScoped(CreateSession) - .AddScoped() + //.AddSingleton() + //.AddScoped(CreateSession) + //.AddScoped() .AddStartupTask() - .AddStartupTask() + //.AddStartupTask() .AddDataMigration() .AddAutoMapperProfile() .AddIndexProvider(); - var webhookOptionsBuilder = new WebhookOptionsBuilder(webhookOptions.Services); + //var webhookOptionsBuilder = new WebhookOptionsBuilder(webhookOptions.Services); - webhookOptionsBuilder.UseWebhookDefinitionStore(sp => sp.GetRequiredService()); + + webhookOptions.UseWebhookDefinitionStore(sp => sp.GetRequiredService()); return webhookOptions; } + public static IServiceCollection AddIndexProvider(this IServiceCollection services) where T : class, IIndexProvider => services.AddSingleton(); + public static IServiceCollection AddScopedIndexProvider(this IServiceCollection services) where T : class, IIndexProvider => services.AddScoped(); + + public static IServiceCollection AddDataMigration(this IServiceCollection services) where T : class, IDataMigration => services.AddScoped(); + private static IStore CreateStore( IServiceProvider serviceProvider, Action configure) @@ -64,7 +73,7 @@ namespace Elsa.Webhooks.Persistence.YesSql.Extensions private static ISession CreateSession(IServiceProvider serviceProvider) { - var provider = serviceProvider.GetRequiredService(); + var provider = serviceProvider.GetRequiredService(); return provider.CreateSession(); } } diff --git a/src/activities/webhooks/Elsa.Webhooks.Persistence.YesSql/Indexes/WebhookDefinitionIndex.cs b/src/activities/webhooks/Elsa.Webhooks.Persistence.YesSql/Indexes/WebhookDefinitionIndex.cs index 3c9ae4194..e1f7bb7bd 100644 --- a/src/activities/webhooks/Elsa.Webhooks.Persistence.YesSql/Indexes/WebhookDefinitionIndex.cs +++ b/src/activities/webhooks/Elsa.Webhooks.Persistence.YesSql/Indexes/WebhookDefinitionIndex.cs @@ -6,7 +6,7 @@ namespace Elsa.Webhooks.Persistence.YesSql.Indexes { public class WebhookDefinitionIndex : MapIndex { - public string WebhookDefinitionId { get; set; } = default!; + public string WebhookId { get; set; } = default!; public string? TenantId { get; set; } public bool IsEnabled { get; set; } } @@ -21,7 +21,7 @@ namespace Elsa.Webhooks.Persistence.YesSql.Indexes .Map( webhookDefinition => new WebhookDefinitionIndex { - WebhookDefinitionId = webhookDefinition.WebhookDefinitionId, + WebhookId = webhookDefinition.WebhookId, TenantId = webhookDefinition.TenantId, IsEnabled = webhookDefinition.IsEnabled } diff --git a/src/activities/webhooks/Elsa.Webhooks.Persistence.YesSql/Mapping/AutoMapperProfile.cs b/src/activities/webhooks/Elsa.Webhooks.Persistence.YesSql/Mapping/AutoMapperProfile.cs index 86f3b1dad..b5eaa8c7f 100644 --- a/src/activities/webhooks/Elsa.Webhooks.Persistence.YesSql/Mapping/AutoMapperProfile.cs +++ b/src/activities/webhooks/Elsa.Webhooks.Persistence.YesSql/Mapping/AutoMapperProfile.cs @@ -9,9 +9,10 @@ namespace Elsa.Webhooks.Persistence.YesSql.Mapping public AutoMapperProfile() { CreateMap() + .ForMember(d => d.WebhookId, d => d.MapFrom(s => s.Id)) .ForMember(d => d.Id, d => d.Ignore()) .ReverseMap() - .ForMember(d => d.Id, d => d.MapFrom(s => s.Id)); + .ForMember(d => d.Id, d => d.MapFrom(s => s.WebhookId)); } } } diff --git a/src/activities/webhooks/Elsa.Webhooks.Persistence.YesSql/Migrations.cs b/src/activities/webhooks/Elsa.Webhooks.Persistence.YesSql/Migrations.cs index 7a76e8f6c..6048b50dc 100644 --- a/src/activities/webhooks/Elsa.Webhooks.Persistence.YesSql/Migrations.cs +++ b/src/activities/webhooks/Elsa.Webhooks.Persistence.YesSql/Migrations.cs @@ -11,7 +11,7 @@ namespace Elsa.Webhooks.Persistence.YesSql { SchemaBuilder.CreateMapIndexTable( table => table - .Column(nameof(WebhookDefinitionIndex.WebhookDefinitionId)) + .Column(nameof(WebhookDefinitionIndex.WebhookId)) .Column(nameof(WebhookDefinitionIndex.TenantId)) .Column(nameof(WebhookDefinitionIndex.IsEnabled)), CollectionNames.WebhookDefinitions); diff --git a/src/activities/webhooks/Elsa.Webhooks.Persistence.YesSql/Services/DatabaseInitializer.cs b/src/activities/webhooks/Elsa.Webhooks.Persistence.YesSql/Services/DatabaseInitializer.cs new file mode 100644 index 000000000..cc9a3e3b2 --- /dev/null +++ b/src/activities/webhooks/Elsa.Webhooks.Persistence.YesSql/Services/DatabaseInitializer.cs @@ -0,0 +1,25 @@ +using System.Threading; +using System.Threading.Tasks; +using Elsa.Services; +using Elsa.Webhooks.Persistence.YesSql.Data; +using YesSql; + +namespace Elsa.Webhooks.Persistence.YesSql.Services +{ + public class DatabaseInitializer : IStartupTask + { + private readonly IStore _store; + + public DatabaseInitializer(IStore store) + { + _store = store; + } + + public int Order => 0; + + public async Task ExecuteAsync(CancellationToken cancellationToken = default) + { + await _store.InitializeCollectionAsync(CollectionNames.WebhookDefinitions); + } + } +} diff --git a/src/activities/webhooks/Elsa.Webhooks.Persistence.YesSql/Stores/YesSqlWebhookDefinitionStore.cs b/src/activities/webhooks/Elsa.Webhooks.Persistence.YesSql/Stores/YesSqlWebhookDefinitionStore.cs index 497977e51..4156ee52f 100644 --- a/src/activities/webhooks/Elsa.Webhooks.Persistence.YesSql/Stores/YesSqlWebhookDefinitionStore.cs +++ b/src/activities/webhooks/Elsa.Webhooks.Persistence.YesSql/Stores/YesSqlWebhookDefinitionStore.cs @@ -1,6 +1,5 @@ using System.Threading; using System.Threading.Tasks; -using Elsa.Models; using Elsa.Webhooks.Persistence.YesSql.Data; using Elsa.Webhooks.Persistence.YesSql.Documents; using Elsa.Webhooks.Persistence.YesSql.Indexes; @@ -22,13 +21,13 @@ namespace Elsa.Webhooks.Persistence.YesSql.Stores { } - protected override async Task FindDocumentAsync(ISession session, WebhookDefinition entity, CancellationToken cancellationToken) => await Query(session, x => x.WebhookDefinitionId == entity.Id).FirstOrDefaultAsync(); + protected override async Task FindDocumentAsync(ISession session, WebhookDefinition entity, CancellationToken cancellationToken) => await Query(session, x => x.WebhookId == entity.Id).FirstOrDefaultAsync(); protected override IQuery MapSpecification(ISession session, ISpecification specification) { return specification switch { - EntityIdSpecification s => Query(session, x => x.WebhookDefinitionId == s.Id), + EntityIdSpecification s => Query(session, x => x.WebhookId == s.Id), _ => AutoMapSpecification(session, specification) }; } diff --git a/src/persistence/Elsa.Persistence.YesSql/Stores/YesSqlStore.cs b/src/persistence/Elsa.Persistence.YesSql/Stores/YesSqlStore.cs index 6115ef924..36d91b078 100644 --- a/src/persistence/Elsa.Persistence.YesSql/Stores/YesSqlStore.cs +++ b/src/persistence/Elsa.Persistence.YesSql/Stores/YesSqlStore.cs @@ -41,14 +41,14 @@ namespace Elsa.Persistence.YesSql.Stores { await _semaphore.WaitAsync(cancellationToken); - try + try { await using var session = SessionProvider.CreateSession(); var existingDocument = await FindDocumentAsync(session, entity, cancellationToken); var document = Mapper.Map(entity, existingDocument); session.Save(document, CollectionName); await session.SaveChangesAsync(); - } + } finally { _semaphore.Release();