| | | 1 | | using System.Buffers.Binary; |
| | | 2 | | using System.Security.Cryptography; |
| | | 3 | | using System.Text; |
| | | 4 | | using Elsa.Common.DistributedHosting; |
| | | 5 | | using Elsa.Common.Multitenancy; |
| | | 6 | | using Elsa.Workflows.ActivationValidators; |
| | | 7 | | using Elsa.Workflows.Activities; |
| | | 8 | | using Elsa.Workflows.Runtime.ActivationValidators; |
| | | 9 | | using Medallion.Threading; |
| | | 10 | | using Microsoft.Extensions.Options; |
| | | 11 | | |
| | | 12 | | namespace Elsa.Workflows.Runtime; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | 202 | 15 | | public class WorkflowActivationGate( |
| | 202 | 16 | | IWorkflowActivationStrategyEvaluator evaluator, |
| | 202 | 17 | | IDistributedLockProvider distributedLockProvider, |
| | 202 | 18 | | IOptions<DistributedLockingOptions> distributedLockingOptions, |
| | 202 | 19 | | ITenantAccessor? tenantAccessor = null) : IWorkflowActivationGate |
| | | 20 | | { |
| | | 21 | | /// <inheritdoc /> |
| | | 22 | | public async Task<WorkflowActivationLease> EvaluateAsync(Workflow workflow, string? correlationId, CancellationToken |
| | | 23 | | { |
| | 190 | 24 | | var lockKey = GetLockKey(workflow, correlationId); |
| | 190 | 25 | | IDistributedSynchronizationHandle? lockHandle = null; |
| | 190 | 26 | | CancellationTokenSource? linkedCancellationTokenSource = null; |
| | | 27 | | |
| | 190 | 28 | | if (lockKey != null) |
| | 37 | 29 | | lockHandle = await distributedLockProvider.AcquireLockAsync(lockKey, distributedLockingOptions.Value.LockAcq |
| | | 30 | | |
| | | 31 | | try |
| | | 32 | | { |
| | 189 | 33 | | var effectiveCancellationToken = cancellationToken; |
| | 189 | 34 | | if (lockHandle != null) |
| | | 35 | | { |
| | 36 | 36 | | linkedCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, lockH |
| | 36 | 37 | | effectiveCancellationToken = linkedCancellationTokenSource.Token; |
| | | 38 | | } |
| | | 39 | | |
| | 189 | 40 | | effectiveCancellationToken.ThrowIfCancellationRequested(); |
| | 189 | 41 | | var canStart = await evaluator.CanStartWorkflowAsync(new() |
| | 189 | 42 | | { |
| | 189 | 43 | | Workflow = workflow, |
| | 189 | 44 | | CorrelationId = correlationId, |
| | 189 | 45 | | CancellationToken = effectiveCancellationToken |
| | 189 | 46 | | }); |
| | 185 | 47 | | effectiveCancellationToken.ThrowIfCancellationRequested(); |
| | | 48 | | |
| | 184 | 49 | | if (!canStart) |
| | | 50 | | { |
| | 8 | 51 | | var acquiredLockHandle = lockHandle; |
| | 8 | 52 | | lockHandle = null; |
| | 8 | 53 | | if (acquiredLockHandle != null) |
| | 8 | 54 | | await acquiredLockHandle.DisposeAsync(); |
| | | 55 | | |
| | 8 | 56 | | linkedCancellationTokenSource?.Dispose(); |
| | 8 | 57 | | linkedCancellationTokenSource = null; |
| | | 58 | | |
| | 8 | 59 | | return WorkflowActivationLease.Denied; |
| | | 60 | | } |
| | | 61 | | |
| | 176 | 62 | | var lease = new WorkflowActivationLease(true, lockHandle, effectiveCancellationToken, linkedCancellationToke |
| | 176 | 63 | | lockHandle = null; |
| | 176 | 64 | | linkedCancellationTokenSource = null; |
| | 176 | 65 | | return lease; |
| | | 66 | | } |
| | 5 | 67 | | catch |
| | | 68 | | { |
| | | 69 | | try |
| | | 70 | | { |
| | 5 | 71 | | if (lockHandle != null) |
| | 5 | 72 | | await lockHandle.DisposeAsync(); |
| | 5 | 73 | | } |
| | | 74 | | finally |
| | | 75 | | { |
| | 5 | 76 | | linkedCancellationTokenSource?.Dispose(); |
| | | 77 | | } |
| | | 78 | | |
| | 5 | 79 | | throw; |
| | | 80 | | } |
| | 184 | 81 | | } |
| | | 82 | | |
| | | 83 | | private string? GetLockKey(Workflow workflow, string? correlationId) |
| | | 84 | | { |
| | 190 | 85 | | var strategyType = workflow.Options.ActivationStrategyType; |
| | | 86 | | |
| | 190 | 87 | | if (strategyType == null || strategyType == typeof(AllowAlwaysStrategy)) |
| | 152 | 88 | | return null; |
| | | 89 | | |
| | 38 | 90 | | var tenantId = tenantAccessor?.TenantId ?? Tenant.DefaultTenantId; |
| | 38 | 91 | | var definitionId = workflow.Identity.DefinitionId; |
| | | 92 | | |
| | 38 | 93 | | if (strategyType == typeof(SingletonStrategy)) |
| | 12 | 94 | | return CreateLockKey("singleton", tenantId, definitionId); |
| | | 95 | | |
| | 26 | 96 | | if (strategyType == typeof(CorrelatedSingletonStrategy)) |
| | | 97 | | { |
| | 20 | 98 | | return CreateLockKey("correlated-singleton", tenantId, definitionId, correlationId ?? string.Empty); |
| | | 99 | | } |
| | | 100 | | |
| | 6 | 101 | | if (strategyType == typeof(CorrelationStrategy)) |
| | | 102 | | { |
| | 5 | 103 | | return CreateLockKey("correlation", tenantId, correlationId ?? string.Empty); |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | // Custom strategies remain compatible, but their uniqueness scope is unknown. Do not |
| | | 107 | | // invent a lock key that implies atomicity the strategy has not declared. |
| | 1 | 108 | | return null; |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | private static string CreateLockKey(string strategy, params string[] components) |
| | | 112 | | { |
| | 37 | 113 | | using var stream = new MemoryStream(); |
| | 37 | 114 | | Span<byte> length = stackalloc byte[sizeof(int)]; |
| | 356 | 115 | | foreach (var bytes in components.Select(component => Encoding.UTF8.GetBytes(component))) |
| | | 116 | | { |
| | 94 | 117 | | BinaryPrimitives.WriteInt32BigEndian(length, bytes.Length); |
| | 94 | 118 | | stream.Write(length); |
| | 94 | 119 | | stream.Write(bytes); |
| | | 120 | | } |
| | | 121 | | |
| | 37 | 122 | | var hash = Convert.ToHexString(SHA256.HashData(stream.ToArray())); |
| | 37 | 123 | | return $"workflow-activation:v1:{strategy}:{hash}"; |
| | 37 | 124 | | } |
| | | 125 | | } |