From 5ed136a41fb31002e53fb1d723cb164b4db2206f Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Thu, 15 Apr 2021 21:38:44 +0200 Subject: [PATCH] Extract auto-Done scheduling to a handler --- .../FodyWeavers.xml | 3 +++ .../ActivityResults/OutcomeResult.cs | 10 +++---- .../ConnectNextActivityViaDefaultOutcome.cs | 26 +++++++++++++++++++ 3 files changed, 32 insertions(+), 7 deletions(-) create mode 100644 src/activities/Elsa.Activities.BlobStorage/FodyWeavers.xml create mode 100644 src/core/Elsa.Core/Handlers/ConnectNextActivityViaDefaultOutcome.cs diff --git a/src/activities/Elsa.Activities.BlobStorage/FodyWeavers.xml b/src/activities/Elsa.Activities.BlobStorage/FodyWeavers.xml new file mode 100644 index 000000000..00e1d9a1c --- /dev/null +++ b/src/activities/Elsa.Activities.BlobStorage/FodyWeavers.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/src/core/Elsa.Abstractions/ActivityResults/OutcomeResult.cs b/src/core/Elsa.Abstractions/ActivityResults/OutcomeResult.cs index 7d3563701..545c5b6e8 100644 --- a/src/core/Elsa.Abstractions/ActivityResults/OutcomeResult.cs +++ b/src/core/Elsa.Abstractions/ActivityResults/OutcomeResult.cs @@ -27,10 +27,6 @@ namespace Elsa.ActivityResults var workflowExecutionContext = activityExecutionContext.WorkflowExecutionContext; var nextConnections = GetNextConnections(workflowExecutionContext, activityExecutionContext.ActivityBlueprint.Id, outcomes).ToList(); - // Always try if we got a "default" connection (from the current activity to the next activity via the default "Done" outcome). - if (!outcomes.Contains(OutcomeNames.Done) && !nextConnections.Any()) - nextConnections = GetNextConnections(workflowExecutionContext, activityExecutionContext.ActivityBlueprint.Id, new[] { OutcomeNames.Done }).ToList(); - var nextActivities = ( from connection in nextConnections @@ -47,15 +43,15 @@ namespace Elsa.ActivityResults } public static IEnumerable GetNextActivities( - WorkflowExecutionContext workflowContext, + WorkflowExecutionContext workflowExecutionContext, string sourceId, IEnumerable outcomes) { - var nextConnections = GetNextConnections(workflowContext, sourceId, outcomes); + var nextConnections = GetNextConnections(workflowExecutionContext, sourceId, outcomes); var query = from connection in nextConnections - from activityBlueprint in workflowContext.WorkflowBlueprint.Activities + from activityBlueprint in workflowExecutionContext.WorkflowBlueprint.Activities where activityBlueprint.Id == connection.Target.Activity.Id select activityBlueprint.Id; diff --git a/src/core/Elsa.Core/Handlers/ConnectNextActivityViaDefaultOutcome.cs b/src/core/Elsa.Core/Handlers/ConnectNextActivityViaDefaultOutcome.cs new file mode 100644 index 000000000..06b81d52e --- /dev/null +++ b/src/core/Elsa.Core/Handlers/ConnectNextActivityViaDefaultOutcome.cs @@ -0,0 +1,26 @@ +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using Elsa.ActivityResults; +using Elsa.Events; +using MediatR; + +namespace Elsa.Handlers +{ + public class ConnectNextActivityViaDefaultOutcome : INotificationHandler + { + public Task Handle(WorkflowExecutionBurstCompleted notification, CancellationToken cancellationToken) + { + var activityExecutionContext = notification.ActivityExecutionContext; + var workflowExecutionContext = activityExecutionContext.WorkflowExecutionContext; + + // Check to see if there are connections with the last-executed activity's "Done" outcome. + // This handles the case for workflows built using the WorkflowBuilder API where activities are chained, expecting the chained activities to execute next, regardless of the activity's outcome. + + var nextActivities = OutcomeResult.GetNextActivities(workflowExecutionContext, activityExecutionContext.ActivityId, new[] {OutcomeNames.Done}).ToList(); + workflowExecutionContext.ScheduleActivities(nextActivities, activityExecutionContext.Output); + + return Task.CompletedTask; + } + } +} \ No newline at end of file