| | | 1 | | using Elsa.Mediator.Contracts; |
| | | 2 | | using Elsa.Workflows.Management.Activities.WorkflowDefinitionActivity; |
| | | 3 | | using Elsa.Workflows.Management.Contracts; |
| | | 4 | | using Elsa.Workflows.Management.Entities; |
| | | 5 | | using Elsa.Workflows.Management.Notifications; |
| | | 6 | | using JetBrains.Annotations; |
| | | 7 | | |
| | | 8 | | namespace Elsa.Workflows.Management.Handlers.Notifications; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Refreshes the <see cref="IActivityRegistry"/> for the <see cref="WorkflowDefinitionActivityProvider"/> provider when |
| | | 12 | | /// </summary> |
| | | 13 | | [PublicAPI] |
| | 319 | 14 | | public class RefreshActivityRegistry(IWorkflowDefinitionActivityRegistryUpdater workflowDefinitionActivityRegistryUpdate |
| | | 15 | | INotificationHandler<WorkflowDefinitionPublished>, |
| | | 16 | | INotificationHandler<WorkflowDefinitionRetracted>, |
| | | 17 | | INotificationHandler<WorkflowDefinitionVersionRetracted>, |
| | | 18 | | INotificationHandler<WorkflowDefinitionDeleted>, |
| | | 19 | | INotificationHandler<WorkflowDefinitionsDeleted>, |
| | | 20 | | INotificationHandler<WorkflowDefinitionVersionDeleted>, |
| | | 21 | | INotificationHandler<WorkflowDefinitionVersionsDeleted>, |
| | | 22 | | INotificationHandler<WorkflowDefinitionVersionsUpdated> |
| | | 23 | | { |
| | | 24 | | /// <inheritdoc /> |
| | | 25 | | public Task HandleAsync(WorkflowDefinitionPublished notification, CancellationToken cancellationToken) |
| | | 26 | | { |
| | 5 | 27 | | return UpdateDefinition(notification.WorkflowDefinition.Id, notification.WorkflowDefinition.Options.UsableAsActi |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <inheritdoc /> |
| | | 31 | | public Task HandleAsync(WorkflowDefinitionRetracted notification, CancellationToken cancellationToken) |
| | | 32 | | { |
| | 0 | 33 | | return UpdateDefinition(notification.WorkflowDefinition.Id, notification.WorkflowDefinition.Options.UsableAsActi |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <inheritdoc /> |
| | | 37 | | public Task HandleAsync(WorkflowDefinitionVersionRetracted notification, CancellationToken cancellationToken) |
| | | 38 | | { |
| | 3 | 39 | | return UpdateDefinition(notification.WorkflowDefinition.Id, notification.WorkflowDefinition.Options.UsableAsActi |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <inheritdoc /> |
| | | 43 | | public Task HandleAsync(WorkflowDefinitionDeleted notification, CancellationToken cancellationToken) |
| | | 44 | | { |
| | 4 | 45 | | workflowDefinitionActivityRegistryUpdater.RemoveDefinitionFromRegistry(notification.DefinitionId); |
| | 4 | 46 | | return Task.CompletedTask; |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <inheritdoc /> |
| | | 50 | | public Task HandleAsync(WorkflowDefinitionsDeleted notification, CancellationToken cancellationToken) |
| | | 51 | | { |
| | 0 | 52 | | foreach (string id in notification.DefinitionIds) |
| | | 53 | | { |
| | 0 | 54 | | workflowDefinitionActivityRegistryUpdater.RemoveDefinitionFromRegistry(id); |
| | | 55 | | } |
| | | 56 | | |
| | 0 | 57 | | return Task.CompletedTask; |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <inheritdoc /> |
| | | 61 | | public Task HandleAsync(WorkflowDefinitionVersionDeleted notification, CancellationToken cancellationToken) |
| | | 62 | | { |
| | 0 | 63 | | workflowDefinitionActivityRegistryUpdater.RemoveDefinitionVersionFromRegistry(notification.WorkflowDefinition.Id |
| | 0 | 64 | | return Task.CompletedTask; |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <inheritdoc /> |
| | | 68 | | public Task HandleAsync(WorkflowDefinitionVersionsDeleted notification, CancellationToken cancellationToken) |
| | | 69 | | { |
| | 0 | 70 | | foreach (string id in notification.Ids) |
| | | 71 | | { |
| | 0 | 72 | | workflowDefinitionActivityRegistryUpdater.RemoveDefinitionVersionFromRegistry(id); |
| | | 73 | | } |
| | | 74 | | |
| | 0 | 75 | | return Task.CompletedTask; |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <inheritdoc /> |
| | | 79 | | public async Task HandleAsync(WorkflowDefinitionVersionsUpdated notification, CancellationToken cancellationToken) |
| | | 80 | | { |
| | 0 | 81 | | foreach (var definition in notification.WorkflowDefinitions) |
| | | 82 | | { |
| | 0 | 83 | | await UpdateDefinition(definition.Id, definition.Options.UsableAsActivity); |
| | | 84 | | } |
| | 0 | 85 | | } |
| | | 86 | | |
| | | 87 | | private Task UpdateDefinition(string id, bool? usableAsActivity) |
| | | 88 | | { |
| | | 89 | | // Once a workflow has been published, it should remain in the activity registry unless no longer being marked a |
| | 8 | 90 | | if (usableAsActivity.GetValueOrDefault()) |
| | 3 | 91 | | return workflowDefinitionActivityRegistryUpdater.AddToRegistry(id); |
| | | 92 | | |
| | 5 | 93 | | workflowDefinitionActivityRegistryUpdater.RemoveDefinitionVersionFromRegistry(id); |
| | 5 | 94 | | return Task.CompletedTask; |
| | | 95 | | } |
| | | 96 | | } |