< Summary

Information
Class: Elsa.Workflows.Runtime.ObsoleteWorkflowRuntime
Assembly: Elsa.Workflows.Runtime
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Runtime/Services/ObsoleteWorkflowRuntime.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 167
Coverable lines: 167
Total lines: 257
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 82
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Runtime/Services/ObsoleteWorkflowRuntime.cs

#LineLine coverage
 1
 2using Elsa.Common.Models;
 3using Elsa.Workflows.Management;
 4using Elsa.Workflows.Management.Filters;
 5using Elsa.Workflows.Models;
 6using Elsa.Workflows.Runtime.Entities;
 7using Elsa.Workflows.Runtime.Filters;
 8using Elsa.Workflows.Runtime.Matches;
 9using Elsa.Workflows.Runtime.Messages;
 10using Elsa.Workflows.Runtime.Options;
 11using Elsa.Workflows.Runtime.Parameters;
 12using Elsa.Workflows.Runtime.Params;
 13using Elsa.Workflows.Runtime.Requests;
 14using Elsa.Workflows.Runtime.Results;
 15using Elsa.Workflows.State;
 16using Microsoft.Extensions.DependencyInjection;
 17using Open.Linq.AsyncExtensions;
 18
 19namespace Elsa.Workflows.Runtime;
 20
 21/// <summary>
 22/// Implements the now deprecated workflow runtime API methods.
 23/// </summary>
 024public class ObsoleteWorkflowRuntime(
 025    Func<string?, CancellationToken, ValueTask<IWorkflowClient>> createClientAsync,
 026    IWorkflowDefinitionService workflowDefinitionService,
 027    IWorkflowActivationStrategyEvaluator workflowActivationStrategyEvaluator,
 028    IStimulusSender stimulusSender,
 029    IStimulusHasher stimulusHasher,
 030    IBookmarkStore bookmarkStore,
 031    IWorkflowInstanceStore workflowInstanceStore,
 032    ITriggerBoundWorkflowService triggerBoundWorkflowService,
 033    IBookmarkBoundWorkflowService bookmarkBoundWorkflowService)
 34{
 35    public static ObsoleteWorkflowRuntime Create(IServiceProvider serviceProvider, Func<string?, CancellationToken, Valu
 36    {
 037        return ActivatorUtilities.CreateInstance<ObsoleteWorkflowRuntime>(serviceProvider, createClientAsync);
 38    }
 39
 40    public async Task<CanStartWorkflowResult> CanStartWorkflowAsync(string definitionId, StartWorkflowRuntimeParams? opt
 41    {
 042        var cancellationToken = options?.CancellationToken ?? CancellationToken.None;
 043        var workflowGraph = await workflowDefinitionService.FindWorkflowGraphAsync(definitionId, options?.VersionOptions
 044        var workflow = workflowGraph!.Workflow;
 45
 046        var canStart = await workflowActivationStrategyEvaluator.CanStartWorkflowAsync(new()
 047        {
 048            Workflow = workflow,
 049            CorrelationId = options?.CorrelationId,
 050            CancellationToken = cancellationToken
 051        });
 52
 053        return new(null, canStart);
 054    }
 55
 56    public async Task<WorkflowExecutionResult> StartWorkflowAsync(string definitionId, StartWorkflowRuntimeParams? optio
 57    {
 058        var cancellationToken = options?.CancellationToken ?? CancellationToken.None;
 059        var client = await createClientAsync(options?.InstanceId, cancellationToken);
 060        var createRequest = new CreateAndRunWorkflowInstanceRequest
 061        {
 062            Properties = options?.Properties,
 063            CorrelationId = options?.CorrelationId,
 064            Input = options?.Input,
 065            WorkflowDefinitionHandle = WorkflowDefinitionHandle.ByDefinitionId(definitionId, options?.VersionOptions ?? 
 066            ParentId = options?.ParentWorkflowInstanceId,
 067            TriggerActivityId = options?.TriggerActivityId
 068        };
 069        var response = await client.CreateAndRunInstanceAsync(createRequest, cancellationToken);
 070        return new(response.WorkflowInstanceId, response.Status, response.SubStatus, response.Bookmarks, response.Incide
 071    }
 72
 73    public async Task<ICollection<WorkflowExecutionResult>> StartWorkflowsAsync(string activityTypeName, object bookmark
 74    {
 075        var cancellationToken = options?.CancellationToken ?? CancellationToken.None;
 076        var metadata = new StimulusMetadata
 077        {
 078            CorrelationId = options?.CorrelationId,
 079            WorkflowInstanceId = options?.WorkflowInstanceId,
 080            Properties = options?.Properties,
 081            ActivityInstanceId = options?.ActivityInstanceId,
 082            Input = options?.Input
 083        };
 084        var result = await stimulusSender.SendAsync(activityTypeName, bookmarkPayload, metadata, cancellationToken);
 085        var results = result.WorkflowInstanceResponses.Select(x => new WorkflowExecutionResult(x.WorkflowInstanceId, x.S
 086        return results;
 087    }
 88
 89    public async Task<WorkflowExecutionResult?> TryStartWorkflowAsync(string definitionId, StartWorkflowRuntimeParams? o
 90    {
 091        return await StartWorkflowAsync(definitionId, options);
 092    }
 93
 94    public async Task<WorkflowExecutionResult?> ResumeWorkflowAsync(string workflowInstanceId, ResumeWorkflowRuntimePara
 95    {
 096        var cancellationToken = options?.CancellationToken ?? CancellationToken.None;
 097        var workflowClient = await createClientAsync(workflowInstanceId, cancellationToken);
 098        var exists = await workflowClient.InstanceExistsAsync(cancellationToken);
 99
 0100        if (!exists)
 0101            return null;
 102
 0103        var runWorkflowRequest = new RunWorkflowInstanceRequest
 0104        {
 0105            Input = options?.Input,
 0106            Properties = options?.Properties,
 0107            ActivityHandle = options?.ActivityHandle,
 0108            BookmarkId = options?.BookmarkId
 0109        };
 110
 0111        var response = await workflowClient.RunInstanceAsync(runWorkflowRequest, cancellationToken);
 112
 0113        return new(response.WorkflowInstanceId, response.Status, response.SubStatus, response.Bookmarks, response.Incide
 0114    }
 115
 116    public async Task<ICollection<WorkflowExecutionResult>> ResumeWorkflowsAsync(string activityTypeName, object bookmar
 117    {
 0118        var cancellationToken = options?.CancellationToken ?? CancellationToken.None;
 0119        var metadata = new StimulusMetadata
 0120        {
 0121            CorrelationId = options?.CorrelationId,
 0122            WorkflowInstanceId = options?.WorkflowInstanceId,
 0123            Properties = options?.Properties,
 0124            ActivityInstanceId = options?.ActivityInstanceId,
 0125            Input = options?.Input
 0126        };
 0127        var result = await stimulusSender.SendAsync(activityTypeName, bookmarkPayload, metadata, cancellationToken);
 0128        var results = result.WorkflowInstanceResponses.Select(x => new WorkflowExecutionResult(x.WorkflowInstanceId, x.S
 0129        return results;
 0130    }
 131
 132    public async Task<TriggerWorkflowsResult> TriggerWorkflowsAsync(string activityTypeName, object bookmarkPayload, Tri
 133    {
 0134        var cancellationToken = options?.CancellationToken ?? CancellationToken.None;
 0135        var metadata = new StimulusMetadata
 0136        {
 0137            CorrelationId = options?.CorrelationId,
 0138            WorkflowInstanceId = options?.WorkflowInstanceId,
 0139            Properties = options?.Properties,
 0140            ActivityInstanceId = options?.ActivityInstanceId,
 0141            Input = options?.Input
 0142        };
 0143        var result = await stimulusSender.SendAsync(activityTypeName, bookmarkPayload, metadata, cancellationToken);
 0144        var results = result.WorkflowInstanceResponses.Select(x => new WorkflowExecutionResult(x.WorkflowInstanceId, x.S
 0145        return new(results);
 0146    }
 147
 148    public async Task<WorkflowExecutionResult> ExecuteWorkflowAsync(WorkflowMatch match, ExecuteWorkflowParams? options 
 149    {
 0150        var cancellationToken = options?.CancellationToken ?? CancellationToken.None;
 0151        if (match is StartableWorkflowMatch collectedStartableWorkflow)
 152        {
 0153            var startOptions = new StartWorkflowRuntimeParams
 0154            {
 0155                CorrelationId = collectedStartableWorkflow.CorrelationId,
 0156                Input = options?.Input,
 0157                Properties = options?.Properties,
 0158                VersionOptions = VersionOptions.Published,
 0159                TriggerActivityId = collectedStartableWorkflow.ActivityId,
 0160                CancellationToken = cancellationToken
 0161            };
 162
 0163            var startResult = await StartWorkflowAsync(collectedStartableWorkflow.DefinitionId!, startOptions);
 0164            return startResult with
 0165            {
 0166                TriggeredActivityId = collectedStartableWorkflow.ActivityId
 0167            };
 168        }
 169
 0170        var collectedResumableWorkflow = (match as ResumableWorkflowMatch)!;
 0171        var runtimeOptions = new ResumeWorkflowRuntimeParams
 0172        {
 0173            CorrelationId = collectedResumableWorkflow.CorrelationId,
 0174            BookmarkId = collectedResumableWorkflow.BookmarkId,
 0175            Input = options?.Input,
 0176            Properties = options?.Properties,
 0177            CancellationToken = cancellationToken,
 0178        };
 179
 0180        return (await ResumeWorkflowAsync(collectedResumableWorkflow.WorkflowInstanceId, runtimeOptions))!;
 0181    }
 182
 183    public async Task<CancellationResult> CancelWorkflowAsync(string workflowInstanceId, CancellationToken cancellationT
 184    {
 0185        var client = await createClientAsync(workflowInstanceId, cancellationToken);
 0186        await client.CancelAsync(cancellationToken);
 0187        return new(true);
 0188    }
 189
 190    public async Task<IEnumerable<WorkflowMatch>> FindWorkflowsAsync(WorkflowsFilter filter, CancellationToken cancellat
 191    {
 0192        var startableWorkflows = await FindStartableWorkflowsAsync(filter, cancellationToken);
 0193        var resumableWorkflows = await FindResumableWorkflowsAsync(filter, cancellationToken);
 0194        var results = startableWorkflows.Concat(resumableWorkflows).ToList();
 0195        return results;
 0196    }
 197
 198    public async Task<WorkflowState?> ExportWorkflowStateAsync(string workflowInstanceId, CancellationToken cancellation
 199    {
 0200        var client = await createClientAsync(workflowInstanceId, cancellationToken);
 0201        return await client.ExportStateAsync(cancellationToken);
 0202    }
 203
 204    public async Task ImportWorkflowStateAsync(WorkflowState workflowState, CancellationToken cancellationToken = defaul
 205    {
 0206        var client = await createClientAsync(workflowState.Id, cancellationToken);
 0207        await client.ImportStateAsync(workflowState, cancellationToken);
 0208    }
 209
 210    public async Task UpdateBookmarkAsync(StoredBookmark bookmark, CancellationToken cancellationToken = default)
 211    {
 0212        await bookmarkStore.SaveAsync(bookmark, cancellationToken);
 0213    }
 214
 215    public async Task<long> CountRunningWorkflowsAsync(CountRunningWorkflowsRequest request, CancellationToken cancellat
 216    {
 0217        var filter = new WorkflowInstanceFilter
 0218        {
 0219            DefinitionId = request.DefinitionId,
 0220            Version = request.Version,
 0221            CorrelationId = request.CorrelationId,
 0222            WorkflowStatus = WorkflowStatus.Running
 0223        };
 0224        return await workflowInstanceStore.CountAsync(filter, cancellationToken);
 0225    }
 226
 227    private async Task<IEnumerable<WorkflowMatch>> FindStartableWorkflowsAsync(WorkflowsFilter filter, CancellationToken
 228    {
 0229        var stimulusHash = stimulusHasher.Hash(filter.ActivityTypeName, filter.BookmarkPayload, filter.Options.ActivityI
 0230        var triggerBoundWorkflows = await triggerBoundWorkflowService.FindManyAsync(stimulusHash, cancellationToken).ToL
 0231        var correlationId = filter.Options.CorrelationId;
 232
 0233        var query =
 0234                from triggerBoundWorkflow in triggerBoundWorkflows
 0235                from trigger in triggerBoundWorkflow.Triggers
 0236                select new StartableWorkflowMatch(correlationId, trigger.ActivityId, triggerBoundWorkflow.WorkflowGraph.
 237
 0238        return query.ToList();
 0239    }
 240
 241    private async Task<IEnumerable<WorkflowMatch>> FindResumableWorkflowsAsync(WorkflowsFilter filter, CancellationToken
 242    {
 0243        var bookmarkOptions = new FindBookmarkOptions
 0244        {
 0245            CorrelationId = filter.Options.CorrelationId,
 0246            WorkflowInstanceId = filter.Options.WorkflowInstanceId,
 0247            ActivityInstanceId = filter.Options.ActivityInstanceId
 0248        };
 0249        var bookmarkBoundWorkflows = await bookmarkBoundWorkflowService.FindManyAsync(filter.ActivityTypeName, filter.Bo
 250
 0251        return (
 0252                from bookmarkBoundWorkflow in bookmarkBoundWorkflows
 0253                from bookmark in bookmarkBoundWorkflow.Bookmarks
 0254                select new ResumableWorkflowMatch(bookmarkBoundWorkflow.WorkflowInstanceId, bookmark.CorrelationId, book
 0255            .ToList();
 0256    }
 257}