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.
This commit is contained in:
Sipke Schoorstra 2025-07-24 19:47:08 +02:00
parent b85ed3b329
commit 18ee694d23
No known key found for this signature in database
GPG key ID: 5C10502B28A4268F
2 changed files with 13 additions and 6 deletions

View file

@ -2,11 +2,11 @@ namespace Elsa.Workflows;
public class DefaultWorkflowInstanceVariableReader(IVariablePersistenceManager variablePersistenceManager) : IWorkflowInstanceVariableReader
{
public async Task<IEnumerable<ResolvedVariable>> GetVariables(WorkflowExecutionContext workflowExecutionContext, IEnumerable<string>? excludeTags = default, CancellationToken cancellationToken = default)
public async Task<IEnumerable<ResolvedVariable>> GetVariables(WorkflowExecutionContext workflowExecutionContext, IEnumerable<string>? 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;

View file

@ -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;
/// <inheritdoc />
public class WorkflowStateExtractor : IWorkflowStateExtractor
public class WorkflowStateExtractor(ILogger<WorkflowStateExtractor> logger) : IWorkflowStateExtractor
{
/// <inheritdoc />
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))