From 18ee694d2360afb64a5359bd4e969f15ce751797 Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Thu, 24 Jul 2025 19:47:08 +0200 Subject: [PATCH] Refactor variable reading and workflow state extraction logic. Revised method parameters, simplified object initializations in `DefaultWorkflowInstanceVariableReader`, and enhanced hierarchy reconstruction in `WorkflowStateExtractor` with logging for missing parent contexts. --- .../DefaultWorkflowInstanceVariableReader.cs | 6 +++--- .../Services/WorkflowStateExtractor.cs | 13 ++++++++++--- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/modules/Elsa.Workflows.Core/Services/DefaultWorkflowInstanceVariableReader.cs b/src/modules/Elsa.Workflows.Core/Services/DefaultWorkflowInstanceVariableReader.cs index c3557bf10..3eca2c3a5 100644 --- a/src/modules/Elsa.Workflows.Core/Services/DefaultWorkflowInstanceVariableReader.cs +++ b/src/modules/Elsa.Workflows.Core/Services/DefaultWorkflowInstanceVariableReader.cs @@ -2,11 +2,11 @@ namespace Elsa.Workflows; public class DefaultWorkflowInstanceVariableReader(IVariablePersistenceManager variablePersistenceManager) : IWorkflowInstanceVariableReader { - public async Task> GetVariables(WorkflowExecutionContext workflowExecutionContext, IEnumerable? excludeTags = default, CancellationToken cancellationToken = default) + public async Task> GetVariables(WorkflowExecutionContext workflowExecutionContext, IEnumerable? excludeTags = null, CancellationToken cancellationToken = default) { var workflow = workflowExecutionContext.Workflow; var workflowVariables = workflow.Variables; - var rootWorkflowActivityExecutionContext = workflowExecutionContext.ActivityExecutionContexts.FirstOrDefault(x => x.ParentActivityExecutionContext == null); + var rootWorkflowActivityExecutionContext = workflowExecutionContext.ActivityExecutionContexts.FirstOrDefault(x => x.Activity == workflow); if (rootWorkflowActivityExecutionContext == null) return []; @@ -17,7 +17,7 @@ public class DefaultWorkflowInstanceVariableReader(IVariablePersistenceManager v foreach (var workflowVariable in workflowVariables) { var value = workflowVariable.Get(rootWorkflowActivityExecutionContext.ExpressionExecutionContext); - resolvedVariables.Add(new ResolvedVariable(workflowVariable, value)); + resolvedVariables.Add(new(workflowVariable, value)); } return resolvedVariables; diff --git a/src/modules/Elsa.Workflows.Core/Services/WorkflowStateExtractor.cs b/src/modules/Elsa.Workflows.Core/Services/WorkflowStateExtractor.cs index 40297f297..f37d2a6b4 100644 --- a/src/modules/Elsa.Workflows.Core/Services/WorkflowStateExtractor.cs +++ b/src/modules/Elsa.Workflows.Core/Services/WorkflowStateExtractor.cs @@ -2,11 +2,12 @@ using Elsa.Extensions; using Elsa.Workflows.Models; using Elsa.Workflows.Services; using Elsa.Workflows.State; +using Microsoft.Extensions.Logging; namespace Elsa.Workflows; /// -public class WorkflowStateExtractor : IWorkflowStateExtractor +public class WorkflowStateExtractor(ILogger logger) : IWorkflowStateExtractor { /// public WorkflowState Extract(WorkflowExecutionContext workflowExecutionContext) @@ -100,7 +101,7 @@ public class WorkflowStateExtractor : IWorkflowStateExtractor workflowExecutionContext.Properties[property.Key] = property.Value; } - private static async Task ApplyActivityExecutionContextsAsync(WorkflowState state, WorkflowExecutionContext workflowExecutionContext) + private async Task ApplyActivityExecutionContextsAsync(WorkflowState state, WorkflowExecutionContext workflowExecutionContext) { var activityExecutionContexts = (await Task.WhenAll( state.ActivityExecutionContexts.Select(async item => await CreateActivityExecutionContextAsync(item)))) @@ -113,7 +114,13 @@ public class WorkflowStateExtractor : IWorkflowStateExtractor // Reconstruct hierarchy. foreach (var contextState in state.ActivityExecutionContexts.Where(x => !string.IsNullOrWhiteSpace(x.ParentContextId))) { - var parentContext = lookup[contextState.ParentContextId!]; + var parentContextId = contextState.ParentContextId; + if (parentContextId == null || !lookup.TryGetValue(parentContextId, out var parentContext)) + { + logger.LogWarning("Parent context with ID '{ParentContextId}' not found for context with ID '{ContextId}'.", parentContextId, contextState.Id); + continue; // Skip if parent context is not found. + } + var contextId = contextState.Id; if (lookup.TryGetValue(contextId, out var context))