Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/modules/Elsa.Workflows.Runtime/Activities/ExecuteWorkflow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,15 @@ private async ValueTask<ExecuteWorkflowResult> ExecuteWorkflowAsync(ActivityExec

var parentInstanceId = context.WorkflowExecutionContext.Id;
var input = Input.GetOrDefault(context) ?? new Dictionary<string, object>();
var correlationId = CorrelationId.GetOrDefault(context);
// propagate inout from parent workflow to child workflow
if (input.Count == 0 && context.WorkflowExecutionContext.Input != null)
{
input = context.WorkflowExecutionContext.Input;
}

// propagate correlation id from parent workflow to child workflow
var correlationId = context.WorkflowExecutionContext.CorrelationId;

var workflowInvoker = context.GetRequiredService<IWorkflowInvoker>();
var identityGenerator = context.GetRequiredService<IIdentityGenerator>();
var properties = new Dictionary<string, object>
Expand Down Expand Up @@ -117,7 +125,8 @@ private async ValueTask<ExecuteWorkflowResult> ExecuteWorkflowAsync(ActivityExec
WorkflowInstanceId = options.WorkflowInstanceId,
Status = workflowResult.WorkflowState.Status,
SubStatus = workflowResult.WorkflowState.SubStatus,
Output = workflowResult.WorkflowState.Output
Output = workflowResult.WorkflowState.Output,
CorrelationId = correlationId
};

return info;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using Elsa.Common.Models;
using Elsa.Common.Entities;
using Elsa.Common.Models;
using Elsa.Workflows.Activities;
using Elsa.Workflows.ComponentTests.Abstractions;
using Elsa.Workflows.ComponentTests.Fixtures;
using Elsa.Workflows.ComponentTests.Scenarios.ExecuteWorkflows.Workflows;
using Elsa.Workflows.Models;
using Elsa.Workflows.Runtime;
using Elsa.Workflows.Runtime.Filters;
using Elsa.Workflows.Runtime.Messages;
using Elsa.Workflows.Runtime.OrderDefinitions;
using Microsoft.Extensions.DependencyInjection;

namespace Elsa.Workflows.ComponentTests.Scenarios.ExecuteWorkflows;
Expand All @@ -24,8 +28,39 @@ public async Task ExecuteWorkflow_ShouldExecuteWorkflow()
var workflowClient = await _workflowRuntime.CreateClientAsync();
await workflowClient.CreateInstanceAsync(new CreateWorkflowInstanceRequest
{
WorkflowDefinitionHandle = WorkflowDefinitionHandle.ByDefinitionId(MainWorkflow.DefinitionId, VersionOptions.Published)
WorkflowDefinitionHandle = WorkflowDefinitionHandle.ByDefinitionId(MainWorkflow.DefinitionId, VersionOptions.Published),
CorrelationId = "test-correlation-id",
Input = new Dictionary<string, object>
{
["Value"] = 2163
}
});
await workflowClient.RunInstanceAsync(RunWorkflowInstanceRequest.Empty);

var response = await workflowClient.RunInstanceAsync(RunWorkflowInstanceRequest.Empty);

// child activity
var filter = new ActivityExecutionRecordFilter { };
var activityExecutionStore = Scope.ServiceProvider.GetRequiredService<IActivityExecutionStore>();
var orderBy = new ActivityExecutionRecordOrder<DateTimeOffset>(x => x.StartedAt, OrderDirection.Ascending);
var activityExecutionRecords = await activityExecutionStore.FindManyAsync(filter, orderBy);

var textEntries = activityExecutionRecords
.Where(r => r.ActivityState != null && r.WorkflowInstanceId != response.WorkflowInstanceId)
.SelectMany(r => r.ActivityState.Where(x => x.Key == nameof(WriteLine.Text)))
.Select(r => r.Value)
.ToList();
Assert.Single(textEntries);
Assert.Equal("Running subroutine on value 2163 ..., correlation id is 'test-correlation-id'.", textEntries[0]);

// parent activity
textEntries = activityExecutionRecords
.Where(r => r.ActivityState != null && r.WorkflowInstanceId == response.WorkflowInstanceId)
.SelectMany(r => r.ActivityState.Where(x => x.Key == nameof(WriteLine.Text)))
.Select(r => r.Value)
.ToList();
Assert.Single(textEntries);
var childWorkflowInstanceId = activityExecutionRecords.Select(r => r.WorkflowInstanceId).Where(r => r != response.WorkflowInstanceId).FirstOrDefault();
Assert.Equal("Subroutine output: {\"WorkflowDefinitionVersionId\":null,\"WorkflowInstanceId\":\"" + childWorkflowInstanceId +
"\",\"CorrelationId\":\"test-correlation-id\",\"Status\":1,\"SubStatus\":3,\"Output\":{\"Output\":4326}}", textEntries[0]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ protected override void Build(IWorkflowBuilder builder)
new ExecuteWorkflow
{
WorkflowDefinitionId = new(SubroutineWorkflow.DefinitionId),
Input = new(new Dictionary<string, object>
{
["Value"] = 21
}),
Result = new(workflowResult)
},
new WriteLine(context => $"Subroutine output: {JsonSerializer.Serialize(workflowResult.Get(context))}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ protected override void Build(IWorkflowBuilder builder)
{
Activities =
{
new WriteLine(context => $"Running subroutine on value {context.GetInput<double>(valueInput)}..."),
new WriteLine(context => $"Running subroutine on value {context.GetInput<double>(valueInput)} ..., " +
$"correlation id is '" + context.GetWorkflowExecutionContext().CorrelationId + "'."),
new SetOutput
{
OutputName = new(output.Name),
Expand Down
Loading