| | | 1 | | using Elsa.Common; |
| | | 2 | | using Elsa.Workflows.Models; |
| | | 3 | | using Elsa.Workflows.Runtime.Contracts; |
| | | 4 | | using Elsa.Workflows.Runtime.Entities; |
| | | 5 | | using Elsa.Workflows.Runtime.Filters; |
| | | 6 | | using Elsa.Workflows.Runtime.Messages; |
| | | 7 | | using Elsa.Workflows.Runtime.Options; |
| | | 8 | | using Elsa.Workflows.Runtime.Params; |
| | | 9 | | using Elsa.Workflows.Runtime.Requests; |
| | | 10 | | using Elsa.Workflows.Runtime.Results; |
| | | 11 | | using Microsoft.Extensions.Logging; |
| | | 12 | | |
| | | 13 | | namespace 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")] |
| | 0 | 19 | | public class StimulusProxyWorkflowInbox( |
| | 0 | 20 | | IStimulusSender stimulusSender, |
| | 0 | 21 | | IWorkflowDispatcher workflowDispatcher, |
| | 0 | 22 | | ISystemClock systemClock, |
| | 0 | 23 | | IIdentityGenerator identityGenerator, |
| | 0 | 24 | | IHasher bookmarkHasher, |
| | 0 | 25 | | ILogger<StimulusProxyWorkflowInbox> logger) |
| | | 26 | | : IWorkflowInbox |
| | | 27 | | { |
| | | 28 | | /// <inheritdoc /> |
| | | 29 | | public async ValueTask<SubmitWorkflowInboxMessageResult> SubmitAsync(NewWorkflowInboxMessage message, CancellationTo |
| | | 30 | | { |
| | 0 | 31 | | var defaultOptions = new WorkflowInboxMessageDeliveryParams(); |
| | 0 | 32 | | return await SubmitAsync(message, defaultOptions, cancellationToken); |
| | 0 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <inheritdoc /> |
| | | 36 | | public async ValueTask<SubmitWorkflowInboxMessageResult> SubmitAsync(NewWorkflowInboxMessage newMessage, WorkflowInb |
| | | 37 | | { |
| | 0 | 38 | | var activityTypeName = newMessage.ActivityTypeName; |
| | 0 | 39 | | var stimulus = newMessage.BookmarkPayload; |
| | 0 | 40 | | var stimulusMetadata = new StimulusMetadata |
| | 0 | 41 | | { |
| | 0 | 42 | | CorrelationId = newMessage.CorrelationId, |
| | 0 | 43 | | WorkflowInstanceId = newMessage.WorkflowInstanceId, |
| | 0 | 44 | | ActivityInstanceId = newMessage.ActivityInstanceId, |
| | 0 | 45 | | Input = newMessage.Input |
| | 0 | 46 | | }; |
| | | 47 | | |
| | 0 | 48 | | var now = systemClock.UtcNow; |
| | | 49 | | |
| | 0 | 50 | | var message = new WorkflowInboxMessage |
| | 0 | 51 | | { |
| | 0 | 52 | | Id = identityGenerator.GenerateId(), |
| | 0 | 53 | | CreatedAt = now, |
| | 0 | 54 | | ExpiresAt = now + newMessage.TimeToLive, |
| | 0 | 55 | | ActivityInstanceId = newMessage.ActivityInstanceId, |
| | 0 | 56 | | CorrelationId = newMessage.CorrelationId, |
| | 0 | 57 | | WorkflowInstanceId = newMessage.WorkflowInstanceId, |
| | 0 | 58 | | ActivityTypeName = newMessage.ActivityTypeName, |
| | 0 | 59 | | BookmarkPayload = newMessage.BookmarkPayload, |
| | 0 | 60 | | Input = newMessage.Input, |
| | 0 | 61 | | Hash = bookmarkHasher.Hash(newMessage.ActivityTypeName, newMessage.BookmarkPayload, newMessage.ActivityInsta |
| | 0 | 62 | | }; |
| | | 63 | | |
| | 0 | 64 | | var result = await stimulusSender.SendAsync(activityTypeName, stimulus, stimulusMetadata, cancellationToken); |
| | 0 | 65 | | var workflowExecutionResults = Map(result.WorkflowInstanceResponses).ToList(); |
| | | 66 | | |
| | 0 | 67 | | return new SubmitWorkflowInboxMessageResult(message, workflowExecutionResults); |
| | 0 | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <inheritdoc /> |
| | | 71 | | public async ValueTask<DeliverWorkflowInboxMessageResult> DeliverAsync(WorkflowInboxMessage message, CancellationTok |
| | | 72 | | { |
| | 0 | 73 | | await ResumeWorkflowsAsynchronouslyAsync(message, cancellationToken); |
| | 0 | 74 | | return new DeliverWorkflowInboxMessageResult(new List<WorkflowExecutionResult>()); |
| | 0 | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <inheritdoc /> |
| | | 78 | | public async ValueTask<DeliverWorkflowInboxMessageResult> BroadcastAsync(WorkflowInboxMessage message, BroadcastWork |
| | | 79 | | { |
| | 0 | 80 | | var activityTypeName = message.ActivityTypeName; |
| | 0 | 81 | | var correlationId = message.CorrelationId; |
| | 0 | 82 | | var workflowInstanceId = message.WorkflowInstanceId; |
| | 0 | 83 | | var activityInstanceId = message.ActivityInstanceId; |
| | 0 | 84 | | var bookmarkPayload = message.BookmarkPayload; |
| | 0 | 85 | | var input = message.Input; |
| | | 86 | | |
| | 0 | 87 | | if (workflowInstanceId != null) |
| | | 88 | | { |
| | 0 | 89 | | if (options?.DispatchAsynchronously == true) |
| | | 90 | | { |
| | 0 | 91 | | await ResumeWorkflowsAsynchronouslyAsync(message, cancellationToken); |
| | 0 | 92 | | return new DeliverWorkflowInboxMessageResult(new List<WorkflowExecutionResult>()); |
| | | 93 | | } |
| | | 94 | | |
| | 0 | 95 | | var results = await ResumeWorkflowsSynchronouslyAsync(message, cancellationToken); |
| | 0 | 96 | | return new DeliverWorkflowInboxMessageResult(results.ToList()); |
| | | 97 | | } |
| | | 98 | | |
| | 0 | 99 | | if (options?.DispatchAsynchronously == false) |
| | | 100 | | { |
| | 0 | 101 | | var result = await stimulusSender.SendAsync(activityTypeName, bookmarkPayload, new StimulusMetadata |
| | 0 | 102 | | { |
| | 0 | 103 | | CorrelationId = correlationId, |
| | 0 | 104 | | WorkflowInstanceId = workflowInstanceId, |
| | 0 | 105 | | ActivityInstanceId = activityInstanceId, |
| | 0 | 106 | | Input = input |
| | 0 | 107 | | }, cancellationToken); |
| | | 108 | | |
| | 0 | 109 | | var workflowExecutionResults = Map(result.WorkflowInstanceResponses).ToList(); |
| | 0 | 110 | | return new DeliverWorkflowInboxMessageResult(workflowExecutionResults); |
| | | 111 | | } |
| | | 112 | | |
| | 0 | 113 | | var dispatchRequest = new DispatchTriggerWorkflowsRequest(activityTypeName, bookmarkPayload) |
| | 0 | 114 | | { |
| | 0 | 115 | | CorrelationId = correlationId, |
| | 0 | 116 | | WorkflowInstanceId = workflowInstanceId, |
| | 0 | 117 | | ActivityInstanceId = activityInstanceId, |
| | 0 | 118 | | Input = input |
| | 0 | 119 | | }; |
| | 0 | 120 | | await workflowDispatcher.DispatchAsync(dispatchRequest, cancellationToken); |
| | 0 | 121 | | return new DeliverWorkflowInboxMessageResult(new List<WorkflowExecutionResult>()); |
| | 0 | 122 | | } |
| | | 123 | | |
| | | 124 | | private async Task ResumeWorkflowsAsynchronouslyAsync(WorkflowInboxMessage message, CancellationToken cancellationTo |
| | | 125 | | { |
| | 0 | 126 | | var activityTypeName = message.ActivityTypeName; |
| | 0 | 127 | | var correlationId = message.CorrelationId; |
| | 0 | 128 | | var workflowInstanceId = message.WorkflowInstanceId; |
| | 0 | 129 | | var activityInstanceId = message.ActivityInstanceId; |
| | 0 | 130 | | var bookmarkPayload = message.BookmarkPayload; |
| | 0 | 131 | | var input = message.Input; |
| | | 132 | | |
| | 0 | 133 | | await workflowDispatcher.DispatchAsync(new DispatchResumeWorkflowsRequest(activityTypeName, bookmarkPayload) |
| | 0 | 134 | | { |
| | 0 | 135 | | CorrelationId = correlationId, |
| | 0 | 136 | | WorkflowInstanceId = workflowInstanceId, |
| | 0 | 137 | | ActivityInstanceId = activityInstanceId, |
| | 0 | 138 | | Input = input |
| | 0 | 139 | | }, cancellationToken: cancellationToken); |
| | 0 | 140 | | } |
| | | 141 | | |
| | | 142 | | private async Task<IEnumerable<WorkflowExecutionResult>> ResumeWorkflowsSynchronouslyAsync(WorkflowInboxMessage mess |
| | | 143 | | { |
| | 0 | 144 | | var activityTypeName = message.ActivityTypeName; |
| | 0 | 145 | | var correlationId = message.CorrelationId; |
| | 0 | 146 | | var workflowInstanceId = message.WorkflowInstanceId; |
| | 0 | 147 | | var activityInstanceId = message.ActivityInstanceId; |
| | 0 | 148 | | var bookmarkPayload = message.BookmarkPayload; |
| | 0 | 149 | | var input = message.Input; |
| | | 150 | | |
| | 0 | 151 | | var result = await stimulusSender.SendAsync(activityTypeName, bookmarkPayload, new StimulusMetadata |
| | 0 | 152 | | { |
| | 0 | 153 | | CorrelationId = correlationId, |
| | 0 | 154 | | WorkflowInstanceId = workflowInstanceId, |
| | 0 | 155 | | ActivityInstanceId = activityInstanceId, |
| | 0 | 156 | | Input = input |
| | 0 | 157 | | }, cancellationToken); |
| | | 158 | | |
| | 0 | 159 | | return Map(result.WorkflowInstanceResponses).ToList(); |
| | 0 | 160 | | } |
| | | 161 | | |
| | | 162 | | /// <inheritdoc /> |
| | | 163 | | public ValueTask<IEnumerable<WorkflowInboxMessage>> FindManyAsync(WorkflowInboxMessageFilter filter, CancellationTok |
| | | 164 | | { |
| | 0 | 165 | | logger.LogWarning("The workflow inbox API is deprecated and will be removed in a future version. Please use the |
| | 0 | 166 | | return new([]); |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | /// <inheritdoc /> |
| | | 170 | | public ValueTask<IEnumerable<WorkflowInboxMessage>> FindManyAsync(IEnumerable<WorkflowInboxMessageFilter> filters, C |
| | | 171 | | { |
| | 0 | 172 | | logger.LogWarning("The workflow inbox API is deprecated and will be removed in a future version. Please use the |
| | 0 | 173 | | return new([]); |
| | | 174 | | } |
| | | 175 | | |
| | | 176 | | private static IEnumerable<WorkflowExecutionResult> Map(IEnumerable<RunWorkflowInstanceResponse> source) |
| | | 177 | | { |
| | 0 | 178 | | return source.Select(response => new WorkflowExecutionResult( |
| | 0 | 179 | | response.WorkflowInstanceId, |
| | 0 | 180 | | response.Status, |
| | 0 | 181 | | response.SubStatus, |
| | 0 | 182 | | new List<Bookmark>(), |
| | 0 | 183 | | response.Incidents, |
| | 0 | 184 | | null, |
| | 0 | 185 | | new Dictionary<string, object>()) |
| | 0 | 186 | | ); |
| | | 187 | | } |
| | | 188 | | } |