< Summary

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

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
SubmitAsync()100%210%
SubmitAsync()100%210%
DeliverAsync()100%210%
BroadcastAsync()0%110100%
ResumeWorkflowsAsynchronouslyAsync()100%210%
ResumeWorkflowsSynchronouslyAsync()100%210%
FindManyAsync(...)100%210%
FindManyAsync(...)100%210%
Map(...)100%210%

File(s)

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

#LineLine coverage
 1using Elsa.Common;
 2using Elsa.Workflows.Models;
 3using Elsa.Workflows.Runtime.Contracts;
 4using Elsa.Workflows.Runtime.Entities;
 5using Elsa.Workflows.Runtime.Filters;
 6using Elsa.Workflows.Runtime.Messages;
 7using Elsa.Workflows.Runtime.Options;
 8using Elsa.Workflows.Runtime.Params;
 9using Elsa.Workflows.Runtime.Requests;
 10using Elsa.Workflows.Runtime.Results;
 11using Microsoft.Extensions.Logging;
 12
 13namespace Elsa.Workflows.Runtime;
 14
 15/// <summary>
 16/// Represents a proxy for sending stimulus to the workflow runtime while <see cref="IWorkflowInbox"/> is being deprecat
 17/// </summary>
 18[Obsolete("Please use the Stimulus API instead")]
 019public class StimulusProxyWorkflowInbox(
 020    IStimulusSender stimulusSender,
 021    IWorkflowDispatcher workflowDispatcher,
 022    ISystemClock systemClock,
 023    IIdentityGenerator identityGenerator,
 024    IHasher bookmarkHasher,
 025    ILogger<StimulusProxyWorkflowInbox> logger)
 26    : IWorkflowInbox
 27{
 28    /// <inheritdoc />
 29    public async ValueTask<SubmitWorkflowInboxMessageResult> SubmitAsync(NewWorkflowInboxMessage message, CancellationTo
 30    {
 031        var defaultOptions = new WorkflowInboxMessageDeliveryParams();
 032        return await SubmitAsync(message, defaultOptions, cancellationToken);
 033    }
 34
 35    /// <inheritdoc />
 36    public async ValueTask<SubmitWorkflowInboxMessageResult> SubmitAsync(NewWorkflowInboxMessage newMessage, WorkflowInb
 37    {
 038        var activityTypeName = newMessage.ActivityTypeName;
 039        var stimulus = newMessage.BookmarkPayload;
 040        var stimulusMetadata = new StimulusMetadata
 041        {
 042            CorrelationId = newMessage.CorrelationId,
 043            WorkflowInstanceId = newMessage.WorkflowInstanceId,
 044            ActivityInstanceId = newMessage.ActivityInstanceId,
 045            Input = newMessage.Input
 046        };
 47
 048        var now = systemClock.UtcNow;
 49
 050        var message = new WorkflowInboxMessage
 051        {
 052            Id = identityGenerator.GenerateId(),
 053            CreatedAt = now,
 054            ExpiresAt = now + newMessage.TimeToLive,
 055            ActivityInstanceId = newMessage.ActivityInstanceId,
 056            CorrelationId = newMessage.CorrelationId,
 057            WorkflowInstanceId = newMessage.WorkflowInstanceId,
 058            ActivityTypeName = newMessage.ActivityTypeName,
 059            BookmarkPayload = newMessage.BookmarkPayload,
 060            Input = newMessage.Input,
 061            Hash = bookmarkHasher.Hash(newMessage.ActivityTypeName, newMessage.BookmarkPayload, newMessage.ActivityInsta
 062        };
 63
 064        var result = await stimulusSender.SendAsync(activityTypeName, stimulus, stimulusMetadata, cancellationToken);
 065        var workflowExecutionResults = Map(result.WorkflowInstanceResponses).ToList();
 66
 067        return new SubmitWorkflowInboxMessageResult(message, workflowExecutionResults);
 068    }
 69
 70    /// <inheritdoc />
 71    public async ValueTask<DeliverWorkflowInboxMessageResult> DeliverAsync(WorkflowInboxMessage message, CancellationTok
 72    {
 073        await ResumeWorkflowsAsynchronouslyAsync(message, cancellationToken);
 074        return new DeliverWorkflowInboxMessageResult(new List<WorkflowExecutionResult>());
 075    }
 76
 77    /// <inheritdoc />
 78    public async ValueTask<DeliverWorkflowInboxMessageResult> BroadcastAsync(WorkflowInboxMessage message, BroadcastWork
 79    {
 080        var activityTypeName = message.ActivityTypeName;
 081        var correlationId = message.CorrelationId;
 082        var workflowInstanceId = message.WorkflowInstanceId;
 083        var activityInstanceId = message.ActivityInstanceId;
 084        var bookmarkPayload = message.BookmarkPayload;
 085        var input = message.Input;
 86
 087        if (workflowInstanceId != null)
 88        {
 089            if (options?.DispatchAsynchronously == true)
 90            {
 091                await ResumeWorkflowsAsynchronouslyAsync(message, cancellationToken);
 092                return new DeliverWorkflowInboxMessageResult(new List<WorkflowExecutionResult>());
 93            }
 94
 095            var results = await ResumeWorkflowsSynchronouslyAsync(message, cancellationToken);
 096            return new DeliverWorkflowInboxMessageResult(results.ToList());
 97        }
 98
 099        if (options?.DispatchAsynchronously == false)
 100        {
 0101            var result = await stimulusSender.SendAsync(activityTypeName, bookmarkPayload, new StimulusMetadata
 0102            {
 0103                CorrelationId = correlationId,
 0104                WorkflowInstanceId = workflowInstanceId,
 0105                ActivityInstanceId = activityInstanceId,
 0106                Input = input
 0107            }, cancellationToken);
 108
 0109            var workflowExecutionResults = Map(result.WorkflowInstanceResponses).ToList();
 0110            return new DeliverWorkflowInboxMessageResult(workflowExecutionResults);
 111        }
 112
 0113        var dispatchRequest = new DispatchTriggerWorkflowsRequest(activityTypeName, bookmarkPayload)
 0114        {
 0115            CorrelationId = correlationId,
 0116            WorkflowInstanceId = workflowInstanceId,
 0117            ActivityInstanceId = activityInstanceId,
 0118            Input = input
 0119        };
 0120        await workflowDispatcher.DispatchAsync(dispatchRequest, cancellationToken);
 0121        return new DeliverWorkflowInboxMessageResult(new List<WorkflowExecutionResult>());
 0122    }
 123
 124    private async Task ResumeWorkflowsAsynchronouslyAsync(WorkflowInboxMessage message, CancellationToken cancellationTo
 125    {
 0126        var activityTypeName = message.ActivityTypeName;
 0127        var correlationId = message.CorrelationId;
 0128        var workflowInstanceId = message.WorkflowInstanceId;
 0129        var activityInstanceId = message.ActivityInstanceId;
 0130        var bookmarkPayload = message.BookmarkPayload;
 0131        var input = message.Input;
 132
 0133        await workflowDispatcher.DispatchAsync(new DispatchResumeWorkflowsRequest(activityTypeName, bookmarkPayload)
 0134        {
 0135            CorrelationId = correlationId,
 0136            WorkflowInstanceId = workflowInstanceId,
 0137            ActivityInstanceId = activityInstanceId,
 0138            Input = input
 0139        }, cancellationToken: cancellationToken);
 0140    }
 141
 142    private async Task<IEnumerable<WorkflowExecutionResult>> ResumeWorkflowsSynchronouslyAsync(WorkflowInboxMessage mess
 143    {
 0144        var activityTypeName = message.ActivityTypeName;
 0145        var correlationId = message.CorrelationId;
 0146        var workflowInstanceId = message.WorkflowInstanceId;
 0147        var activityInstanceId = message.ActivityInstanceId;
 0148        var bookmarkPayload = message.BookmarkPayload;
 0149        var input = message.Input;
 150
 0151        var result = await stimulusSender.SendAsync(activityTypeName, bookmarkPayload, new StimulusMetadata
 0152        {
 0153            CorrelationId = correlationId,
 0154            WorkflowInstanceId = workflowInstanceId,
 0155            ActivityInstanceId = activityInstanceId,
 0156            Input = input
 0157        }, cancellationToken);
 158
 0159        return Map(result.WorkflowInstanceResponses).ToList();
 0160    }
 161
 162    /// <inheritdoc />
 163    public ValueTask<IEnumerable<WorkflowInboxMessage>> FindManyAsync(WorkflowInboxMessageFilter filter, CancellationTok
 164    {
 0165        logger.LogWarning("The workflow inbox API is deprecated and will be removed in a future version. Please use the 
 0166        return new([]);
 167    }
 168
 169    /// <inheritdoc />
 170    public ValueTask<IEnumerable<WorkflowInboxMessage>> FindManyAsync(IEnumerable<WorkflowInboxMessageFilter> filters, C
 171    {
 0172        logger.LogWarning("The workflow inbox API is deprecated and will be removed in a future version. Please use the 
 0173        return new([]);
 174    }
 175
 176    private static IEnumerable<WorkflowExecutionResult> Map(IEnumerable<RunWorkflowInstanceResponse> source)
 177    {
 0178        return source.Select(response => new WorkflowExecutionResult(
 0179            response.WorkflowInstanceId,
 0180            response.Status,
 0181            response.SubStatus,
 0182            new List<Bookmark>(),
 0183            response.Incidents,
 0184            null,
 0185            new Dictionary<string, object>())
 0186        );
 187    }
 188}