diff --git a/src/activities/webhooks/Elsa.Activities.Webhooks/Extensions/WebhookOptionsBuilderExtensions.cs b/src/activities/webhooks/Elsa.Activities.Webhooks/Extensions/WebhookOptionsBuilderExtensions.cs index cdb5e79f5..d3776741e 100644 --- a/src/activities/webhooks/Elsa.Activities.Webhooks/Extensions/WebhookOptionsBuilderExtensions.cs +++ b/src/activities/webhooks/Elsa.Activities.Webhooks/Extensions/WebhookOptionsBuilderExtensions.cs @@ -1,5 +1,6 @@ using Elsa.Activities.Webhooks.ActivityTypes; using Elsa.Activities.Webhooks.Bookmarks; +using Elsa.Activities.Webhooks.Handlers; using Elsa.Services; using Microsoft.Extensions.DependencyInjection; @@ -11,7 +12,8 @@ namespace Elsa.Activities.Webhooks.Extensions { elsaOptions.Services .AddScoped() - .AddBookmarkProvider(); + .AddBookmarkProvider() + .AddNotificationHandlersFrom(); return elsaOptions; } diff --git a/src/activities/webhooks/Elsa.Activities.Webhooks/Handlers/EvictWorkflowRegistryCacheHandler.cs b/src/activities/webhooks/Elsa.Activities.Webhooks/Handlers/EvictWorkflowRegistryCacheHandler.cs new file mode 100644 index 000000000..b426cdf65 --- /dev/null +++ b/src/activities/webhooks/Elsa.Activities.Webhooks/Handlers/EvictWorkflowRegistryCacheHandler.cs @@ -0,0 +1,24 @@ +using System.Threading; +using System.Threading.Tasks; +using Elsa.Caching; +using Elsa.Services.Workflows; +using Elsa.Webhooks.Events; +using MediatR; + +namespace Elsa.Activities.Webhooks.Handlers +{ + public class EvictWorkflowRegistryCacheHandler : INotificationHandler + { + private readonly ICacheSignal _cacheSignal; + + public EvictWorkflowRegistryCacheHandler(ICacheSignal cacheSignal) + { + _cacheSignal = cacheSignal; + } + + public async Task Handle(WebhookDefinitionSaved notification, CancellationToken cancellationToken) + { + await _cacheSignal.TriggerTokenAsync(ActivityTypeService.CacheKey); + } + } +} \ No newline at end of file diff --git a/src/activities/webhooks/Elsa.Activities.Webhooks/Services/WebhookPublisher.cs b/src/activities/webhooks/Elsa.Activities.Webhooks/Services/WebhookPublisher.cs deleted file mode 100644 index 6e3fbd9e2..000000000 --- a/src/activities/webhooks/Elsa.Activities.Webhooks/Services/WebhookPublisher.cs +++ /dev/null @@ -1,76 +0,0 @@ -// using System.Threading; -// using System.Threading.Tasks; -// using Elsa.Persistence.Specifications; -// using Elsa.Services; -// using Elsa.Webhooks.Abstractions.Models; -// using Elsa.Webhooks.Abstractions.Persistence; -// using Elsa.Webhooks.Abstractions.Services; -// using MediatR; -// -// namespace Elsa.Activities.Webhooks.Services -// { -// public class WebhookPublisher : IWebhookPublisher -// { -// private readonly IWebhookDefinitionStore _webhookDefinitionStore; -// private readonly IIdGenerator _idGenerator; -// private readonly ICloner _cloner; -// private readonly IMediator _mediator; -// -// public WebhookPublisher(IWebhookDefinitionStore webhookDefinitionStore, IIdGenerator idGenerator, ICloner cloner, IMediator mediator) -// { -// _webhookDefinitionStore = webhookDefinitionStore; -// _idGenerator = idGenerator; -// _cloner = cloner; -// _mediator = mediator; -// } -// -// public WebhookDefinition New() -// { -// var definition = new WebhookDefinition -// { -// Id = _idGenerator.Generate(), -// Name = "New Webhook" -// }; -// -// return definition; -// } -// -// public async Task SaveAsync(WebhookDefinition webhookDefinition, CancellationToken cancellationToken = default) -// { -// var webhook = webhookDefinition; -// webhook = Initialize(webhook); -// -// await _webhookDefinitionStore.SaveAsync(webhook, cancellationToken); -// return webhook; -// } -// -// public async Task UpdateAsync(WebhookDefinition webhookDefinition, CancellationToken cancellationToken = default) -// { -// var webhook = webhookDefinition; -// webhook = Initialize(webhook); -// -// await _webhookDefinitionStore.UpdateAsync(webhook, cancellationToken); -// return webhook; -// } -// -// public async Task DeleteAsync(string webhookId, CancellationToken cancellationToken = default) -// { -// var webhookDefinition = New(); -// if (!string.IsNullOrWhiteSpace(webhookId)) -// { -// webhookDefinition = await _webhookDefinitionStore.FindAsync(new EntityIdSpecification(webhookId), cancellationToken); -// } -// await _webhookDefinitionStore.DeleteAsync(webhookDefinition, cancellationToken); -// } -// -// public Task DeleteAsync(WebhookDefinition webhookDefinition, CancellationToken cancellationToken = default) => DeleteAsync(webhookDefinition, cancellationToken); -// -// private WebhookDefinition Initialize(WebhookDefinition webhookDefinition) -// { -// if (webhookDefinition.Id == null!) -// webhookDefinition.Id = _idGenerator.Generate(); -// -// return webhookDefinition; -// } -// } -// } \ No newline at end of file diff --git a/src/activities/webhooks/Elsa.Webhooks.Abstractions/Services/IWebhookPublisher.cs b/src/activities/webhooks/Elsa.Webhooks.Abstractions/Services/IWebhookPublisher.cs deleted file mode 100644 index d21f43623..000000000 --- a/src/activities/webhooks/Elsa.Webhooks.Abstractions/Services/IWebhookPublisher.cs +++ /dev/null @@ -1,15 +0,0 @@ -// using System.Threading; -// using System.Threading.Tasks; -// using Elsa.Webhooks.Abstractions.Models; -// -// namespace Elsa.Webhooks.Abstractions.Services -// { -// public interface IWebhookPublisher -// { -// WebhookDefinition New(); -// Task SaveAsync(WebhookDefinition webhookDefinition, CancellationToken cancellationToken = default); -// Task UpdateAsync(WebhookDefinition webhookDefinition, CancellationToken cancellationToken = default); -// Task DeleteAsync(string webhookId, CancellationToken cancellationToken = default); -// Task DeleteAsync(WebhookDefinition webhookDefinition, CancellationToken cancellationToken = default); -// } -// } diff --git a/src/core/Elsa.Core/Decorators/CachingWorkflowRegistry.cs b/src/core/Elsa.Core/Decorators/CachingWorkflowRegistry.cs index 1d6fde63c..dbc9d982c 100644 --- a/src/core/Elsa.Core/Decorators/CachingWorkflowRegistry.cs +++ b/src/core/Elsa.Core/Decorators/CachingWorkflowRegistry.cs @@ -19,7 +19,7 @@ namespace Elsa.Decorators { public class CachingWorkflowRegistry : IWorkflowRegistry, INotificationHandler, INotificationHandler { - private const string CacheKey = "WorkflowRegistry"; + public const string CacheKey = "WorkflowRegistry"; private readonly IWorkflowRegistry _workflowRegistry; private readonly IMemoryCache _memoryCache; private readonly ICacheSignal _cacheSignal; diff --git a/src/core/Elsa.Core/Services/Workflows/ActivityTypeService.cs b/src/core/Elsa.Core/Services/Workflows/ActivityTypeService.cs index fe0b22e3c..7c810a200 100644 --- a/src/core/Elsa.Core/Services/Workflows/ActivityTypeService.cs +++ b/src/core/Elsa.Core/Services/Workflows/ActivityTypeService.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; +using Elsa.Caching; using Elsa.Events; using Elsa.Exceptions; using Elsa.Metadata; @@ -14,15 +15,18 @@ namespace Elsa.Services.Workflows { public class ActivityTypeService : IActivityTypeService { + public const string CacheKey = "ActivityTypes"; private readonly IEnumerable _providers; private readonly IMemoryCache _memoryCache; private readonly IMediator _mediator; + private readonly ICacheSignal _cacheSignal; - public ActivityTypeService(IEnumerable providers, IMemoryCache memoryCache, IMediator mediator) + public ActivityTypeService(IEnumerable providers, IMemoryCache memoryCache, IMediator mediator, ICacheSignal cacheSignal) { _providers = providers; _memoryCache = memoryCache; _mediator = mediator; + _cacheSignal = cacheSignal; } public async ValueTask> GetActivityTypesAsync(CancellationToken cancellationToken) => (await GetDictionaryAsync(cancellationToken)).Values; @@ -61,8 +65,11 @@ namespace Elsa.Services.Workflows private async ValueTask> GetDictionaryAsync(CancellationToken cancellationToken) { - const string key = "ActivityTypes"; - return await _memoryCache.GetOrCreate(key, async _ => await GetActivityTypesInternalAsync(cancellationToken).ToDictionaryAsync(x => x.TypeName, cancellationToken)); + return await _memoryCache.GetOrCreate(CacheKey, async entry => + { + entry.Monitor(_cacheSignal.GetToken(CacheKey)); + return await GetActivityTypesInternalAsync(cancellationToken).ToDictionaryAsync(x => x.TypeName, cancellationToken); + }); } private async IAsyncEnumerable GetActivityTypesInternalAsync([EnumeratorCancellation] CancellationToken cancellationToken)