2024-03-24 20:09:51 +00:00
|
|
|
using Elsa.MassTransit.Messages;
|
2024-07-05 16:35:22 +00:00
|
|
|
using Elsa.MassTransit.Services;
|
|
|
|
|
using Elsa.Mediator.Contracts;
|
2024-06-07 08:53:29 +00:00
|
|
|
using Elsa.Workflows.Management.Contracts;
|
2024-03-24 20:09:51 +00:00
|
|
|
using JetBrains.Annotations;
|
|
|
|
|
using MassTransit;
|
|
|
|
|
|
|
|
|
|
namespace Elsa.MassTransit.Consumers;
|
|
|
|
|
|
|
|
|
|
/// Consumes messages related to workflow definition changes.
|
|
|
|
|
[PublicAPI]
|
2024-07-05 16:35:22 +00:00
|
|
|
public class WorkflowDefinitionEventsConsumer(IWorkflowDefinitionActivityRegistryUpdater workflowDefinitionActivityRegistryUpdater, INotificationSender notificationSender) :
|
|
|
|
|
global::MassTransit.IConsumer<WorkflowDefinitionDeleted>,
|
|
|
|
|
global::MassTransit.IConsumer<WorkflowDefinitionPublished>,
|
|
|
|
|
global::MassTransit.IConsumer<WorkflowDefinitionRetracted>,
|
|
|
|
|
global::MassTransit.IConsumer<WorkflowDefinitionVersionRetracted>,
|
|
|
|
|
global::MassTransit.IConsumer<WorkflowDefinitionsDeleted>,
|
|
|
|
|
global::MassTransit.IConsumer<WorkflowDefinitionVersionDeleted>,
|
|
|
|
|
global::MassTransit.IConsumer<WorkflowDefinitionVersionsDeleted>,
|
|
|
|
|
global::MassTransit.IConsumer<WorkflowDefinitionVersionsUpdated>,
|
2024-07-12 06:35:46 +00:00
|
|
|
global::MassTransit.IConsumer<WorkflowDefinitionsRefreshed>,
|
|
|
|
|
global::MassTransit.IConsumer<WorkflowDefinitionsReloaded>
|
2024-03-24 20:09:51 +00:00
|
|
|
{
|
|
|
|
|
/// <inheritdoc />
|
2024-05-13 10:44:24 +00:00
|
|
|
public Task Consume(ConsumeContext<WorkflowDefinitionDeleted> context)
|
2024-07-05 16:35:22 +00:00
|
|
|
{
|
2024-06-07 08:53:29 +00:00
|
|
|
workflowDefinitionActivityRegistryUpdater.RemoveDefinitionFromRegistry(context.Message.Id);
|
2024-05-13 10:44:24 +00:00
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
2024-03-24 20:09:51 +00:00
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2024-05-13 10:44:24 +00:00
|
|
|
public Task Consume(ConsumeContext<WorkflowDefinitionPublished> context)
|
2024-04-10 06:46:50 +00:00
|
|
|
{
|
2024-06-24 08:45:53 +00:00
|
|
|
return UpdateDefinition(context.Message.Id, context.Message.UsableAsActivity);
|
2024-05-15 09:51:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public Task Consume(ConsumeContext<WorkflowDefinitionRetracted> context)
|
|
|
|
|
{
|
2024-06-24 08:45:53 +00:00
|
|
|
return UpdateDefinition(context.Message.Id, context.Message.UsableAsActivity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public Task Consume(ConsumeContext<WorkflowDefinitionVersionRetracted> context)
|
|
|
|
|
{
|
|
|
|
|
return UpdateDefinition(context.Message.Id, context.Message.UsableAsActivity);
|
2024-04-10 06:46:50 +00:00
|
|
|
}
|
2024-03-24 20:09:51 +00:00
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2024-05-13 10:44:24 +00:00
|
|
|
public Task Consume(ConsumeContext<WorkflowDefinitionsDeleted> context)
|
|
|
|
|
{
|
|
|
|
|
foreach (var id in context.Message.Ids)
|
2024-06-07 08:53:29 +00:00
|
|
|
workflowDefinitionActivityRegistryUpdater.RemoveDefinitionFromRegistry(id);
|
2024-03-24 20:09:51 +00:00
|
|
|
|
2024-05-13 10:44:24 +00:00
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
2024-03-24 20:09:51 +00:00
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2024-05-13 10:44:24 +00:00
|
|
|
public Task Consume(ConsumeContext<WorkflowDefinitionVersionDeleted> context)
|
|
|
|
|
{
|
2024-06-07 08:53:29 +00:00
|
|
|
workflowDefinitionActivityRegistryUpdater.RemoveDefinitionVersionFromRegistry(context.Message.Id);
|
2024-05-13 10:44:24 +00:00
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
2024-03-24 20:09:51 +00:00
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2024-05-13 10:44:24 +00:00
|
|
|
public Task Consume(ConsumeContext<WorkflowDefinitionVersionsDeleted> context)
|
|
|
|
|
{
|
|
|
|
|
foreach (var id in context.Message.Ids)
|
2024-06-07 08:53:29 +00:00
|
|
|
workflowDefinitionActivityRegistryUpdater.RemoveDefinitionVersionFromRegistry(id);
|
2024-05-13 10:44:24 +00:00
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
2024-05-15 09:51:02 +00:00
|
|
|
|
2024-05-23 13:06:51 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public async Task Consume(ConsumeContext<WorkflowDefinitionVersionsUpdated> context)
|
|
|
|
|
{
|
2024-06-13 12:11:47 +00:00
|
|
|
foreach (WorkflowDefinitionVersionUpdate definitionUpdate in context.Message.WorkflowDefinitionVersionUpdates)
|
2024-06-24 08:45:53 +00:00
|
|
|
await UpdateDefinition(definitionUpdate.Id, definitionUpdate.UsableAsActivity);
|
2024-07-05 16:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public async Task Consume(ConsumeContext<WorkflowDefinitionsRefreshed> context)
|
|
|
|
|
{
|
|
|
|
|
var message = context.Message;
|
|
|
|
|
var notification = new Elsa.Workflows.Runtime.Notifications.WorkflowDefinitionsRefreshed(message.WorkflowDefinitionIds);
|
2024-07-12 06:35:46 +00:00
|
|
|
AmbientConsumerScope.IsWorkflowDefinitionEventsConsumer = true;
|
2024-07-05 16:35:22 +00:00
|
|
|
await notificationSender.SendAsync(notification, context.CancellationToken);
|
2024-07-12 06:35:46 +00:00
|
|
|
AmbientConsumerScope.IsWorkflowDefinitionEventsConsumer = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public async Task Consume(ConsumeContext<WorkflowDefinitionsReloaded> context)
|
|
|
|
|
{
|
|
|
|
|
var message = context.Message;
|
|
|
|
|
var notification = new Elsa.Workflows.Runtime.Notifications.WorkflowDefinitionsReloaded(message.WorkflowDefinitionIds);
|
|
|
|
|
AmbientConsumerScope.IsWorkflowDefinitionEventsConsumer = true;
|
|
|
|
|
await notificationSender.SendAsync(notification, context.CancellationToken);
|
|
|
|
|
AmbientConsumerScope.IsWorkflowDefinitionEventsConsumer = false;
|
2024-05-23 13:06:51 +00:00
|
|
|
}
|
|
|
|
|
|
2024-06-24 08:45:53 +00:00
|
|
|
private Task UpdateDefinition(string id, bool usableAsActivity)
|
2024-05-15 09:51:02 +00:00
|
|
|
{
|
2024-07-05 16:35:22 +00:00
|
|
|
// Once a workflow has been published, it should remain in the activity registry unless no longer being marked as an activity.
|
2024-06-24 08:45:53 +00:00
|
|
|
if (usableAsActivity)
|
2024-06-07 08:53:29 +00:00
|
|
|
return workflowDefinitionActivityRegistryUpdater.AddToRegistry(id);
|
2024-05-15 09:51:02 +00:00
|
|
|
|
2024-06-07 08:53:29 +00:00
|
|
|
workflowDefinitionActivityRegistryUpdater.RemoveDefinitionVersionFromRegistry(id);
|
2024-05-15 09:51:02 +00:00
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
2024-03-24 20:09:51 +00:00
|
|
|
}
|