Fix to use EF persistence instead of InMemory fo Webhooks

Fix to use EF persistence instead of InMemory fo Webhooks
This commit is contained in:
axeleron007 2021-06-04 14:11:34 +03:00
parent f6e9c1e87d
commit e391884e5f
13 changed files with 195 additions and 7 deletions

View file

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\..\..\common.props" />
<Import Project="..\..\..\..\configureawait.props" />
@ -18,6 +18,10 @@
<None Remove="Services\**" />
</ItemGroup>
<ItemGroup>
<Compile Remove="Persistence\Decorators\InitializingStoreBase.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>

View file

@ -21,11 +21,17 @@ namespace Elsa.Activities.Webhooks.Extensions
configure?.Invoke(webhookOptionsBuilder);
// Services.
services
.AddScoped<IActivityTypeProvider, WebhookActivityTypeProvider>()
.AddScoped(webhookOptionsBuilder.WebhookOptions.WebhookDefinitionStoreFactory);
.AddScoped(sp =>
{
var x = webhookOptionsBuilder.WebhookOptions.WebhookDefinitionStoreFactory(sp);
return x;
});
services.Decorate<IWebhookDefinitionStore, InitializingWebhookDefinitionStore>();
services.Decorate<IWebhookDefinitionStore, EventPublishingWebhookDefinitionStore>();
return elsaOptions;
}

View file

@ -0,0 +1,95 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Elsa.Persistence.Specifications;
using Elsa.Webhooks.Abstractions.Events;
using Elsa.Webhooks.Abstractions.Models;
using Elsa.Webhooks.Abstractions.Persistence;
using MediatR;
using Open.Linq.AsyncExtensions;
namespace Elsa.Activities.Webhooks.Persistence.Decorators
{
public class EventPublishingWebhookDefinitionStore : IWebhookDefinitionStore
{
private readonly IWebhookDefinitionStore _store;
private readonly IMediator _mediator;
public EventPublishingWebhookDefinitionStore(IWebhookDefinitionStore store, IMediator mediator)
{
_store = store;
_mediator = mediator;
}
public Task<int> CountAsync(ISpecification<WebhookDefinition> specification, CancellationToken cancellationToken = default) => _store.CountAsync(specification, cancellationToken);
public async Task DeleteAsync(WebhookDefinition entity, CancellationToken cancellationToken = default)
{
await _mediator.Publish(new WebhookDefinitionDeleting(entity), cancellationToken);
await _store.DeleteAsync(entity, cancellationToken);
await _mediator.Publish(new WebhookDefinitionDeleted(entity), cancellationToken);
}
public async Task<int> DeleteManyAsync(ISpecification<WebhookDefinition> specification, CancellationToken cancellationToken = default)
{
var webhookDefinitions = await FindManyAsync(specification, cancellationToken: cancellationToken).ToList();
if (!webhookDefinitions.Any())
return 0;
foreach (var webhookDefinition in webhookDefinitions)
await _mediator.Publish(new WebhookDefinitionDeleting(webhookDefinition), cancellationToken);
await _mediator.Publish(new ManyWebhookDefinitionsDeleting(webhookDefinitions), cancellationToken);
var count = await _store.DeleteManyAsync(specification, cancellationToken);
foreach (var instance in webhookDefinitions)
await _mediator.Publish(new WebhookDefinitionDeleted(instance), cancellationToken);
await _mediator.Publish(new ManyWebhookDefinitionsDeleted(webhookDefinitions), cancellationToken);
return count;
}
public Task<WebhookDefinition?> FindAsync(ISpecification<WebhookDefinition> specification, CancellationToken cancellationToken = default) => _store.FindAsync(specification, cancellationToken);
public Task<IEnumerable<WebhookDefinition>> FindManyAsync(ISpecification<WebhookDefinition> specification, IOrderBy<WebhookDefinition>? orderBy = null, IPaging? paging = null, CancellationToken cancellationToken = default)
=> _store.FindManyAsync(specification, orderBy, paging, cancellationToken);
public async Task SaveAsync(WebhookDefinition entity, CancellationToken cancellationToken = default)
{
await _mediator.Publish(new WebhookDefinitionSaving(entity), cancellationToken);
await _store.SaveAsync(entity, cancellationToken);
await _mediator.Publish(new WebhookDefinitionSaved(entity), cancellationToken);
}
public async Task AddAsync(WebhookDefinition entity, CancellationToken cancellationToken = default)
{
await _mediator.Publish(new WebhookDefinitionSaving(entity), cancellationToken);
await _store.AddAsync(entity, cancellationToken);
await _mediator.Publish(new WebhookDefinitionSaved(entity), cancellationToken);
}
public async Task AddManyAsync(IEnumerable<WebhookDefinition> entities, CancellationToken cancellationToken = default)
{
var list = entities.ToList();
foreach (var entity in list)
await _mediator.Publish(new WebhookDefinitionSaving(entity), cancellationToken);
await _store.AddManyAsync(list, cancellationToken);
foreach (var entity in list)
await _mediator.Publish(new WebhookDefinitionSaved(entity), cancellationToken);
}
public async Task UpdateAsync(WebhookDefinition entity, CancellationToken cancellationToken = default)
{
await _mediator.Publish(new WebhookDefinitionSaving(entity), cancellationToken);
await _store.UpdateAsync(entity, cancellationToken);
await _mediator.Publish(new WebhookDefinitionSaved(entity), cancellationToken);
}
}
}

View file

@ -0,0 +1,12 @@
using System.Collections.Generic;
using Elsa.Webhooks.Abstractions.Models;
using MediatR;
namespace Elsa.Webhooks.Abstractions.Events
{
public class ManyWebhookDefinitionsDeleted : INotification
{
public ManyWebhookDefinitionsDeleted(IEnumerable<WebhookDefinition> webhookDefinitions) => WebhookDefinitions = webhookDefinitions;
public IEnumerable<WebhookDefinition> WebhookDefinitions { get; }
}
}

View file

@ -0,0 +1,12 @@
using System.Collections.Generic;
using Elsa.Webhooks.Abstractions.Models;
using MediatR;
namespace Elsa.Webhooks.Abstractions.Events
{
public class ManyWebhookDefinitionsDeleting : INotification
{
public ManyWebhookDefinitionsDeleting(IEnumerable<WebhookDefinition> webhookDefinitions) => WebhookDefinitions = webhookDefinitions;
public IEnumerable<WebhookDefinition> WebhookDefinitions { get; }
}
}

View file

@ -0,0 +1,11 @@
using Elsa.Webhooks.Abstractions.Models;
namespace Elsa.Webhooks.Abstractions.Events
{
public class WebhookDefinitionDeleted : WebhookDefinitionNotification
{
public WebhookDefinitionDeleted(WebhookDefinition webhookDefinition) : base(webhookDefinition)
{
}
}
}

View file

@ -0,0 +1,11 @@
using Elsa.Webhooks.Abstractions.Models;
namespace Elsa.Webhooks.Abstractions.Events
{
public class WebhookDefinitionDeleting : WebhookDefinitionNotification
{
public WebhookDefinitionDeleting(WebhookDefinition webhookDefinition) : base(webhookDefinition)
{
}
}
}

View file

@ -0,0 +1,11 @@
using Elsa.Webhooks.Abstractions.Models;
using MediatR;
namespace Elsa.Webhooks.Abstractions.Events
{
public abstract class WebhookDefinitionNotification : INotification
{
public WebhookDefinitionNotification(WebhookDefinition webhookDefinition) => WebhookDefinition = webhookDefinition;
public WebhookDefinition WebhookDefinition { get; }
}
}

View file

@ -0,0 +1,11 @@
using Elsa.Webhooks.Abstractions.Models;
namespace Elsa.Webhooks.Abstractions.Events
{
public class WebhookDefinitionSaved : WebhookDefinitionNotification
{
public WebhookDefinitionSaved(WebhookDefinition webhookDefinition) : base(webhookDefinition)
{
}
}
}

View file

@ -0,0 +1,11 @@
using Elsa.Webhooks.Abstractions.Models;
namespace Elsa.Webhooks.Abstractions.Events
{
public class WebhookDefinitionSaving : WebhookDefinitionNotification
{
public WebhookDefinitionSaving(WebhookDefinition webhookDefinition) : base(webhookDefinition)
{
}
}
}

View file

@ -8,7 +8,6 @@ namespace Elsa.Webhooks.Persistence.EntityFramework.Core.Configuration
{
public void Configure(EntityTypeBuilder<WebhookDefinition> builder)
{
builder.Property<string>("Data");
builder.HasIndex(x => x.TenantId).HasDatabaseName($"IX_{nameof(WebhookDefinition)}_{nameof(WebhookDefinition.TenantId)}");
builder.HasIndex(x => x.Path).HasDatabaseName($"IX_{nameof(WebhookDefinition)}_{nameof(WebhookDefinition.Path)}");
builder.HasIndex(x => x.Name).HasDatabaseName($"IX_{nameof(WebhookDefinition)}_{nameof(WebhookDefinition.Name)}");

View file

@ -220,9 +220,11 @@ namespace Elsa.Webhooks.Persistence.EntityFramework.Core.Extensions
if (autoRunMigrations)
webhookOptions.Services.AddStartupTask<RunMigrations>();
var webhookOptionsBuilder = new WebhookOptionsBuilder(webhookOptions.Services);
webhookOptionsBuilder.UseWebhookDefinitionStore(sp => sp.GetRequiredService<EntityFrameworkWebhookDefinitionStore>());
webhookOptions.UseWebhookDefinitionStore(sp =>
{
var x = sp.GetRequiredService<EntityFrameworkWebhookDefinitionStore>();
return x;
});
return webhookOptions;
}

View file

@ -63,7 +63,10 @@ namespace Elsa.Samples.Server.Host
.AddConductorActivities(options => elsaSection.GetSection("Conductor").Bind(options))
.AddActivitiesFrom<Startup>()
.AddWorkflowsFrom<Startup>()
.AddWebhooks(webhooks => webhooks.UseEntityFrameworkPersistence(ef => ef.UseWebhookSqlite()))
.AddWebhooks(webhooks =>
{
webhooks.UseEntityFrameworkPersistence(ef => ef.UseWebhookSqlite());
})
);
// Elsa API endpoints.