| | | 1 | | using System.ComponentModel.DataAnnotations; |
| | | 2 | | using Elsa.Workflows.Management; |
| | | 3 | | using Elsa.Workflows.Management.Filters; |
| | | 4 | | |
| | | 5 | | namespace Elsa.Workflows.Runtime.ActivationValidators; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Only allow new workflow instances if a running one with the same correlation ID doesn't already exist. |
| | | 9 | | /// A non-blank correlation ID is required for this strategy. |
| | | 10 | | /// </summary> |
| | | 11 | | [Display(Name = "Correlation", Description = "Only allow new workflow instances of any workflow definition if a running |
| | 749 | 12 | | public class CorrelationStrategy(IWorkflowInstanceStore workflowInstanceStore) : IWorkflowActivationStrategy |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Only allow a new instance if no running ones exist already. |
| | | 16 | | /// </summary> |
| | | 17 | | public async ValueTask<bool> GetAllowActivationAsync(WorkflowInstantiationStrategyContext context) |
| | | 18 | | { |
| | 10 | 19 | | if (string.IsNullOrWhiteSpace(context.CorrelationId)) |
| | 4 | 20 | | throw new ArgumentException("A non-blank correlation ID is required when using CorrelationStrategy.", nameof |
| | | 21 | | |
| | 6 | 22 | | var filter = new WorkflowInstanceFilter |
| | 6 | 23 | | { |
| | 6 | 24 | | CorrelationId = context.CorrelationId, |
| | 6 | 25 | | WorkflowStatus = WorkflowStatus.Running |
| | 6 | 26 | | }; |
| | | 27 | | |
| | 6 | 28 | | var count = await workflowInstanceStore.CountAsync(filter, context.CancellationToken); |
| | 6 | 29 | | return count == 0; |
| | 6 | 30 | | } |
| | | 31 | | } |