public async Task StartAsync(CancellationToken cancellationToken)
            {
                var workflow = await GetWorkflowBlueprintAsync(GetWorkflowDefinition());

                var workflowInstance = await _workflowRunner.StartWorkflowAsync(workflow, cancellationToken : cancellationToken);

                Assert.NotNull(workflowInstance);
                Assert.Null(workflowInstance.Fault);
            }
Beispiel #2
0
        public async Task <Unit> Handle(ExecuteWorkflowDefinitionRequest request, CancellationToken cancellationToken)
        {
            var workflowDefinitionId = request.WorkflowDefinitionId;
            var tenantId             = request.TenantId;
            var workflowBlueprint    = await _workflowRegistry.GetAsync(workflowDefinitionId, tenantId, VersionOptions.Published, cancellationToken);

            if (!ValidatePreconditions(workflowDefinitionId, workflowBlueprint))
            {
                return(Unit.Value);
            }

            var lockKey               = $"execute-workflow-definition:tenant:{tenantId}:workflow-definition:{workflowDefinitionId}";
            var correlationId         = request.CorrelationId;
            var correlationLockHandle = default(IDistributedSynchronizationHandle?);

            if (!string.IsNullOrWhiteSpace(correlationId))
            {
                _logger.LogDebug("Acquiring lock on correlation ID {CorrelationId}", correlationId);
                correlationLockHandle = await _distributedLockProvider.AcquireLockAsync(correlationId, _elsaOptions.DistributedLockTimeout, cancellationToken);

                if (correlationLockHandle == null)
                {
                    throw new LockAcquisitionException($"Failed to acquire a lock on correlation ID {correlationId}");
                }
            }

            try
            {
                await using var handle = await _distributedLockProvider.AcquireLockAsync(lockKey, _elsaOptions.DistributedLockTimeout, cancellationToken);

                if (handle == null)
                {
                    throw new LockAcquisitionException($"Failed to acquire a lock on {lockKey}");
                }

                if (!workflowBlueprint !.IsSingleton || await GetWorkflowIsAlreadyExecutingAsync(tenantId, workflowDefinitionId) == false)
                {
                    await _startsWorkflow.StartWorkflowAsync(workflowBlueprint, request.ActivityId, request.Input, request.CorrelationId, request.ContextId, cancellationToken);
                }
            }
            finally
            {
                if (correlationLockHandle != null)
                {
                    await correlationLockHandle.DisposeAsync();
                }
            }

            return(Unit.Value);
        }