-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix workflow variable scope inconsistency (#5558)
* Refactor variable handling in ExpressionExecutionContextExtensions The core change in this commit is the refactoring of the handling of variables within the ExpressionExecutionContextExtensions. The methods GetVariable, CreateVariable, and GetVariableBlock have been altered for clarity and simplified reducing redundant code. Unnecessary parameters and returns in method documentation have been removed, and overall code formatting has been improved to enhance readability. * Simplify MemoryRegister creation in Workflow.cs The creation of the MemoryRegister object in the file Workflow.cs was simplified to one line. The previous method, which declared a new object then called the Declare method before returning, was removed. * Map controller routes in server web program Added a line of code in the Elsa.Server.Web program.cs file to map controller routes. This change ensures that HTTP requests are correctly directed to their corresponding controller actions. * Update workflow state extraction logic The logic in the WorkflowStateExtractor has been updated to retain the root Workflow activity context even if it's completed. This change is necessary to keep workflow-level variables accessible. * Remove RequiresUnreferencedCode attribute from ConvertTo method The RequiresUnreferencedCode attribute was removed from the ConvertTo method in the ObjectConverter class. * Remove unused services and rename test file Unused services in the AutoUpdateTests.cs class were removed, reducing clutter and improving code readability. Additionally, the DeleteWorkflow_Clustered.cs test file has been renamed to DeleteWorkflowClustered.cs for better naming consistency. * Add CountdownStep activity and CountdownWorkflow for testing This commit introduces new component tests for simulations involving counters. It includes a new CountdownStep activity that decrements a counter variable, as well as a CountdownWorkflow which consists of a loop based on the aforementioned activity. It also involves a CountdownWorkflowTests class for testing counter persistence across workflow runs. * Remove unnecessary whitespace in CountdownWorkflowTests This commit eliminates the superfluous whitespace in the CountdownWorkflowTests.cs file. It maintains the proper formatting and ensures code consistency across the test component. * Refactor CountdownWorkflowTests constructor Simplified the constructor of the CountdownWorkflowTests class. The changes remove the unnecessary constructor body and pass the 'app' object directly to the base AppComponentTest class, enhancing the code's readability and maintainability. * Add application roles and configure them in MassTransit A new enum ApplicationRole has been added for distinguishing among different roles (Hybrid, Api, Worker) an application can take. In the configuration of MassTransit, it is now possible to disable the consumers based on application role, which can help optimize the usage of resources and increase application efficiency. * Update Program.cs Switch to Memory broker * Update Program.cs Simplify DisableConsumers assignment. * Update ApplicationRole.cs Rename Hybrid to Default. * Update appsettings.json
- Loading branch information
1 parent
29742db
commit 0422435
Showing
12 changed files
with
131 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ namespace Elsa.Server.Web; | |
|
||
public enum ApplicationRole | ||
{ | ||
Hybrid, | ||
Default, | ||
Api, | ||
Worker | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
test/component/Elsa.Workflows.ComponentTests/Scenarios/Variables/Activities/CountdownStep.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Elsa.Extensions; | ||
|
||
namespace Elsa.Workflows.ComponentTests.Scenarios.Variables.Activities; | ||
|
||
public class CountdownStep : Activity | ||
{ | ||
protected override void Execute(ActivityExecutionContext context) | ||
{ | ||
var counter = context.GetVariable<int>("Counter"); | ||
context.SetVariable("Counter", counter - 1); | ||
context?.CreateBookmark(); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
test/component/Elsa.Workflows.ComponentTests/Scenarios/Variables/CountdownWorkflowTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using Elsa.Expressions.Helpers; | ||
using Elsa.Extensions; | ||
using Elsa.Workflows.ComponentTests.Scenarios.Variables.Workflows; | ||
using Elsa.Workflows.Management.Contracts; | ||
using Elsa.Workflows.Models; | ||
using Elsa.Workflows.Runtime.Contracts; | ||
using Elsa.Workflows.Runtime.Parameters; | ||
using Elsa.Workflows.Services; | ||
using Elsa.Workflows.State; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Elsa.Workflows.ComponentTests.Scenarios.Variables; | ||
|
||
public class CountdownWorkflowTests(App app) : AppComponentTest(app) | ||
{ | ||
[Fact(DisplayName = "Variable is persisted across workflow runs")] | ||
public async Task VariableIsPersistedAcrossWorkflowRuns() | ||
{ | ||
var workflowRuntime = Scope.ServiceProvider.GetRequiredService<IWorkflowRuntime>(); | ||
var workflowInstanceStore = Scope.ServiceProvider.GetRequiredService<IWorkflowInstanceStore>(); | ||
var startParams = new StartWorkflowRuntimeParams(); | ||
var result = await workflowRuntime.StartWorkflowAsync(CountdownWorkflow.DefinitionId, startParams); | ||
var workflowInstanceId = result.WorkflowInstanceId; | ||
var bookmarks = new Stack<Bookmark>(result.Bookmarks); | ||
var expectedCounter = 3; | ||
|
||
while (bookmarks.Any()) | ||
{ | ||
var workflowInstance = await workflowInstanceStore.FindAsync(workflowInstanceId); | ||
var workflowState = workflowInstance!.WorkflowState; | ||
var rootWorkflowActivityExecutionContext = workflowState.ActivityExecutionContexts.Single(x => x.ParentContextId == null); | ||
var variables = GetVariablesDictionary(rootWorkflowActivityExecutionContext); | ||
var actualCounter = variables["Workflow1:variable-1"].ConvertTo<int>(); | ||
Assert.Equal(--expectedCounter, actualCounter); | ||
|
||
var bookmark = bookmarks.Pop(); | ||
var resumeWorkflowRuntimeOptions = new ResumeWorkflowRuntimeParams | ||
{ | ||
BookmarkId = bookmark?.Id, | ||
}; | ||
|
||
result = await workflowRuntime.ResumeWorkflowAsync(workflowInstanceId, resumeWorkflowRuntimeOptions); | ||
|
||
if (result == null) | ||
break; | ||
|
||
foreach (var newBookmark in result.Bookmarks) bookmarks.Push(newBookmark); | ||
} | ||
} | ||
|
||
private IDictionary<string, object> GetVariablesDictionary(ActivityExecutionContextState context) => | ||
context.Properties.GetOrAdd(WorkflowStorageDriver.VariablesDictionaryStateKey, () => new Dictionary<string, object>()); | ||
} |
35 changes: 35 additions & 0 deletions
35
...omponent/Elsa.Workflows.ComponentTests/Scenarios/Variables/Workflows/CountdownWorkflow.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using Elsa.Extensions; | ||
using Elsa.Workflows.Activities; | ||
using Elsa.Workflows.ComponentTests.Scenarios.Variables.Activities; | ||
using Elsa.Workflows.Contracts; | ||
|
||
namespace Elsa.Workflows.ComponentTests.Scenarios.Variables.Workflows; | ||
|
||
public class CountdownWorkflow : WorkflowBase | ||
{ | ||
public static readonly string DefinitionId = Guid.NewGuid().ToString(); | ||
|
||
protected override void Build(IWorkflowBuilder builder) | ||
{ | ||
builder.WithDefinitionId(DefinitionId); | ||
var counter = builder.WithVariable("Counter", 3).WithWorkflowStorage(); | ||
|
||
builder.Root = new Sequence | ||
{ | ||
Activities = | ||
{ | ||
new While(context => counter.Get(context) > 0) | ||
{ | ||
Body = new Sequence | ||
{ | ||
Activities = | ||
{ | ||
new WriteLine(context => $"Counter: {counter.Get(context)}"), | ||
new CountdownStep() | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.