Moved logic for updating the Activity Registry
This commit is contained in:
parent
e7863da413
commit
90d9561e76
|
|
@ -1,6 +1,6 @@
|
|||
using Elsa.MassTransit.Messages;
|
||||
using Elsa.Workflows.Contracts;
|
||||
using Elsa.Workflows.Management.Activities.WorkflowDefinitionActivity;
|
||||
using Elsa.Workflows.Management.Contracts;
|
||||
using JetBrains.Annotations;
|
||||
using MassTransit;
|
||||
|
||||
|
|
@ -10,7 +10,7 @@ namespace Elsa.MassTransit.Consumers;
|
|||
/// Consumes messages related to workflow definition changes.
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public class WorkflowDefinitionEventsConsumer(IActivityRegistryPopulator activityRegistryPopulator) :
|
||||
public class WorkflowDefinitionEventsConsumer(IActivityRegistryUpdateService activityRegistryUpdateService) :
|
||||
IConsumer<WorkflowDefinitionCreated>,
|
||||
IConsumer<WorkflowDefinitionDeleted>,
|
||||
IConsumer<WorkflowDefinitionPublished>,
|
||||
|
|
@ -29,7 +29,7 @@ public class WorkflowDefinitionEventsConsumer(IActivityRegistryPopulator activit
|
|||
/// <inheritdoc />
|
||||
public Task Consume(ConsumeContext<WorkflowDefinitionDeleted> context)
|
||||
{
|
||||
activityRegistryPopulator.RemoveDefinitionFromRegistry(typeof(WorkflowDefinitionActivityProvider), context.Message.Id);
|
||||
activityRegistryUpdateService.RemoveDefinitionFromRegistry(typeof(WorkflowDefinitionActivityProvider), context.Message.Id);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
|
|
@ -42,7 +42,7 @@ public class WorkflowDefinitionEventsConsumer(IActivityRegistryPopulator activit
|
|||
/// <inheritdoc />
|
||||
public Task Consume(ConsumeContext<WorkflowDefinitionRetracted> context)
|
||||
{
|
||||
activityRegistryPopulator.RemoveDefinitionVersionFromRegistry(typeof(WorkflowDefinitionActivityProvider), context.Message.Id);
|
||||
activityRegistryUpdateService.RemoveDefinitionVersionFromRegistry(typeof(WorkflowDefinitionActivityProvider), context.Message.Id);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
|
|
@ -51,7 +51,7 @@ public class WorkflowDefinitionEventsConsumer(IActivityRegistryPopulator activit
|
|||
{
|
||||
foreach (var id in context.Message.Ids)
|
||||
{
|
||||
activityRegistryPopulator.RemoveDefinitionFromRegistry(typeof(WorkflowDefinitionActivityProvider), id);
|
||||
activityRegistryUpdateService.RemoveDefinitionFromRegistry(typeof(WorkflowDefinitionActivityProvider), id);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
|
|
@ -60,7 +60,7 @@ public class WorkflowDefinitionEventsConsumer(IActivityRegistryPopulator activit
|
|||
/// <inheritdoc />
|
||||
public Task Consume(ConsumeContext<WorkflowDefinitionVersionDeleted> context)
|
||||
{
|
||||
activityRegistryPopulator.RemoveDefinitionVersionFromRegistry(typeof(WorkflowDefinitionActivityProvider), context.Message.Id);
|
||||
activityRegistryUpdateService.RemoveDefinitionVersionFromRegistry(typeof(WorkflowDefinitionActivityProvider), context.Message.Id);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
|
|
@ -69,7 +69,7 @@ public class WorkflowDefinitionEventsConsumer(IActivityRegistryPopulator activit
|
|||
{
|
||||
foreach (var id in context.Message.Ids)
|
||||
{
|
||||
activityRegistryPopulator.RemoveDefinitionVersionFromRegistry(typeof(WorkflowDefinitionActivityProvider), id);
|
||||
activityRegistryUpdateService.RemoveDefinitionVersionFromRegistry(typeof(WorkflowDefinitionActivityProvider), id);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
|
|
@ -87,9 +87,9 @@ public class WorkflowDefinitionEventsConsumer(IActivityRegistryPopulator activit
|
|||
private Task UpdateDefinition(string id, bool usableAsActivity)
|
||||
{
|
||||
if (usableAsActivity)
|
||||
return activityRegistryPopulator.AddToRegistry(typeof(WorkflowDefinitionActivityProvider), id);
|
||||
return activityRegistryUpdateService.AddToRegistry(typeof(WorkflowDefinitionActivityProvider), id);
|
||||
|
||||
activityRegistryPopulator.RemoveDefinitionVersionFromRegistry(typeof(WorkflowDefinitionActivityProvider), id);
|
||||
activityRegistryUpdateService.RemoveDefinitionVersionFromRegistry(typeof(WorkflowDefinitionActivityProvider), id);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
namespace Elsa.Workflows.Contracts;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a service for updating the activity registry.
|
||||
/// </summary>
|
||||
public interface IActivityRegistryUpdateService
|
||||
{
|
||||
/// <summary>
|
||||
/// Tries to add a workflow as an activity to the registry.
|
||||
/// </summary>
|
||||
/// <param name="providerType">The type of the activity provider.</param>
|
||||
/// <param name="workflowDefinitionId">The ID of the workflow definition.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
Task AddToRegistry(Type providerType, string workflowDefinitionId, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Removes workflow definition activities from the <see cref="IActivityRegistry"/>.
|
||||
/// </summary>
|
||||
/// <param name="providerType">The type of the Activity Provider.</param>
|
||||
/// <param name="workflowDefinitionId">The ID of the workflow definition to remove.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
void RemoveDefinitionFromRegistry(Type providerType, string workflowDefinitionId, CancellationToken cancellationToken = default);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Removes a workflow definition version activity from the <see cref="IActivityRegistry"/>.
|
||||
/// </summary>
|
||||
/// <param name="providerType">The type of the Activity Provider.</param>
|
||||
/// <param name="workflowDefinitionVersionId">The ID of the workflow definition to remove.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
void RemoveDefinitionVersionFromRegistry(Type providerType, string workflowDefinitionVersionId, CancellationToken cancellationToken = default);
|
||||
}
|
||||
|
|
@ -137,6 +137,7 @@ public class WorkflowsFeature : FeatureBase
|
|||
.AddSingleton<IActivityDescriber, ActivityDescriber>()
|
||||
.AddSingleton<IActivityRegistry, ActivityRegistry>()
|
||||
.AddScoped<IActivityRegistryLookupService, ActivityRegistryLookupService>()
|
||||
.AddScoped<IActivityRegistryUpdateService, ActivityRegistryUpdateService>()
|
||||
.AddSingleton<IPropertyDefaultValueResolver, PropertyDefaultValueResolver>()
|
||||
.AddSingleton<IPropertyUIHandlerResolver, PropertyUIHandlerResolver>()
|
||||
.AddSingleton<IActivityFactory, ActivityFactory>()
|
||||
|
|
|
|||
|
|
@ -0,0 +1,54 @@
|
|||
using Elsa.Workflows.Contracts;
|
||||
using Elsa.Workflows.Models;
|
||||
|
||||
namespace Elsa.Workflows.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Service responsible for updating the activity registry based on activity providers.
|
||||
/// </summary>
|
||||
public class ActivityRegistryUpdateService(IEnumerable<IActivityProvider> providers, IActivityRegistry registry) : IActivityRegistryUpdateService
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task AddToRegistry(Type providerType, string workflowDefinitionVersionId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var provider = providers.First(x => x.GetType() == providerType);
|
||||
var descriptors = await provider.GetDescriptorsAsync(cancellationToken);
|
||||
var descriptorToAdd = descriptors
|
||||
.SingleOrDefault(d =>
|
||||
d.CustomProperties.TryGetValue("WorkflowDefinitionVersionId", out var val) &&
|
||||
val.ToString() == workflowDefinitionVersionId);
|
||||
|
||||
if (descriptorToAdd is not null)
|
||||
registry.Add(providerType, descriptorToAdd);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void RemoveDefinitionFromRegistry(Type providerType, string workflowDefinitionId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var providerDescriptors = registry.ListByProvider(providerType);
|
||||
|
||||
var descriptorsToRemove = providerDescriptors
|
||||
.Where(d =>
|
||||
d.CustomProperties.TryGetValue("WorkflowDefinitionId", out var val) &&
|
||||
val.ToString() == workflowDefinitionId).ToList();
|
||||
|
||||
foreach (ActivityDescriptor activityDescriptor in descriptorsToRemove)
|
||||
{
|
||||
registry.Remove(providerType, activityDescriptor);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void RemoveDefinitionVersionFromRegistry(Type providerType, string workflowDefinitionVersionId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var providerDescriptors = registry.ListByProvider(providerType);
|
||||
|
||||
var descriptorToRemove = providerDescriptors
|
||||
.SingleOrDefault(d =>
|
||||
d.CustomProperties.TryGetValue("WorkflowDefinitionVersionId", out var val) &&
|
||||
val.ToString() == workflowDefinitionVersionId);
|
||||
|
||||
if (descriptorToRemove is not null)
|
||||
registry.Remove(providerType, descriptorToRemove);
|
||||
}
|
||||
}
|
||||
|
|
@ -12,29 +12,4 @@ public interface IActivityRegistryPopulator
|
|||
/// </summary>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
Task PopulateRegistryAsync(CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Tries to add a workflow as an activity to the registry.
|
||||
/// </summary>
|
||||
/// <param name="providerType">The type of the activity provider.</param>
|
||||
/// <param name="workflowDefinitionId">The ID of the workflow definition.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
Task AddToRegistry(Type providerType, string workflowDefinitionId, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Removes workflow definition activities from the <see cref="IActivityRegistry"/>.
|
||||
/// </summary>
|
||||
/// <param name="providerType">The type of the Activity Provider.</param>
|
||||
/// <param name="workflowDefinitionId">The ID of the workflow definition to remove.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
void RemoveDefinitionFromRegistry(Type providerType, string workflowDefinitionId, CancellationToken cancellationToken = default);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Removes a workflow definition version activity from the <see cref="IActivityRegistry"/>.
|
||||
/// </summary>
|
||||
/// <param name="providerType">The type of the Activity Provider.</param>
|
||||
/// <param name="workflowDefinitionVersionId">The ID of the workflow definition to remove.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
void RemoveDefinitionVersionFromRegistry(Type providerType, string workflowDefinitionVersionId, CancellationToken cancellationToken = default);
|
||||
}
|
||||
|
|
@ -1,7 +1,6 @@
|
|||
using Elsa.Mediator.Contracts;
|
||||
using Elsa.Workflows.Contracts;
|
||||
using Elsa.Workflows.Management.Activities.WorkflowDefinitionActivity;
|
||||
using Elsa.Workflows.Management.Contracts;
|
||||
using Elsa.Workflows.Management.Entities;
|
||||
using Elsa.Workflows.Management.Notifications;
|
||||
using JetBrains.Annotations;
|
||||
|
|
@ -12,7 +11,7 @@ namespace Elsa.Workflows.Management.Handlers;
|
|||
/// Refreshes the <see cref="IActivityRegistry"/> for the <see cref="WorkflowDefinitionActivityProvider"/> provider whenever an <see cref="WorkflowDefinition"/> is published, retracted or deleted.
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public class RefreshActivityRegistry(IActivityRegistryPopulator activityRegistryPopulator) :
|
||||
public class RefreshActivityRegistry(IActivityRegistryUpdateService activityRegistryUpdateService) :
|
||||
INotificationHandler<WorkflowDefinitionPublished>,
|
||||
INotificationHandler<WorkflowDefinitionRetracted>,
|
||||
INotificationHandler<WorkflowDefinitionDeleted>,
|
||||
|
|
@ -31,14 +30,14 @@ public class RefreshActivityRegistry(IActivityRegistryPopulator activityRegistry
|
|||
/// <inheritdoc />
|
||||
public Task HandleAsync(WorkflowDefinitionRetracted notification, CancellationToken cancellationToken)
|
||||
{
|
||||
activityRegistryPopulator.RemoveDefinitionVersionFromRegistry(typeof(WorkflowDefinitionActivityProvider), notification.WorkflowDefinition.Id, cancellationToken);
|
||||
activityRegistryUpdateService.RemoveDefinitionVersionFromRegistry(typeof(WorkflowDefinitionActivityProvider), notification.WorkflowDefinition.Id, cancellationToken);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task HandleAsync(WorkflowDefinitionDeleted notification, CancellationToken cancellationToken)
|
||||
{
|
||||
activityRegistryPopulator.RemoveDefinitionFromRegistry(typeof(WorkflowDefinitionActivityProvider), notification.DefinitionId, cancellationToken);
|
||||
activityRegistryUpdateService.RemoveDefinitionFromRegistry(typeof(WorkflowDefinitionActivityProvider), notification.DefinitionId, cancellationToken);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
|
|
@ -47,7 +46,7 @@ public class RefreshActivityRegistry(IActivityRegistryPopulator activityRegistry
|
|||
{
|
||||
foreach (string id in notification.DefinitionIds)
|
||||
{
|
||||
activityRegistryPopulator.RemoveDefinitionFromRegistry(typeof(WorkflowDefinitionActivityProvider), id, cancellationToken);
|
||||
activityRegistryUpdateService.RemoveDefinitionFromRegistry(typeof(WorkflowDefinitionActivityProvider), id, cancellationToken);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
|
|
@ -62,7 +61,7 @@ public class RefreshActivityRegistry(IActivityRegistryPopulator activityRegistry
|
|||
/// <inheritdoc />
|
||||
public Task HandleAsync(WorkflowDefinitionVersionDeleted notification, CancellationToken cancellationToken)
|
||||
{
|
||||
activityRegistryPopulator.RemoveDefinitionVersionFromRegistry(typeof(WorkflowDefinitionActivityProvider), notification.WorkflowDefinition.Id, cancellationToken);
|
||||
activityRegistryUpdateService.RemoveDefinitionVersionFromRegistry(typeof(WorkflowDefinitionActivityProvider), notification.WorkflowDefinition.Id, cancellationToken);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
|
|
@ -71,7 +70,7 @@ public class RefreshActivityRegistry(IActivityRegistryPopulator activityRegistry
|
|||
{
|
||||
foreach (string id in notification.Ids)
|
||||
{
|
||||
activityRegistryPopulator.RemoveDefinitionVersionFromRegistry(typeof(WorkflowDefinitionActivityProvider), id, cancellationToken);
|
||||
activityRegistryUpdateService.RemoveDefinitionVersionFromRegistry(typeof(WorkflowDefinitionActivityProvider), id, cancellationToken);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
|
|
@ -89,9 +88,9 @@ public class RefreshActivityRegistry(IActivityRegistryPopulator activityRegistry
|
|||
private Task UpdateDefinition(string id, bool? usableAsActivity)
|
||||
{
|
||||
if (usableAsActivity.GetValueOrDefault())
|
||||
return activityRegistryPopulator.AddToRegistry(typeof(WorkflowDefinitionActivityProvider), id);
|
||||
return activityRegistryUpdateService.AddToRegistry(typeof(WorkflowDefinitionActivityProvider), id);
|
||||
|
||||
activityRegistryPopulator.RemoveDefinitionVersionFromRegistry(typeof(WorkflowDefinitionActivityProvider), id);
|
||||
activityRegistryUpdateService.RemoveDefinitionVersionFromRegistry(typeof(WorkflowDefinitionActivityProvider), id);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,5 @@
|
|||
using Elsa.Workflows.Contracts;
|
||||
using Elsa.Workflows.Management.Contracts;
|
||||
using Elsa.Workflows.Models;
|
||||
|
||||
namespace Elsa.Workflows.Management.Services;
|
||||
|
||||
|
|
@ -14,48 +13,4 @@ public class ActivityRegistryPopulator(IEnumerable<IActivityProvider> providers,
|
|||
{
|
||||
await registry.RefreshDescriptors(providers, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task AddToRegistry(Type providerType, string workflowDefinitionVersionId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var provider = providers.First(x => x.GetType() == providerType);
|
||||
var descriptors = await provider.GetDescriptorsAsync(cancellationToken);
|
||||
var descriptorToAdd = descriptors
|
||||
.SingleOrDefault(d =>
|
||||
d.CustomProperties.TryGetValue("WorkflowDefinitionVersionId", out var val) &&
|
||||
val.ToString() == workflowDefinitionVersionId);
|
||||
|
||||
if (descriptorToAdd is not null)
|
||||
registry.Add(providerType, descriptorToAdd);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void RemoveDefinitionFromRegistry(Type providerType, string workflowDefinitionId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var providerDescriptors = registry.ListByProvider(providerType);
|
||||
|
||||
var descriptorsToRemove = providerDescriptors
|
||||
.Where(d =>
|
||||
d.CustomProperties.TryGetValue("WorkflowDefinitionId", out var val) &&
|
||||
val.ToString() == workflowDefinitionId).ToList();
|
||||
|
||||
foreach (ActivityDescriptor activityDescriptor in descriptorsToRemove)
|
||||
{
|
||||
registry.Remove(providerType, activityDescriptor);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void RemoveDefinitionVersionFromRegistry(Type providerType, string workflowDefinitionVersionId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var providerDescriptors = registry.ListByProvider(providerType);
|
||||
|
||||
var descriptorToRemove = providerDescriptors
|
||||
.SingleOrDefault(d =>
|
||||
d.CustomProperties.TryGetValue("WorkflowDefinitionVersionId", out var val) &&
|
||||
val.ToString() == workflowDefinitionVersionId);
|
||||
|
||||
if (descriptorToRemove is not null)
|
||||
registry.Remove(providerType, descriptorToRemove);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue