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:
parent
b85ed3b329
commit
18ee694d23
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
|
|
|
|||
Loading…
Reference in a new issue